Introduction to Serial Communication
Serial communication is a cornerstone of interfacing computers with peripheral devices, such as microcontrollers or modems. Unlike parallel communication, which uses multiple channels simultaneously, serial communication transmits data sequentially over a single channel or wire. This simplicity makes it ideal for long-distance and low-speed applications. In Python, the pyserial
library provides an interface to the serial port, allowing you to read from and write to these devices with ease.
Understanding PySerial
PySerial is a set of Python classes that encapsulate the access for the serial port, offering an easy-to-use API for interacting with connected hardware. It supports various platforms including Windows, macOS, and Linux. To begin using pySerial, you must first install it via pip:
pip install pyserial
Setting Up a Serial Connection
Establishing a serial connection involves configuring the communication parameters such as baud rate, parity bits, stop bits, and byte size. These settings must match those expected by the device.
Here’s an example of how to set up a serial connection in Python using pySerial:
import serial
# Define the port configuration for the connected device.
ser = serial.Serial(
port='/dev/ttyUSB0', # Serial port name (Linux/Mac) or COMx (Windows)
baudrate=9600, # Communication speed in bits per second
parity=serial.PARITY_NONE, # Parity bit setting: None, Even, Odd
stopbits=serial.STOPBITS_ONE,# Number of stop bits: One or Two
bytesize=serial.EIGHTBITS # Data size (5, 6, 7, or 8 bits)
)
# Open the serial connection if not already open.
if ser.isOpen():
print("Serial port is already open.")
else:
ser.open()
Sending AT Commands
AT commands are instructions used to control modems. To send an AT command, you write a string to the serial port and possibly wait for a response.
# Send the 'AT' command followed by carriage return and line feed.
ser.write(b"AT\r\n")
# Wait briefly to allow the device time to respond.
import time
time.sleep(0.5)
# Read the response from the device.
response = ser.readline()
print("Response:", response.decode().strip())
Note that you might need to adjust time.sleep()
duration depending on how quickly your device processes commands.
Reading Responses
To read a response from the serial device, check if there’s incoming data and then read it. Use non-blocking or blocking reads based on your requirements:
# Non-blocking read with a timeout.
ser.timeout = 1 # Set to None for infinite blocking read.
response = ser.readline()
if response:
print("Received:", response.decode().strip())
else:
print("No data received.")
Handling AT Command Responses
AT command responses may vary in format, and some commands might require a "silent period" before they are sent or after a response is expected. This means you should sometimes insert delays to ensure the device is ready for communication.
# Ensure there's a silent period if required by the device.
time.sleep(1)
# Send another AT command.
ser.write(b"AT+CSQ\r\n")
# Read and print the response line-by-line, up to a certain limit.
num_of_lines = 0
while num_of_lines < 5:
response = ser.readline()
if response:
print("Line", num_of_lines + 1, ":", response.decode().strip())
num_of_lines += 1
# Close the serial connection when done.
ser.close()
Best Practices
- Always ensure you close the serial port after your operations to free up system resources.
- Use exception handling when opening or communicating through the serial port to catch and handle errors gracefully.
- If working with a device that requires entering command mode, follow its specific protocol for initiating such a state.
By understanding these basics of pySerial and serial communication, you are now equipped to interface your Python applications with an array of hardware devices. Whether it’s sending AT commands or reading sensor data from a microcontroller, the flexibility offered by pySerial can significantly streamline your development process in embedded systems projects or IoT applications.