Python decode bytearray

63 Views Asked by At

I need send some data over serialport, but I have problem with bytearray I wrote simple script, like this:

msg = bytearray([255,1,134,0,0,0,0,121])
print(msg)

But output from this looks like this:

python3 bytearrtest.py
bytearray(b'\xff\x01\x86\x00\x00\x00\x00y')

What is it this y? Why output do not have 0x79 at the end instead of y? And why 4 times Zero is converted to two times 0x00 ?

Context of question: I wrote very simple code in Micropython:

from machine import UART, Pin
import time

uart1 = UART(1, baudrate=9600, tx=Pin(8), rx=Pin(9),bits=8, parity=None, stop=1)

msg = b"\xff\x01\x86\x00\x00\x00\x00\x00\x79"

print(msg)
uart1.write(msg)  # write

time.sleep_ms(2000)
print("Response:")
print(uart1.read())

And response of this code is:

b'\xff\x01\x86\x00\x00\x00\x00\x00y'
Response:
b'\x00\x01\x86\x00\x00\x00\x00\x00y\x00'

Last line of output come from another device. But If I do it in terminal BRAY, on command: FF 01 86 00 00 00 00 79 response is: FF 86 02 55 3E 00 02 00 E3 and this is ok, because response must staart from FF 86...

So, why it works in console, but not in MicroPython?

0

There are 0 best solutions below