Serial Communication Protocol
Serial communication is the backbone of communication between the Raspberry Pi (vision processing) and VEX Brain (robot control). In this lesson, you’ll learn how to establish reliable bidirectional communication.Learning Objectives
By the end of this lesson, you will be able to:- Configure serial ports on both Raspberry Pi and VEX Brain
- Implement basic read and write operations
- Handle connection errors and timeouts
- Design a message framing protocol
- Debug serial communication issues
All code examples in this lesson are from the actual course implementation in
course/comm_class/raspberry_comm/Serial Communication Basics
Hardware Setup
Raspberry Pi Side:- USB Serial Adapter (Windows:
COM7, Linux:/dev/ttyUSB0) - Or direct GPIO UART pins:
/dev/ttyAMA0
- Built-in serial port:
/dev/serial1 - Accessible from VEX Python API
Critical: Connect TX to RX and RX to TX (crossover). Also ensure both devices share a common ground (GND).
Communication Parameters
Both devices must agree on:- Baud Rate: 115200 bits/second (fast enough for real-time control)
- Data Bits: 8
- Parity: None
- Stop Bits: 1
- Flow Control: None
Basic Serial Operations
Writing Data (Transmit)
Let’s start with a simple example of sending data from Raspberry Pi to VEX Brain. Raspberry Pi Code (write_data.py:1-16):
serial.Serial()opens the port immediatelywrite()requires bytes, so weencode()stringswrite_timeout=10prevents indefinite blocking
vex_brain_comm/src/main.py:18-27):
- VEX uses file operations:
open()instead of library - Binary mode (
'rb') for raw byte access read(1)blocks until one byte is available
Reading Data (Receive)
Raspberry Pi Code (read_data.py:1-17):
vex_brain_comm/src/main.py:28-37):
These basic examples read/write single bytes. In practice, you’ll send complete messages, which requires a framing protocol.
Message Framing Protocol
The Challenge
Serial communication is a continuous stream of bytes. How do you know where one message ends and the next begins? Problem Example:Solution: Newline Delimiter
We use the newline character (\n) as a message delimiter:
\n, making boundaries clear.
Implementation: Buffered Reading
Instead of processing bytes immediately, accumulate them in a buffer until we receive a complete message. Fromjson_data.py:18-20:
json_data.py:50-68):
- Check for data:
com.in_waitingreturns number of bytes ready - Read one byte: Process character by character
- Check for delimiter: Is it
\n?- Yes: Decode buffer as complete message → process it → reset buffer
- No: Append byte to buffer → continue reading
- Error handling: Catch decode errors (malformed messages)
VEX Brain Equivalent
Fromvex_brain_comm/src/main.py:39-69:
Connection Management
Establishing Connection
Fromjson_data.py:21-39:
- Check if already connected (avoid duplicate connections)
- Use threading for non-blocking reads
daemon=True: Background thread won’t prevent program exit- Return
boolto indicate success/failure
Why Threading?Reading from serial is blocking —
read() waits until data arrives. Without threading, your main program would freeze. A background thread monitors the serial port continuously while your main code processes vision or handles other tasks.Graceful Shutdown
Fromjson_data.py:83-91:
- Set stop event (thread checks
_stop_event.is_set()in loop) - Wait for thread to finish with
join(timeout=1.0) - Close serial port
- Update connection status
Error Handling
Common Issues
Port Not Available:Robust error handling is critical. Don’t let a single malformed message crash your entire robot control system!
Debugging Serial Communication
Testing Tools
-
Serial Monitor (Arduino IDE, PuTTY, screen)
-
Loopback Test: Connect TX to RX on same device
- Should receive everything you send
- Confirms port works
-
Logging: Add verbose output
Common Problems
| Issue | Symptom | Solution |
|---|---|---|
| Baud rate mismatch | Garbled characters | Ensure both sides use 115200 |
| TX/RX swapped | No data received | Swap TX and RX connections |
| Missing ground | Intermittent errors | Connect GND pins |
| Port in use | ”Port busy” error | Close other programs using port |
Practice Exercise
Echo Server
Implement a simple echo protocol: Raspberry Pi:- Send a message to VEX Brain
- Wait for echo response
- Verify received message matches sent message
- Read incoming message
- Send it back immediately
- Messages transmitted without corruption
- Round-trip time < 100ms
- Handles 10 messages/second reliably
Extension Challenge
Add a checksum to detect transmission errors:Summary
You’ve learned:- ✓ Serial port configuration and basic read/write
- ✓ Message framing using newline delimiters
- ✓ Buffered reading for complete message extraction
- ✓ Threaded communication for non-blocking operation
- ✓ Connection management and error handling
- ✓ Debugging techniques for serial issues
Next Steps
Now that you can send and receive raw messages, the next lesson covers structured communication using JSON for type-safe robot commands.JSON Messaging
Learn to structure robot commands with JSON message formats
Course Repository: All code examples are in
course/comm_class/raspberry_comm/write_data.py: Basic transmit exampleread_data.py: Basic receive examplejson_data.py: Complete implementation with framing