简体   繁体   中英

PySerial readline() first returns command, then the answer

I have programmed a device (Trinket M0 3V) to interface with Python over a virtual COM port. When I test it in a serial monitor it works just fine, but when I switch to PySerial I run into problems. Specifically, I do the following:

import serial
ser = serial.Serial(physicalAddress, timeout = 5)
command = 'bogus\r'.encode()
ser.write(command)
response = ser.readline()

The response that I get is just the command I've written on the line preceding it. However, when I execute ser.readline() again I do get the response that I need. Since everything works fine when I try communicating on my serial monitor it suggests that there's something I'm missing in my use of PySerial, but that's about as far as I'm able to get.

Did you find the infos of https://www.thecodingforums.com/threads/pyserial-unexpected-local-echo.711453/ ?
It had to be linked to the way the echo is handled, and the link gives steps to see if the echo is local or distant, and more...
I didn't find anything about a local echo done by PySerial!
But on the serial monitor (which one?), it seems to be possible to configure it, hence the reason you didn't see it!? ( https://community.platformio.org/t/solved-enable-just-local-echo-in-serial-monitor/539 )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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