UART Sensor Packet Parser
You are receiving sensor data from an embedded device over UART. Each packet follows a fixed binary protocol and is received as a Python bytes object.
Packet format (5 bytes total): - Byte 0 : Header (0xAA) - Byte 1 : Sensor ID - Byte 2 : Sensor Value (0-255) - Byte 3 : Checksum = (Sensor ID + Sensor Value) & 0xFF - Byte 4 : Footer (0x55)
Write a Python function that: 1) Validates the packet format 2) Verifies the checksum 3) Returns the parsed data as a dictionary
If the packet is invalid, return None.
This simulates packet parsing logic commonly used in embedded systems, firmware validation, and hardware test automation.
1def parse_uart_packet(packet_str):2 """3 packet_str: string containing hex bytes separated by spaces4 return: 'sensor_id value' or 'INVALID'5 """6 # TODO: implement parsing logic7 pass