Reputation: 11
I've been consistently running into a problem when running some basic code using the PySerial library in Python. Specifically, when I use the write function given by PySerial, I lose bytes of the commands I am sending to my STM microcontroller.
For example, when running this code :
import serial
stm_serial_port = '/dev/ttyUSB0'
stm_baud_rate = 460800
ser_stm = Serial(stm_serial_port, stm_baud_rate, timeout=1)
cmd = "initc 1 0.6 50 1000 1000 1000 1000 100 100"
encoded_cmd = str.encode(cmd+"\r\n")
for i in range(100):
flush_buffers()
print('Cmd : '+cmd)
ser_stm.write(encoded_cmd)
feedback = ser_stm.readline().decode('utf-8')
print('Echo : '+feedback)
while(True):
data = ser_stm.readline().decode('utf-8')
if 'sh>' in data:
break
I get the following in my terminal:
Cmd : initc 1 0.6 50 1000 1000 1000 1000 100
Echo : intc 0.6 50 1000 1000 1000 1000 100
Cmd : initc 1 0.6 50 1000 1000 1000 1000 100
Echo : initc 1 0.650 1000 1000 1000 1000 100
Cmd : initc 1 0.6 50 1000 1000 1000 1000 100
Echo : initc 1 0.6 50 1000 1000 00 1000 100
Cmd : initc 1 0.6 50 1000 1000 1000 1000 100
Echo : initc 1 0.6 50 10001000 1000 1000 100
About half of the time I do get the proper echo I expected. The other half, a bit or two are missing.
I'm confident that the hardware is not at fault as I can communicate without errors when using putty. The same commands are send and the echo matches.
I've tried compiling the code into an executable, using a different OS (Windows and Ubuntu), making sure I'm using the same settings as in putty.
Does anyone have an idea what the issue might be and how to fix it?
Upvotes: 1
Views: 34