2

I am using Xbee API library for Python to process the data received by the Xbee module. I am wondering how to construct the robust protocol for sending and receiving data. What I would like to build is

  1. send the data request message to power monitoring uint (Xbee in remote).
  2. Receive the transmitted power monitoring data and process it in Python.

If the aforementioned process works in just one time, it would be great, but sometimes it does not work. Thus, I would like to code autonomous acknowledging procedure into this communication. That is

  1. send the data request message to power monitoring uint (Xbee in remote).
  2. If the dat is not received, go back to 1.
  3. If the data is received ok.

Here are my code, but it does not work all the time. Would you please look at how can I improve the code further to make it this

def listeningAndClusterData(targetWT):

#targetWT-->this will be used to construct the target address
targetAddress = struct.pack('b',0x00)+struct.pack('b',targetWT)
Data = struct.pack('h',0x0000) #prefix for requesting information
xbee.tx(dest_addr=targetAddress, data=Data)
ser.flush()
time.sleep(0.01)
response = xbee.wait_read_frame(100)#wait for data packet from Xbee, if packet is not recieved within 100ms, return 1

watingData = 1;
while watingData:
    try:

        if response != 1: # if the data is received
            sa = hex(response['source_addr'])
            rf = hex(response['rf_data'])
            datalength=len(rf)
            agentId = int(sa[2:4],24)
            # if datalength is compatible with two floats
            if datalength==16: 
                voltage=struct.unpack('f',response['rf_data'][0:4])[0]
                current=struct.unpack('f',response['rf_data'][4:8])[0]
                power=voltage*current
                watingData = 0 # I received the average data and escape the loop
                ser.flush()
            else: #data is received but the format is not correct
                ser.flush()

        if watingData != 0: #if the data is not received--> keep sending the request message to other xbee
            xbee.tx(dest_addr=targetAddress, data=Data)
            print("data is requested again")
            time.sleep(0.01)
            ser.flush()
            response = xbee.wait_read_frame(100)

    except KeyboardInterrupt:
        break

return  voltage, current, power
Jinkyoo Park
  • 81
  • 1
  • 5
  • Why do you flush before you read a frame? Is it not possible to have received the frame before you flushed? Also how does it fail, is it an error is there sometimes no response from the xbee? – Treesrule14 Feb 10 '15 at 16:17
  • I thought ser.flush() will clear the buffer containing the message sent my xbee.tx(dest_addr=targetAddress, data=Data).I couldn't yet figure it out why the error occurs. Do you have any idea how to wisely use sleep() and ser.flush()? – Jinkyoo Park Feb 10 '15 at 18:35
  • You can just use xbee.wait_read_frame. The protocol is very fast – Treesrule14 Feb 16 '15 at 16:54

0 Answers0