Overview
TheCommunicationManager class provides bidirectional serial communication between the Raspberry Pi controller and the VEX Brain. It implements a JSON-based messaging protocol with threaded message reading and automatic message parsing.
Source: ~/workspace/source/examples/serial_com.py:11
This is the Raspberry Pi implementation. The VEX Brain uses a simplified version without threading.
Constructor
__init__(port: str = 'COM7', baudrate: int = 115200)
Initializes the communication manager with serial port configuration.
Serial port identifier. Common values:
- Windows:
'COM3','COM7', etc. - Linux:
'/dev/ttyUSB0','/dev/ttyACM0' - macOS:
'/dev/cu.usbserial-*'
Communication speed in bits per second. Must match VEX Brain configuration.
Standard values:
9600, 19200, 38400, 57600, 115200message_end: Line terminator (b'\n')serial_port: PySerial Serial objectis_connected: Connection state flag_read_thread: Background thread for receiving messagesbuffer: Byte buffer for incomplete messages_stop_event: Threading event for graceful shutdown
serial_com.py:12
Connection Methods
connect()
Establishes serial connection and starts background message reading thread.
Returns
True if connection succeeds, False on error- Checks if already connected (idempotent)
- Opens serial port with PySerial
- Configures timeouts (10 seconds for read/write)
- Starts daemon thread for continuous message reading
- Sets
is_connectedflag
- Daemon thread (exits when main program exits)
- Runs
_read_loop()method continuously - Automatically processes incoming messages
- Logs connection errors
- Returns
Falsewithout raising exceptions - Connection state remains
False
serial_com.py:24
Messaging Methods
send_message(message_type: str, data: dict)
Sends a JSON-formatted message to the VEX Brain.
Message type identifier. Valid types:
'check_service': Hardware verification request'safety_service': Safety sequence initiation'scan_service': Environmental scan request
Message payload dictionary. Can be empty
{} or contain command-specific data.Returns
True if message sent successfully, False on error\n).
Example Messages:
serial_com.py:49
Thread Management
_read_loop() (Internal)
Background thread method that continuously reads and processes incoming serial data.
- Checks for available data in serial buffer
- Reads one byte at a time
- Accumulates bytes in buffer until newline
- Decodes complete message as JSON
- Calls
_process_message()for handling - Sleeps 10ms between iterations
- Catches JSON decode errors
- Logs malformed messages
- Continues operation on errors
- Stops when
_stop_eventis set
serial_com.py:72
_process_message(message: dict) (Internal)
Parses and handles incoming messages from VEX Brain.
serial_com.py:92
Connection Cleanup
close()
Gracefully closes the serial connection and stops background threads.
- Sets stop event to terminate read thread
- Waits up to 1 second for thread to finish
- Closes serial port
- Updates connection state
- Logs closure confirmation
- Call in
finallyblock for guaranteed cleanup - Safe to call multiple times
- Prevents resource leaks
serial_com.py:127
Message Protocol Specification
Message Structure
All messages follow a consistent JSON structure:- JSON format (UTF-8)
- Newline terminated (
\n) - No additional framing or checksums
- Practical limit: ~1KB per message
- VEX Brain buffer constraints
- Large object arrays may be split
Command Messages (Raspberry Pi → VEX)
Request hardware verification
Initiate safety check sequence
Start environmental scan
Response Messages (VEX → Raspberry Pi)
All responses includestate in the data field:
"approved": Operation completed successfully"complete": Multi-step operation finished"in_progress": Operation ongoing"error": Operation failed
Complete Usage Example
VEX Brain Implementation
The VEX Brain uses a simplified synchronous version:- No threading (VEX OS limitations)
- Synchronous
read_message()called in main loop - Opens
/dev/serial1directly - No PySerial dependency
Related Classes
- RoboticServices - Main service orchestrator that processes messages
- SensorModule - Hardware sensors that generate scan data
- ControlModule - Motor controls triggered by commands