2

I have a sensor system. The sensors receive commands from me, do something and then send a response to me.

The response is like this:

seq. number |   net_id  |   opcode_group |  opcode | payloadlength    |   val

where I have these values delimited by a space character.

Now I want to take the last value named val. In this part, I have all the information I want to know to elaborate the response from the sensors.

For example, I have this response for the command that wants to know the IEEE MAC address of the sensor:

response

In this case val is all the fields after Length in the response. There are not separation, but I have a sort of string.

All I have to do is to split this array/string of numbers, just knowing only the length of every field. For ex. the status is 1 byte, the MAC address 8 byte, and so on...

My code is this:

if response.error:
        ret['error'] = 'Error while retrieving unregistered sensors'
    else:
        for line in response.body.split("\n"):
            if line != "": 
                value = int(line.split(" ")[6])

    ret['response'] = value
    self.write(tornado.escape.json_encode(ret))
    self.finish()               

    if command == 'IDENTIFY':
        status = value.split(" ")[0]
        IEEEAddrRemoteDev = value.split(" ")[1]
        NWKAddrRemoteDev = value.split(" ")[2]
        NumOfAssociatedDevice = value.split(" ")[3]
        StartIndex = value.split(" ")[4]
        ListOfShortAddress = value.split(" ")[5]
        if status == 0x00:
            ret['success'] = "The %s command has been succesfully sent! \
            IEEE address: %s" % % (command.upper(), IEEEAddrRemoteDev)
            self.write(tornado.escape.json_encode(ret))
        elif status == 0x80:
            ret['success'] = "Invalid Request Type"
            self.write(tornado.escape.json_encode(ret))
        elif status == 0x81:
            ret['success'] = "Device Not Found"
            self.write(tornado.escape.json_encode(ret))

where in the first part I take the 6th value from the entire response and I put this in the variable value. After this I want to split this variable in every component.

For ex. this status = value.split(" ")[0] in which way I have to split????

Thank you very much for the help!

Community
  • 1
  • 1
sharkbait
  • 2,980
  • 16
  • 51
  • 89

2 Answers2

0

What is the exact format of the val field (i.e. the contents of value in your code)? Is it a string? A sequence of bytes?

If it's a string, you could use:

   status = value[0]
   IEEEAddrRemoteDev = value[1:8]
   NWKAddrRemoteDev = value[9:2]
   NumOfAssociatedDevice = value[11:1]
   StartIndex = value[12:1]
   ListOfShortAddress = value[13:2*NumOfAssociatedDevice]

If it's a sequence of bytes, then you could use the struct.unpack() - see here.

isedev
  • 18,848
  • 3
  • 60
  • 59
  • I think it's a string. I try your solution thank you very much! – sharkbait Jan 28 '13 at 18:00
  • Correct me if I'm wrong, but the second index in [Python's slicing notation](http://stackoverflow.com/a/509295/321973) is not the length but the (excluded!) end index, so it should be code like `NWKAddrRemoteDev = value[9:(9+2)+1]` (or `[9:11]`) – Tobias Kienzler Dec 06 '13 at 13:51
0

The problem is that you're treating this as a Python string, but it's just going to be a bunch of bits.

You need to use struct.unpack to split these up.

tom10
  • 67,082
  • 10
  • 127
  • 137