Page 1 of 1

Check for valid bytes

Posted: Tue Apr 07, 2020 8:53 am
by newb
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' 

Re: Check for valid bytes

Posted: Tue Apr 07, 2020 9:27 am
by fstengel
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.

Re: Check for valid bytes

Posted: Tue Apr 07, 2020 9:36 am
by newb
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?

Re: Check for valid bytes

Posted: Tue Apr 07, 2020 10:26 am
by jimmo
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'

Re: Check for valid bytes

Posted: Tue Apr 07, 2020 11:11 am
by newb
Thank you, guys!
I'm now getting somewhere by dividing individual bytes with hex() and parsing the dataframe :D

Re: Check for valid bytes

Posted: Tue Apr 07, 2020 11:22 am
by P@T
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

Re: Check for valid bytes

Posted: Tue Apr 07, 2020 1:48 pm
by jimmo
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

Re: Check for valid bytes

Posted: Tue Apr 07, 2020 4:11 pm
by newb
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

Re: Check for valid bytes

Posted: Tue Apr 07, 2020 11:29 pm
by jimmo
Sure, I was just confused what you meant by "dividing individual bytes with hex()"