怎样使用PySerial接口Python和Arduino

步骤1:安装
下载后,打开终端并输入:
tar xfvz /users/*account*/downloads/pyserial-2.6.tar.gz
cd pyserial-2.6
sudo python setup.py install
为确保所有安装正确的设备都打开空闲并输入在“导入序列号”中。如果没有错误出现,则一切正常。
您可以通过
ls /dev/tty.*
行检查可用的端口,步骤2:对arduino进行编程
现在进行测试,将以下草图上传到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,然后打印出来的计算机。


!销售/收购/维修HP8647A信号源HP8647A现货!小
2p空气开关接线图详解
泰克将推最全面的400G PAM4成套测试解决方案
来自星星的vivo NEX提供双面屏幕新方案
步进电机原理与驱动方式深度解析
怎样使用PySerial接口Python和Arduino
如何正确穿绕电流互感器
你还在认为电流从火线流向零线?
Zynq-7000 可扩展处理平台问题解答
Sub-GHz无线访问控制物联网系统设计方案
正版SolidWorks软件 不只是三维机械设计软件
半入耳式蓝牙耳机什么牌子好?半入耳式蓝牙耳机排行榜!
苹果公司开始发布macOS Big Sur 11.2的另一个候选版本
万用表使用注意事项汇总
无爱!iOS10.2.1 Beta4更新包高达2GB!逼死16GB党
比特币的低认可度是一种战斗口号
交流电的瞬时功率不是一个恒定值 平均值叫做有功功率
2019年中国人工智能产业准独角兽行业排行榜
今日看点丨龙芯 3A6000 国产桌面处理器发布;消息称华为正研发苹果 Vision Pro 头显竞品
电阻壳体上的条纹代表什么