Python Forum
Read Byte Array from Zigbee coordinator using python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Read Byte Array from Zigbee coordinator using python
#1
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
Reply
#2
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.
Reply
#3
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~"
Reply
#4
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  extract only text strip byte array Pir8Radio 7 2,790 Nov-29-2022, 10:24 PM
Last Post: Pir8Radio
  Indexing [::-1] to Reverse ALL 2D Array Rows, ALL 3D, 4D Array Columns & Rows Python Jeremy7 8 6,963 Mar-02-2021, 01:54 AM
Last Post: Jeremy7
  'utf-8' codec can't decode byte 0xe2 in position 122031: invalid continuation byte tienttt 12 11,356 Sep-18-2020, 10:10 PM
Last Post: tienttt
  convert array of numbers to byte array adetheheat 3 2,731 Aug-13-2020, 05:09 PM
Last Post: bowlofred
  Convert even byte array to odd medatib531 1 2,174 Mar-17-2020, 02:48 AM
Last Post: scidam
  Python crypto byte plaintext to hexint Mangaz90 2 2,807 Feb-21-2020, 07:42 PM
Last Post: Mangaz90
  'utf-8' codec can't decode byte 0xda in position 184: invalid continuation byte karkas 8 31,479 Feb-08-2020, 06:58 PM
Last Post: karkas
  How to read a text file into a list or an array musouka 2 2,800 Oct-07-2019, 01:54 PM
Last Post: musouka
  Byte array is sorted when sending via USB daviddlc68 1 2,779 Aug-16-2019, 10:11 AM
Last Post: wavic
  Reading data from serial port as byte array vlad93 1 11,981 May-18-2019, 05:26 AM
Last Post: heiner55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020