Bottom Page

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
Quote
#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.
Feel like you're not getting the answers you want? Checkout the help/rules for things like what to include/not include in a post, how to use code tags, how to ask smart questions, and more.

Pro-tip - there's an inverse correlation between the number of lines of code posted and my enthusiasm for helping with a question :)
Quote
#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~"
Quote
#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.
Feel like you're not getting the answers you want? Checkout the help/rules for things like what to include/not include in a post, how to use code tags, how to ask smart questions, and more.

Pro-tip - there's an inverse correlation between the number of lines of code posted and my enthusiasm for helping with a question :)
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  'utf-8' codec can't decode byte 0xda in position 184: invalid continuation byte karkas 7 284 Sep-12-2019, 11:19 PM
Last Post: newbieAuggie2019
  Byte array is sorted when sending via USB daviddlc68 1 113 Aug-16-2019, 10:11 AM
Last Post: wavic
  Reading data from serial port as byte array vlad93 1 1,018 May-18-2019, 05:26 AM
Last Post: heiner55
  charmap codec can't decode byte error with gzipped file in python bluethundr 2 279 Apr-30-2019, 12:26 PM
Last Post: bluethundr
  Not able to Read byte Array From Wireless Coordinator jenkins43 0 231 Mar-18-2019, 07:05 AM
Last Post: jenkins43
  Read data of Wireless Modem in Byte Format barry76 2 335 Jan-31-2019, 11:26 AM
Last Post: barry76
  Byte-Array okmog 3 947 Jun-13-2018, 03:19 PM
Last Post: DeaD_EyE
  How can Read text file and create array? puneet102 1 564 May-22-2018, 06:41 PM
Last Post: snippsat
  4 byte hex byte swap from binary file medievil 7 6,144 May-08-2018, 08:16 AM
Last Post: killerrex
  Read an array of structures from a c file using python Alisha 1 581 Apr-30-2018, 07:41 AM
Last Post: Gribouillis

Forum Jump:


Users browsing this thread: 1 Guest(s)