简体   繁体   English

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

[英]Writing serial data to Arduino using pyserial on Windows

I am having some trouble writing serial data to an Arduino Uno using pyserial on a 64 bit Windows 10 machine with Python 3.7. 我在使用python 3.7的64位Windows 10计算机上使用pyserial将串行数据写入Arduino Uno时遇到一些麻烦。

Here is the stripped down version of the code I am testing: 这是我正在测试的代码的简化版本:

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()

The issue I have when I run the above code is that I don't get an echo back from the Arduino. 当我运行上面的代码时,我遇到的问题是我没有收到Arduino的回声。 I do see the Connected print statement as well as the characters of erial connected come through (as expected) but no 5 s or 6 s and the built in LED does not turn on. 我确实看到了Connected打印语句以及erial connectederial connected字符通过(如预期的那样),但是没有5 s或6 s,并且内置的LED灯没有打开。

I know there isn't an issue with the Arduino or its connection to the computer because it works just fine using the Arduino Serial monitor (all characters echo back just fine and the built in led turns on after receiving the first character). 我知道Arduino或它与计算机的连接没有问题,因为它可以在Arduino串行监视器上正常工作(所有字符回传得很好,并且在收到第一个字符后内置的led指示灯亮起)。

If I use a non-zero value for writeTimeout I get the following traceback as soon as the com.write runs (even if I set writeTimeout to 1000 ): 如果我对writeTimeout使用非零值, writeTimeoutcom.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

I also get the same traceback if I attempt to send a character to the arduino program using miniterm . 如果尝试使用miniterm将字符发送到arduino程序,我也会得到相同的回溯。

I have commented in SerialEcho.py a couple of other changes I have tried which haven't solved the issue. 我在SerialEcho.py评论了我尝试过的其他一些未解决问题的更改。

I have come across some posts suggesting issues with different versions of pyserial on windows. 我遇到过一些帖子,建议在Windows上使用不同版本的pyserial出现问题。 I have tried the above on most combinations of Python 3.7 32bit and 64bit along with pyserial 3.4, 2.7 and 2.6 but none work for me. 我已经在python 3.7 32bit和64bit以及pyserial 3.4、2.7和2.6的大多数组合上尝试了上述方法,但对我来说都不起作用。

I have come across many different posts here on Stack Overflow as well as on the Arduino forums while trying to solve this issue but either there is no answer or the answer doesn't work for me. 在尝试解决此问题时,我在Stack Overflow和Arduino论坛上遇到了很多不同的帖子,但是都没有答案,或者答案对我不起作用。 I have tried my best to cover all the answers I have found. 我已尽力涵盖了所有找到的答案。

Thanks 谢谢

Edit 编辑

I have modified the above test code to include more changes that haven't fixed the issue. 我修改了上面的测试代码,以包含更多无法解决问题的更改。

I had issue with arduino and python too, I just give you some tips, first add a delay after arduino connection, So: 我也有arduino和python的问题,我只给你一些提示,首先在arduino连接后添加延迟,所以:

com.open()
time.sleep(1)

This is my send function which will send with a new line character: 这是我的send函数,它将以换行符发送:

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

and These are my read and read line functions: 这些是我的读取和读取行函数:

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

And this is my loop: 这是我的循环:

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

Hope this will help. 希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM