Hi,

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:
7e001090007d33a200408b2e0c2db7010016005763

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:
7e001090007d33a200408b2e0c2db70100160057637e001090007d33a2004089e04a569d01001600
3885

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...

Recommended Answers

All 3 Replies

Somethin like this?

for  data in "7e001090007d33a200408b2e0c2db70100160057637e001090007d33a2004089e04a569d010016003885".split('7e'):
    if data[4:6] == '90':
        print "=========================="
        print "Found Packet: 7e%s" % data
        print "Packet Type = ZgBee RX Packet"
        AH = data [10:18]
        AL = data [18:26]
        print "Device Address = ", AH, AL
        TH = data [34:36]
        TL = data [38:40]
        THc = int(TH, 16)
        TLc = int(TL, 16)
        print "Temperature: %i.%i" % (THc, TLc)
        print "======================="
        print " "

You have to capture the return from split() and iterate over each one. Using split eliminates the split characters so you can prepend them back or adjust the slice numbers by -2 as I have done below.

test_data=\
"7e001090007d33a200408b2e0c2db70100160057637e001090007d33a2004089e04a569d010016003885"

if test_data[0:2] == '7e':
    data_split=test_data.split('7e')
    print data_split
    for Data_in in data_split:
        print "=========================="
        print "Found Packet: " + Data_in

        begin=4
        if Data_in[begin:begin+2] == '90' and
           len(Data_in) > 36:
            print "Packet Type = ZgBee RX Packet"
            AH = Data_in [begin+6:begin+14]
            AL = Data_in [begin+14:begin+22]
            print "Device Address = ", AH, AL
            TH = Data_in [begin+30:begin+32]
            TL = Data_in [begin+34:begin+36]
            THc = int(TH, 16)
            TLc = int(TL, 16)
            print "Temperature: %d.%d" % (THc, TLc)
            print "=======================\n"

Thanks both!
Each seem to be working perfectly so I will play some more and see what happens.

As I have the thread open - I have one other semi related question. Once the info has been printed it will be written to a MySQL DB. I know thats not the fastest process ever so should I be worried about data loss while thats going on? Data is sent from the controller at 3 second intervals
If so would it be an idea to write it to a list/array or file and write to the DB from there?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.