[英]How to send string to arduino from raspberry pi using python
I have an Arduino microcontroller (Adafruit feather M0), and if I have it plugged into my windows PC and open the serial monitor, I can type P 0 2 1 0
and then enter
, the Arduino interprets this command and works as expected.我有一个 Arduino 微控制器(Adafruit Feather M0),如果我将它插入 Windows PC 并打开串行监视器,我可以输入P 0 2 1 0
然后enter
,Arduino 会解释此命令并按预期工作。 Now I want to plug in the same device into my raspberry pi, and have python do this instead.现在我想将同一个设备插入我的树莓派,让 python 来代替。 I have done this in the past on a different microcontroller (sparkfun pro micro) and it worked great.我过去在不同的微控制器(sparkfun pro micro)上做过这个,效果很好。 I can't seem to get it to work and am not sure what I am missing.我似乎无法让它工作,也不确定我错过了什么。
python code:蟒蛇代码:
import serial
import time
ser = serial.Serial('/dev/ttyACM0', 115200, timeout = None)
time.sleep(2)
returnKey = "\r\n"
ser.write(str.encode("P 2 1 0"))
ser.write(str.encode(returnKey)
The code finishes, but the Arduino simply does nothing, as if it discarded the command or didn't get it at all.代码完成了,但 Arduino 什么也不做,就好像它丢弃了命令或根本没有得到它。 The serial speed is matching on the Arduino too (115200). Arduino 上的串行速度也匹配(115200)。
I also tried:我也试过:
ser.write(b'P 2 1 0')
ser.write(returnKey.encode())
Any ideas what I am missing here?任何想法我在这里缺少什么?
Solved it.解决了。 Ended up using结束使用
import serial
import time
ser = serial.Serial('/dev/ttyACM0', 115200, timeout = None)
time.sleep(2)
ser.write(b'P 2 1 0 \r\n')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.