[英]Sending serial communication from Raspberry pi
I am sending serial data from a Raspberry Pi to an Arduino using a Python program. 我正在使用Python程序将Raspberry Pi的串行数据发送到Arduino。 I am running Python 2.7.3.
我正在运行Python 2.7.3。 The program is:
该程序是:
import serial
ser = serial.Serial('/dev/ttyACM0', 115200)
ser.write(b'\x4c\xff\x46')
The problem is that nothing seems to be sent by these three lines if they are run in a program. 问题在于,如果这三行在程序中运行,似乎什么也不会发送。 But if I run them line by line in a Python shell, they work fine.
但是,如果我在Python Shell中逐行运行它们,它们会正常工作。
Also, if I have the Arduino Serial Monitor open, the program works fine as well, without running the lines one by one in the shell. 另外,如果我打开了Arduino Serial Monitor,该程序也可以正常运行,而无需在外壳中逐行运行。
EDITED TO ADD: 编辑添加:
It seems that there is some delay in sending to the Arduino. 似乎发送到Arduino会有一些延迟。 So when I run the code in interpretive mode, it works, but if as a program, it doesn't.
因此,当我在解释性模式下运行代码时,它可以工作,但是如果作为程序,则不能。 I think that because I tried the same program on a Windows machine.
我认为是因为我在Windows计算机上尝试了相同的程序。
import serial
ser = serial.Serial('COM8', 115200)
ser.write(b'\x4c\x20\x46')
If I run the program in interpretive mode, or even in debugging mode with a breakpoint on the ser.write command, it works. 如果我以解释性模式运行程序,甚至在ser.write命令上带有断点的调试模式下运行程序,它都可以工作。 But not if run as a program.
但是,如果不是作为程序运行,则不会。
EDITED TO ADD MORE: 编辑以添加更多:
It turns out that the Arduino has an auto-reset on serial communications that has to be disabled: 事实证明,Arduino具有必须禁用的串行通信自动重置功能:
http://playground.arduino.cc/Main/DisablingAutoResetOnSerialConnection#.UwP_wfldV8E http://playground.arduino.cc/Main/DisablingAutoResetOnSerialConnection#.UwP_wfldV8E
http://forum.arduino.cc/index.php/topic,28723.0.html http://forum.arduino.cc/index.php/topic,28723.0.html
I used a 220 uF capacitor between the RESET pin and ground. 我在RESET引脚和地面之间使用了一个220 uF电容器。 That works.
这样可行。
Tough to be bitten by a bug like that! 很难被这样的错误咬住! It still smarts.
它仍然很聪明。
Try this. 尝试这个。 If you can't run it under idle or etc, try terminal by typing python name.py.
如果您不能在空闲或其他状态下运行它,请尝试通过键入python name.py来尝试终端。 I also suggest you to check the data coming or written from/to Rpi with putty to be sure.
我还建议您使用腻子检查来自Rpi或从Rpi写入的数据。
import serial
import time
def readlineCR(port):
rv = ""
while True:
ch = port.read()
rv += ch
if ch == '\r' or ch == '':
return rv
port = serial.Serial("/dev/ttyAMA0", baudrate = 7200, timeout = 2)
while True:
rcv = readlineCR(port)
port.write("I typed: " + repr(rcv))
print(rcv)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.