Bytes and struct
A sensor doesn't send numbers; it sends bytes. Python's struct module turns a frame of raw bytes into named values in one line, as long as you tell it the layout and the byte order.
From five bytes to four values
Our sensor sends five bytes: a start byte 0xAA, a signed 16-bit temperature in hundredths of a degree, a status byte and a checksum. The format string "<BhBB" describes exactly that: B is an unsigned byte and h a signed 16-bit integer.
import structframe = bytes([0xAA, 0x29, 0x09, 0x01, 0xDD])print(frame.hex(" "))start, raw, status, check = struct.unpack("<BhBB", frame)ok = sum(frame[:-1]) & 0xFF == checkprint(f"start=0x{start:02X} temp={raw / 100:.2f} C status={status} ok={ok}")
aa 29 09 01 dd start=0xAA temp=23.45 C status=1 ok=True
Byte order matters
Most microcontrollers, Arm Cortex-M parts among them, are used little-endian: low byte first. But the frame's byte order is set by the sensor's protocol, so check its datasheet. Read the same two bytes big-endian (">h") and 23.45 °C becomes 105.05 °C.
It works in reverse too: struct.pack("<BhBB", 0xAA, 2345, 1, 0xDD) builds the same five bytes, which is how a test script sends a command or fakes a sensor. A bytes object is read-only; when you fill a buffer piece by piece, use a bytearray.
On the bench the bytes come from a serial port. With the pyserial package, installed with pip install pyserial, reading them is one more line:
import serial # the pyserial packagewith serial.Serial("COM3", 115200, timeout=1) as port: frame = port.read(5) # the same five bytes, from the board