Bipra
Posts: 3
Joined: Fri Mar 29, 2019 2:23 pm

Reliably read sensor data

Fri Mar 29, 2019 2:36 pm

Hello guys,

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"
EDIT: moderator added code tags

User avatar
joan
Posts: 14960
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Reliably read sensor data

Fri Mar 29, 2019 3:18 pm

Why not use a lower baud rate? That is likely to be more reliable.

ghp
Posts: 1517
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: Reliably read sensor data

Fri Mar 29, 2019 7:57 pm

It could help to know the type of sensor and then also where to find a datasheet.
What I think is strange is the size of the data package: '1268 byte' for a 'single pixel ... sensor'.
Many sensors with long data packages have some message frame including checksum which allows to find out if something is wrong.
Is there some handshake available ?

Andyroo

Re: Reliably read sensor data

Fri Mar 29, 2019 8:37 pm

One thought would be to collect the data in one stream, pop the read data onto a Python queue or pipe and then process the data in another stream.

Have a look at the multiprocessing module at https://docs.python.org/3.5/library/mul ... ssing.html as that should take multiple cores rather than the single core threads use.

At that speed thought I would see if you can buffer the data with a fast Arduino or similar and pass it back under serial control (even just RTS/CTS). Can you get a SPI or I2C version / interface?

Bipra
Posts: 3
Joined: Fri Mar 29, 2019 2:23 pm

Re: Reliably read sensor data

Thu Apr 11, 2019 1:11 pm

Hello ghp,

Yes I made it work. We could not lower the baud rate as it was not permitted by the embeded team. Your suggestion to check the frames did the trick. I now can see from the converted data if I am missing any frames in packets and I saw that I am not missing any frames at the moment so I think I can be sure that I am not missing any packets of data. Thanks for the help

Return to “Python”