0

I am working on a Python application that sends a binary stream over serial to a microcontroller. My initial use case is simple - send 4 integers which represent the binary values to send over the serial port. I need to translate the array into a byte array or a stream of bits or bytes. Using bytearray works great unless one of my integers is > 256 (more than 1 byte).

Here is the code and the first 3 elements of the buffer list are 1 byte integers (<256). But the last element (dataCRC) is represented by a 14-bit integer (8444 in my case).

dataCRC = 8444
buffer = [128, 32, length]   
buffer.append(dataCRC)

barray = bytearray(buffer)

This raises the error that each byte must be in the range (0,256). How do I break up my last buffer element (the 14 bit number) into 2 bytes?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • 1
    Which two bytes do you expect to be appended to the buffer for `dataCRC = 8444`? – mkrieger1 Dec 19 '22 at 01:50
  • Does this answer your question? [Converting int to bytes in Python 3](https://stackoverflow.com/questions/21017698/converting-int-to-bytes-in-python-3), or better: https://stackoverflow.com/questions/846038/convert-a-python-int-into-a-big-endian-string-of-bytes – mkrieger1 Dec 19 '22 at 01:56

0 Answers0