步骤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
+关注
关注
58文章
4889浏览量
90332 -
Arduino
+关注
关注
190文章
6527浏览量
197521
发布评论请先 登录
使用PYTHON进行的跨平台仿真
[VirtualLab] 使用Python运行VirtualLab Fusion光学仿真
强强联合:imc FAMOS内嵌Python接口,提高分析效率!
微店商品列表API接口指南
没有专利的opencv-python 版本
淘宝商品详情API接口技术解析与实战应用
Python调用API教程
淘宝商品详情接口(item_get)企业级全解析:参数配置、签名机制与 Python 代码实战
怎样使用PySerial接口Python和Arduino
评论