0

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
AlfroJang80
  • 125
  • 2
  • 8
  • How do you run your script? Python has a notorious issue with buffering output. If you run it from a terminal, try adding the `-u` flag. See [this](https://stackoverflow.com/questions/14258500/python-significance-of-u-option) for further details. – BoboDarph Feb 25 '19 at 13:42
  • Take a look at this may it help you: https://stackoverflow.com/a/47235876/6808714 – Mahrez BenHamad Feb 25 '19 at 13:44
  • L.E: Or if you use Python>=3.3, the print() function has a `flush` argument. Just call it with `print(VAL, flush=True)` – BoboDarph Feb 25 '19 at 13:48
  • Just side notes, `DATA[0:len(DATA)-1].decode("utf-8")` probably should be decoded before doing string operations, and `DATA.decode("utf-8")[:-1]` will accomplish the same thing. – tgikal Feb 25 '19 at 13:54
  • @BoboDarph I use IDLE to run it. I tried -u directly via cmd - no luck. Also tried the flush=True argument in print(). Same issue. I don't think print is causing the issue. It's weird that PuTTy receives it perfectly fine. – AlfroJang80 Feb 25 '19 at 14:01
  • @BoboDarph have a look at my edit. Might help – AlfroJang80 Feb 25 '19 at 14:32
  • In your updated version, you are explicitly reading at least part of the data (into `bufClear`) and then *doing nothing with it* - of course that loses data! Your root problem is calling `.readline()` with a timeout of 0 - that will usually get you no more than one character at a time, which you are then chopping off. (The string will end with a newline only if a timeout did not occur.) – jasonharper Feb 25 '19 at 14:47
  • @jasonharper I see. I just tried increasing timeout to 0.5s, 1s and 5s and removed the bufClear lines. Now, it's taking really long to update like before. – AlfroJang80 Feb 25 '19 at 14:56

0 Answers0