I am coding some python to work with some wireless serial devices. I have 2 devices reading temperatures and other environmental data over an Xbee which then writes the data over serial.
The data looks like this when read in Hex:
The important character is the 7e at the beginning which is the start delimiter for the packets - device information and sensor data follows that.
I have the following up to now:
import serial ser = serial.Serial('com6',9600,timeout=1) while 1: Data_in = ser.readline().encode('hex') if Data_in[0:2] == '7e': Data_in.split('7e', -1) print "==========================" print "Found Packet: " + Data_in if Data_in[6:8] == '90': print "Packet Type = ZgBee RX Packet" AH = Data_in [10:18] AL = Data_in [18:26] print "Device Address = ", AH, AL TH = Data_in [34:36] TL = Data_in [38:40] THc = int(TH, 16) TLc = int(TL, 16) print "Temperature: " , THc , "." , TLc print "=======================" print " "
That is working fine for now but when I introduce 2 devices the split isn't working as expected and the data arrives over serial as follows:
As you can see there are 2 packets in there which as stated above is seprated by the 7e in the middle there.
Is there an alternative way of doing the split?
The devices communicate with 1 coordinator (COM6) so I can't read them over seperate serial lines either.
I can't do it by the length of the packet either as there could be different packet types which will be different lengths. I could do with a way of splitting the data that will seperate the 2 streams but still allow me to pick out the individual characters from the string
Any ideas on a possible method?
The above works if the devices don't sync and send at different intervals but they seem to always end up doing that in the end...