I am currently using raspberry pi(Raspberry Pi 3 Model B+) to get data from a single pixel thermopile sensor. The data is sent to the serial port of Pi(configured the Pi to use ttyAMA0 port) at a baud rate of 961200. The data that is transmitted is in binary format. The size of the data packet is 1268 bytes with 4 packets per second. I want to be able to reliably read the data from the serial port with no data loss. Currently I have the sample code that I run to receive the data. My issue is I am losing some data from the packets and I am not able to figure out how I can be sure that I receive the complete data. Can anyone help me with my problem.
Code:
Code: Select all
import time, serial,json,base64,binascii
from threading import Timer
ser = serial.Serial('/dev/ttyAMA0',961200)
reading = ser.read(1268)
data = ser.inWaiting()
print data
print reading
rb = bytearray(reading)
rs = bytearray(data)
test = binascii.hexlify(rb)
test1 = binascii.hexlify(rs)
print test
if reading is None:
print "testnotsuccded"
else:
print "succeeded"