步骤1:安装
下载后,打开终端并输入:
tar xfvz /Users/*Account*/Downloads/pyserial-2.6.tar.gz
cd pyserial-2.6
sudo python setup.py install
为确保所有安装正确的设备都打开空闲并输入在“导入序列号”中。如果没有错误出现,则一切正常。
您可以通过
ls /dev/tty.*
现在进行测试,将以下草图上传到Arduino。我不知道这在Arduino克隆上将如何工作。
void setup() {
Serial.begin(9600); // set the baud rate
Serial.println(“Ready”); // print “Ready” once
}
void loop() {
char inByte = ‘ ’;
if(Serial.available()){ // only send data back if data has been sent
char inByte = Serial.read(); // read the incoming data
Serial.println(inByte); // send the data back in a new line so that it is not all one long line
}
delay(100); // delay for 1/10 of a second
}
步骤3:程序空闲
下一步在Idle中创建一个新窗口并创建以下程序。
from time import sleep
import serial
ser = serial.Serial(‘/dev/tty.usbmodem1d11’, 9600) # Establish the connection on a specific port
counter = 32 # Below 32 everything in ASCII is gibberish
while True:
counter +=1
ser.write(str(chr(counter))) # Convert the decimal number to ASCII then send it to the Arduino
print ser.readline() # Read the newest output from the Arduino
sleep(.1) # Delay for one tenth of a second
if counter == 255:
counter = 32
请记住两点。要确定您的Arduino连接了哪个串行端口,请查看Arduino草图的右下角。不管是什么,都应该是Python程序第3行中的引号。
您还可以更改Python程序第3行和Arduino程序的第2行中的波特率,只要它们保持不变即可。程序运行后,它将打印出大多数ASCII字符。首先将它们发送到Arduino,然后将其发送回Python,然后打印出来的计算机。
责任编辑:wv
-
python
+关注
关注
56文章
4832浏览量
87796 -
Arduino
+关注
关注
190文章
6501浏览量
193111
发布评论请先 登录
python app不能运行怎么解决?
【Milk-V Duo S 开发板免费体验】Milk-V DuoS之使用Arduino开发小核
零基础入门:如何在树莓派上编写和运行Python程序?

STM32开发板一般都会配置arduino接口,是不是基本上也都会支持arduino?
DLP6500能否用Python编程进行开发,是否有API接口?
使用Python实现xgboost教程
I2C总线与Arduino的接口示例
华为云 Flexus X 实例下的场景体验——小企业使用 Python 语言——超迅速搭建简单公网 API 接口服务

评论