Python Forum
Read Byte Array from Zigbee coordinator using python - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Read Byte Array from Zigbee coordinator using python (/thread-17024.html)



Read Byte Array from Zigbee coordinator using python - jenkins43 - Mar-25-2019

Hey, to interface Zigbee coordinator with python, I have used the mentioned below program. I was able to read the Byte array
import serial
ser = serial.Serial('COM6',115200)
read_byte = ser.read()
while read_byte is not None:
    read_byte = ser.read()    
    print ('%x' % ord(read_byte))
Result in hexadecimal format:

7E 00 32 90 00 13 A2 00 41 91 25 D4 FF FE C2 7F 00 01 03 FF BC 00 08 00 01 52 55 00 BE 6B 01 89 DC 00 00 00 00 CB 4D 00 00 00 FE A4 04 00 00 00 FE 58 3E 00 1B 46
but when I am trying to read the data from the byte array then I am facing some problem using mentioned below code.

import time
import serial

# Change this serial port to the serial port you are using.
s = serial.Serial('COM6', 115200)

while True:
    packet_ready = s.read(1)
    if(ord(packet_ready) == 126):
        while s.inWaiting() < 28:
            time.sleep(.001)
        bytes_back = s.read(28)
        if(ord(bytes_back[15]) == 127):
            print('The packet has a data payload: ' + str(ord(bytes_back[15])))
            print('The packet is for sensor type: '+str(ord(bytes_back[22])))
Error:

Error:
Traceback (most recent call last): File "C:/Users/Misha/Desktop/test/Xbee Test/Test2.py", line 13, in <module> if(ord(bytes_back[15]) == 127): TypeError: ord() expected string of length 1, but int found
and I am not able to understand the error problem
please suggest the best way to make it work so that I'll be able to read data from Byte Array


RE: Read Byte Array from Zigbee coordinator using python - micseydel - Mar-25-2019

That's pretty weird. Could you print bytes_back and its type? If we can isolate an example result from the serial read() operation, then you'll have code that forum members can run and play with to try to solve the problem.


RE: Read Byte Array from Zigbee coordinator using python - jenkins43 - Mar-26-2019

Hi, thanks for the reply so as per the mentioned below code I have run the test

import time
import serial

# Change this serial port to the serial port you are using.
s = serial.Serial('COM6', 115200)

while True:
    packet_ready = s.read(1)
    if(ord(packet_ready) == 126):
        while s.inWaiting() < 54:
            time.sleep(.001)
        bytes_back = s.read(54)
        print(bytes_back)
        if(ord(packet_ready) == 127):
            print('The packet has a data payload: ' + (ord(packet_ready[1])))
            print('The packet is for sensor type: '+ (ord(bytes_back[22])))
and was able to get the result as follows

b"\x002\x90\x00\x13\xa2\x00A\x91%\xd4\xff\xfe\xc2\x7f\x00\x01\x03\xffg\x00\x08\x00\x00~\x1d\x00\x17\xca\x00'g\x00\x85?\x003\xc1\x005/\x00\x00\x00\xff\xa2\x80\x00\x00\x00\x00\x1a\xde~"



RE: Read Byte Array from Zigbee coordinator using python - micseydel - Mar-26-2019

Well we don't necessarily have serial devices to test this. If you can re-frame your question with a hard-coded value that reproduces the issue without serial, that would be ideal.

Without digging in more, my only comment at this point is that something is off with your logic, you check the value of packet_ready and then inside that if-block you check it for a different value. That doesn't really make sense. It doesn't look like your newest code reproduces your original problem, you should make sure to include (1) your latest code and (2) the exact thing wrong with that code, including the exact stack trace associated with it.