What is Serial Data? - GMU-ASRC/IR-Sensor-Characterization GitHub Wiki
What is Serial Data?
Serial Data follows the UART protocol. It is a stream of binary data that enters your computer one at a time. Think of a series network of resistors sequentially stacked one by one all containing some information.
Serial Parameters
Each serial connection from one device to another must have the correct parameters for proper communication:
- COM Port
- Baud Rate
- Data Bits
- Stop Bit
- Parity Bit
- Flow Control
Note: The stop bit is usually set to
1
, and flow control is typically set toNone
.
UART Communication
As stated before, UART is a communication protocol used to sort information throughout a digital system
- COM port is the "identifier" of your device, it will show up when you plug it in
- The number of bits in each sequence is defined by the data bits.
- A stop bit marks the end of each data sequence.
- The baud rate defines the speed of communication.
Within each sequence of data bits can be a number of information types such as numbers, words, coordinates etc.
To learn more: Search "UART communication protocol".
What does this have to do with my device?
Devices like sensors and encoders use this protocol to communicate to your processor what it's doing in real time in order to feed live information like position, distance, state, etc.
How do I access my device's information?
In order to find the settings of your device, go into the Device Manager by pressing Windows Key + x or right clicking on the windows icon.
- Find the PORTS tab
- Plug in your device
- A new device with a different COM port should show up
- Click on it and navigate to port settings
- You can access anything needed here such as Baud rate, stop and parity bit, and change them accordingly
This is what you should see if the device is connected to your computer properly, if not try looking up the drivers associated with your PORTS
Port settings of the device connected to your computer
How do I access serial information from a device?
A Serial Terminal is a great place to read incoming data from a device like a radio where the information needed is to be read, or encoded information that needs to be decoded for further understanding by humans.
Devices like mice and microphones tend to be only binary, as their information is only needed by the computer
)
Using PuTTY
PuTTY is a popular terminal interface for accessing serial data.
Refer to the PuTTY documentation in this project's GitHub repository.
Reading Serial Data with Python
Install the pyserial
library to interact with serial data:
pip install pyserial
Example Script Structure
Start by importing and setting up the serial object:
import serial
# Define serial parameters
COM_PORT = 'COM3' # Replace with your port
BAUD_RATE = 9600 # Replace with your baud rate
ser = serial.Serial(COM_PORT, BAUD_RATE)
Make sure the above values match those found in Device Manager.
Parsing Each Line
Create a function to decode each line:
def read_serial_data(serial_obj):
while True:
line = serial_obj.readline()
decoded_line = line.decode('utf-8').strip()
print(decoded_line)
This function:
- Runs indefinitely using a
while
loop - Reads each incoming line
- Decodes it to a human-readable string
Once you’ve extracted the string, you can process it like any other string in Python.