python - 在 Windows 上使用 pyserial 将串行数据写入 Arduino

标签 python windows arduino serial-port pyserial

我在使用 Python 3.7 的 64 位 Windows 10 计算机上使用 pyserial 将串行数据写入 Arduino Uno 时遇到一些问题。

这是我正在测试的代码的精简版本:

SerialEcho.ino

void setup() {
    pinMode(LED_BUILTIN, OUTPUT);
    digitalWrite(LED_BUILTIN, LOW);

    Serial.begin(9600);
    Serial.println("Serial connected");
}

void loop() {
    int c = Serial.read();
    if (c < 0) return;
    digitalWrite(LED_BUILTIN, HIGH);
    Serial.write(c);
}

SerialEcho.py

from sys import stdout
import serial
import time

port = "COM14"
baud_rate = 9600

com = serial.Serial()
com.port = port
com.baudrate = baud_rate

# Doesn't effect write issues
com.timeout = 0.1

# 0 = no exception but no echo, anything else = instant SerialTimeoutException
com.writeTimeout = 0

# setDTR works as expected but doesn't help the issue
# True = Arduino resets on open, False = Arduino doesn't reset on open
com.setDTR(True)

# EDIT: This doesn't seem to do anything
#com.setRTS(False)

com.open()

# Adding a sleep here has no effect because I am already waiting for a character to come through from the Arduino
time.sleep(1)

# Wait for a character to come through to ensure there is no issue with auto resetting
while not com.read():
    pass

print("Connected")

# This should be echoed back but it isn't
# EDIT: adding a new line character doesn't fix the issue
com.write(b'55555555555555555555555555555555555\n')

# Neither of these lines have any effect on the issue
#com.flush()
#com.flushOutput()

# This is always 0
#print(com.outWaiting())
print(com.out_waiting)

while True:
    c = com.read()
    if c:
        print(c, end=" ")
        com.write(b'6')
        stdout.flush()

运行上述代码时遇到的问题是我没有从 Arduino 收到回声。我确实看到了Connected print 语句以及 erial connected 的字符通过(如预期)但没有 56 s 并且内置 LED 不亮。

我知道 Arduino 或其与计算机的连接没有问题,因为它使用 Arduino 串行监视器工作得很好(所有字符都很好地回显,并且内置 LED 在收到第一个字符后打开) .

如果我对 writeTimeout 使用非零值一旦 com.write 我就得到以下回溯运行(即使我将 writeTimeout 设置为 1000 ):

Traceback (most recent call last):
File "SerialEcho/SerialEcho.py", line 30, in <module>
    com.write(b'55555555555555555555555555555555555')
File "C:\Users\James\AppData\Local\Programs\Python\Python37-32\lib\site-packages\serial\serialwin32.py", line 323, in write
    raise writeTimeoutError
serial.serialutil.SerialTimeoutException: Write timeout

如果我尝试使用 miniterm 将字符发送到 arduino 程序,我也会得到相同的回溯。 .

我在 SerialEcho.py 中评论过我尝试过其他一些更改,但没有解决问题。

我遇到过一些帖子,建议 Windows 上不同版本的 pyserial 存在问题。我已经在 Python 3.7 32 位和 64 位以及 pyserial 3.4、2.7 和 2.6 的大多数组合上尝试了上述方法,但没有一个对我有用。

在尝试解决此问题时,我在 Stack Overflow 以及 Arduino 论坛上遇到了许多不同的帖子,但要么没有答案,要么答案对我不起作用。我已尽力涵盖我找到的所有答案。

谢谢

编辑

我修改了上面的测试代码以包含更多尚未解决问题的更改。

最佳答案

我也遇到了arduino和python的问题,我只是给你一些提示,首先在arduino连接后添加延迟,所以:

com.open()
time.sleep(1)

这是我的发送函数,它将使用换行符发送:

def ArduinoSend(data):
    arduino.write(format(('{}\n').format(data)).encode())

这些是我的读取和读取行函数:

def ArduinoRead():
    if Serial.in_waiting:
        data = arduino.read().decode('ascii').strip()
    return data

def ArduinoReadLine():
    if Serial.in_waiting:
        data = arduino.readline().decode('ascii').strip()
        return data

这是我的循环:

while arduino.is_open:
        info = ArduinoRead()
        time.sleep(0.2)

希望这会有所帮助。

关于python - 在 Windows 上使用 pyserial 将串行数据写入 Arduino,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54303118/

相关文章:

arduino - 如何在arduino上计算256模校验和

javascript - Python 闭包 vs javascript 闭包

java - 传输端点错误

c++ - 液晶屏旋转1位位图

c - 将 SQLite FTS 扩展构建为 DLL 的正确方法是什么?

c++ - 多个粗体菜单项

c++ - 在 C/C++ 中使用字符串——[ ] 在 LPCTSTR 之后

python - 如何让 Python 的单元测试不捕获异常?

python - 将数据框中的列转换为百分位排名 - Python 3.x

python - 如何在 Mac OS X 和 Linux 上加载 Cython 编译的 .so 文件?