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?