I'm on Windows, reading data via Serial from another device at 115200 baud rate. The data coming in is from a microcontroller which has a sensor connected to it sending integer sensor (gyroscope) readings ranging from 1 to 25.
I used PuTTY to connect to it and I can read those values perfectly and they update almost instantly when I move my gyroscope.
However, when I use the python code below, it takes almost 20-25 seconds for it to update after I move the gyroscope. Why is it taking so long? How do I fix it? I've tried all sorts of things like changing timeout, adding sleep delays, nothing works.
import serial
ser = serial.Serial(COM1, 115200, timeout=0)
ser.flushInput()
while True:
DATA = ser.readline()
VAL = DATA[0:len(DATA)-1].decode("utf-8")
print(VAL)
EDIT:
import serial
import time
ser = serial.Serial(COM1, 115200, timeout=0)
ser.flushInput()
while True:
DATA = ser.readline()
VAL = DATA[0:len(DATA)-1].decode("utf-8")
print(VAL)
bufClear = ser.read(ser.inWaiting())
time.sleep(0.5)
Now I can get it to update quickly, however it seems to cut out some info. Sometimes. For example if I move my gyroscope to a value that corresponds to 22. The print output would be like
22
22
2
2
2
22