Check for valid bytes

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
newb
Posts: 43
Joined: Wed Jan 10, 2018 8:19 pm
Location: Bulgaria

Check for valid bytes

Post by newb » Tue Apr 07, 2020 8:53 am

Hi, I've got a general question. I'm reading a serial sensor which is suppose to produce a data frame of 4 bytes(always starting with 0xff and the last 4th byte being the checksum). I've got problems with detecting a valid data frame because some of the bytes are just garbage. My question is how do I check for valid bytes?
Here's an example of the output:

Code: Select all

b'\xff\x08\x9e\xa5'                                                                                                                                   
b'\xff\x08\xc1\xc8'                                                                                                                                   
b'\xff\x08\xb0\xb7'                                                                                                                                   
b'\xff\x08\x9e\xa5'                                                                                                                                   
b'\xff\x08\xc1\xc8'                                                                                                                                   
b'\xff\x08|\x83'                                                                                                                                      
b'\xff\x08kr'                                                                                                                                         
b'\xff\x08\xc1\xc8'                                                                                                                                   
b'\xff\x08kr'                                                                                                                                         
b'\xff\x08\x9e\xa5'                                                                                                                                   
b'\xff\x04\xfb\xfe'                                                                                                                                   
b'\xff\x06\x0f\x14'                                                                                                                                   
b'\xff\x07\xd0\xd6' 

fstengel
Posts: 55
Joined: Tue Apr 17, 2018 4:37 pm

Re: Check for valid bytes

Post by fstengel » Tue Apr 07, 2020 9:27 am

What do you mean by garbage? The sample output you give is composed of 13 bytearrays of length 4. When printing them (micro)python only escapes the bytes which do not have ans ascii representation.

newb
Posts: 43
Joined: Wed Jan 10, 2018 8:19 pm
Location: Bulgaria

Re: Check for valid bytes

Post by newb » Tue Apr 07, 2020 9:36 am

I thought every byte should start with \x. Do you mean that e.g. this bytearray - b'\xff\x08kr' has valid 4 bytes in it? If that's the case, how do I separate each individual byte for further processing?

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Check for valid bytes

Post by jimmo » Tue Apr 07, 2020 10:26 am

When (Micro)Python prints a bytearray, any printable ascii character is printed normally. Only the ones outside this range are printed as their hex representation \xXX

So in your list for example, b'\xff\x08kr' is 0xff 0x08 0x6b 0x72 (the last two just happen to be 'k' and 'r')

Code: Select all

>>> b = b'\xff\x08kr'
>>> hex(b[0])
'0xff'
>>> hex(b[1])
'0x8'
>>> hex(b[2])
'0x6b'
>>> hex(b[3])
'0x72'

newb
Posts: 43
Joined: Wed Jan 10, 2018 8:19 pm
Location: Bulgaria

Re: Check for valid bytes

Post by newb » Tue Apr 07, 2020 11:11 am

Thank you, guys!
I'm now getting somewhere by dividing individual bytes with hex() and parsing the dataframe :D

P@T
Posts: 33
Joined: Tue Nov 06, 2018 2:37 pm

Re: Check for valid bytes

Post by P@T » Tue Apr 07, 2020 11:22 am

Hi,

You can to use

Code: Select all

import struct
struct.unpack('BBBB',b'\xff\x08kr')
>>>(255, 8, 107, 114)
'B' = unsigned char see struct in python documentation

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Check for valid bytes

Post by jimmo » Tue Apr 07, 2020 1:48 pm

newb wrote:
Tue Apr 07, 2020 11:11 am
Thank you, guys!
I'm now getting somewhere by dividing individual bytes with hex() and parsing the dataframe :D
I'm not quite sure why you cant just access the bytes directly?

i.e.

Code: Select all

if frame[0] == 0xff:
  data1 = frame[1]
  data2 = frame[2]
  if calc_checksum(data1, data2) == frame[3]:
    # do something with data

newb
Posts: 43
Joined: Wed Jan 10, 2018 8:19 pm
Location: Bulgaria

Re: Check for valid bytes

Post by newb » Tue Apr 07, 2020 4:11 pm

May be I've taken wrong approach?
Because when I plug the esp8266 I don't know what byte will be first caught by the UART, I'm reading one byte from UART until that byte is the data frame start (0xff), then I read the immediate next 3 other bytes. That's why I can't access all bytes as the same time from one bytearray, as you suggest.

Code: Select all

while True:
    if uart.any():
    	start=uart.read(1)
    	if start == b'\xff':
            data=uart.read(3)
            #do stuff with the other 3 bytes of the data frame

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Check for valid bytes

Post by jimmo » Tue Apr 07, 2020 11:29 pm

Sure, I was just confused what you meant by "dividing individual bytes with hex()"

Post Reply