pyserial not replenish to input buffer immediately after received data?

Viewed 36

I want to use pyserial module to read serial data sent by Arduino, the data update rate is about 250Hz, one data consists of 29 bytes and the baud rate is 230400.

When my code is running, I found that the pyserial input buffer does not automatically replenish data from arduino until about 0.5 seconds has passed. Is there any way to replenish the input buffer immediately after data received from Arduino?

Many thanks!

Adam Shiau


import serial
import time 

ser = serial.Serial()
old = 0

while True
    if ser.in_waiting > 29:
       print('buf: ', ser.readInputBuffer())
       new = time.perf_counter_ns()
       print(ser.readBinaryList(29))
       print(''%.1f\n %((new - old)*1e-3))
       old = new



# updated function definition_07/23

def readBinaryList(mum):     
    data_r = ser.read(mum)
    data_r = [i for i in data_r]   
    return data_r

def readInputBuffer(self):
    return ser.in_waiting

result1

result2

1 Answers

ser.readInputBuffer() and ser.readBinaryList() look weird. I cannot find those functions in the docs. If I was writing this code, I would just use ser.read(29)

import serial
import time 

ser = serial.Serial()
old = 0

while True
    if ser.in_waiting > 29:
    new = time.perf_counter_ns()
    print(ser.read(29))
    print(''%.1f\n %((new - old)*1e-3))
    old = new
Related