树莓PI发送串行通信 [英] Sending serial communication from Raspberry pi
问题描述
我从树莓派给一个Arduino使用Python程序发送串行数据。我正在运行的Python 2.7.3。该计划是:
I am sending serial data from a Raspberry Pi to an Arduino using a Python program. I am running Python 2.7.3. The program is:
import serial
ser = serial.Serial('/dev/ttyACM0', 115200)
ser.write(b'\x4c\xff\x46')
的问题是,似乎没有任何如果它们在一个程序运行,以通过这三行发送。但是,如果我在Python shell中运行他们一行行,他们的工作很好。
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.
另外,如果我有Arduino的串行监视器打开时,程序正常工作为好,但不运行线路逐一外壳。
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一些延迟。所以,当我运行间pretive模式code,它的工作原理,但如果作为一个程序,它没有。我认为,因为我尝试在Windows机器上相同的程序。
It seems that there is some delay in sending to the 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.
import serial
ser = serial.Serial('COM8', 115200)
ser.write(b'\x4c\x20\x46')
如果我在跑跨pretive模式下运行程序,甚至在上ser.write命令断点调试模式下,它的工作原理。但如果作为程序运行。
If I run the program in interpretive mode, or even in debugging mode with a breakpoint on the ser.write command, it works. But not if run as a program.
编辑,以增加更多的:
原来,Arduino的对串行通信的自动复位有被禁用:
It turns out that the Arduino has an auto-reset on serial communications that has to be disabled:
<一个href=\"http://playground.arduino.cc/Main/DisablingAutoResetOnSerialConnection#.UwP_wfldV8E\">http://playground.arduino.cc/Main/DisablingAutoResetOnSerialConnection#.UwP_wfldV8E
<一个href=\"http://forum.arduino.cc/index.php/topic,28723.0.html\">http://forum.arduino.cc/index.php/topic,28723.0.html
我用了RESET引脚与地之间的220 uF的电容。这样可行。
I used a 220 uF capacitor between the RESET pin and ground. That works.
很难通过这样的错误咬!它仍然智慧。
Tough to be bitten by a bug like that! It still smarts.
推荐答案
试试这个。如果你不能在空闲或等运行,键入蟒蛇name.py.尝试终端我也建议你来检查数据未来或/与RPI用腻子写是肯定的。
Try this. If you can't run it under idle or etc, try terminal by typing python name.py. I also suggest you to check the data coming or written from/to Rpi with putty to be sure.
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)
这篇关于树莓PI发送串行通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!