I'm using pySerial library to read serial data from Arduino's basic example "AnalogReadSerial" into Python 3.7.2. There is an erratic delay of approx. 16 milliseconds and an unusual peak of approx. 1.5 seconds observed in every recording. (Printing time taken by Arduino is approx. 5 milliseconds/data point)
Is this delay normal when using pySerial or is there something wrong with my code?
If delay in pySerial is usually seen, can you suggest a better library to read serial data?
Hacks tried:
- I tried putting a sufficient delay in Arduino code (delay of 5ms) -- similar delay results.
- Suggestions in this thread -- PySerial delay in reading line from Arduino --incorporated "in_waiting"
- Took into account Arduino's printing time (approx. 5ms)
import serial
import time
serialport = serial.Serial('COM3',9600) #define my port
count =1
timedata = []
while count<=100: #run for 100 serial values
if serialport.in_waiting > 0: #buffer
count += 1
t1 = int(round(time.time()*1000)) #time before reading
reading = serialport.readline().decode() #read serial data and decode
t2 = int(round(time.time()*1000)) #time after reading
finalt = t2 - t1 #time taken to read
timedata.append(finalt) #store all time values in a list
print(timedata[-1]) #print time for reading every new value
I got similar results nearly every time. A peak of 1.5 seconds once during the code run and otherwise erratic 16ms delay.
Here is an image of the graph:

Appreciate your time.