1

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?

sosnus
  • 968
  • 10
  • 28
  • 3
    When bytes correspond to ascii characters it prints them as such. `ord('y')` -> `121` and `chr(121)` -> `y` – Mark Jul 14 '23 at 03:58
  • Thank You @Mark ! I extend my question, maybe You know answer ;) – sosnus Jul 14 '23 at 04:06
  • 1
    You seem to be confused by the representation. The `y` is just how Python prints the bytearray items by default. The actual data contains a `0x79` byte. – Klaus D. Jul 14 '23 at 04:16
  • 1
    You can always print `list(msg)` or `msg.hex(" ").upper()` if that's more conveniently readable to you. As for the unexpected response, one can only guess. Maybe there are initial bytes in the RX stream that you should clear before sending the first msg? – Jeronimo Jul 14 '23 at 05:11

0 Answers0