Python 从树莓派发送串行通信

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21866762/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:46:19  来源:igfitidea点击:

Sending serial communication from Raspberry pi

pythonserial-portarduinoraspberry-pi

提问by Daanii

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:

我正在使用 Python 程序将串行数据从 Raspberry Pi 发送到 Arduino。我正在运行 Python 2.7.3。该计划是:

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 串行监视器,该程序也能正常运行,无需在 shell 中逐行运行。

EDITED TO ADD:

编辑添加:

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.

发送到 Arduino 似乎有一些延迟。所以当我在解释模式下运行代码时,它可以工作,但如果作为程序,它就不行。我认为这是因为我在 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. But not if run as a program.

如果我在解释模式下运行程序,或者甚至在调试模式下在 ser.write 命令上设置断点,它都可以工作。但如果作为程序运行则不然。

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. That works.

我在 RESET 引脚和地之间使用了一个 220 uF 的电容器。那个有效。

Tough to be bitten by a bug like that! It still smarts.

被这样的虫子咬了好难受!它仍然很聪明。

回答by Cugomastik

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.

尝试这个。如果您无法在空闲等情况下运行它,请通过键入 python name.py 来尝试终端。我还建议您使用腻子检查来自/写入 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)