I'm testing serial communication between two Pi's (Zero -> Pi 4) with Python pyserial library. I am using Zero as camera with preprocessing and sending preprocessed data to Pi 4 via UART (/dev/ttyS0). Camera is working properly at 37FPS (27ms) and sending data (two float values - 8 bytes) to Pi 4 only on request. Pi 4 receive all 8 bytes and converts them into two floats successfully. BUT reading data on Pi 4 takes approx. 75ms. I found out that function pyserial.Serial.read(bytes) processes those 75ms. I also tried changing baudrate to different values (115200 and 250000) but with no effect. Same result was when I changed Pi 4 to Pi 3B+.
What might be the cause of slow reading from serial buffer using pyserial library?
Raspberry Pi Zero code:
with Serial('/dev/ttyS0', 250000) as ser:
for frame in camera.capture_continuous(stream, format="yuv", use_video_port=True):
start = time()
ser.read(1)
regions = self.__Regions(frame, self.__channel, self.__threshold, self.__disk)
if (regions): coordinates = self.__Coordinates(regions)
else: coordinates = [-1.0, -1.0]
for value in coordinates: ser.write(pack('f', value))
stream.truncate(0)
end = time()
print(end - start) # prints out 0.027s which is 37FPS
Raspberry Pi 4B code:
with Serial('/dev/ttyS0', 250000) as ser:
while True:
ser.write(1)
start = time()
data = ser.read(8) # this takes 0.075s
end = time()
print(list(unpack('ff', data)))
print(end - start) # prints out 0.075s