PySerial is a Python library used for working with serial ports. RS232 is a standard for serial data transmission between computers and other devices. Raspberry Pi, a low-cost single-board computer, is commonly used for various applications, including communicating with serial devices such as GPS, modem, etc.
You can add RS232 to the Raspberry Pi using the Serial Pi Plus or Serial Pi Zero development boards from AB Electronics UK.
For information on enabling the UART port on your Raspberry Pi, view our tutorial Serial Port setup in Raspberry Pi OS.
This tutorial will teach us to use PySerial to send and receive data over RS232 serial communication on a Raspberry Pi.
Installing PySerial:
To use PySerial, we need to install it first. To do that, open the terminal on your Raspberry Pi and type the following command:
pip3 install pyserial
If you don't have pip3 installed, use the following command to install it:
sudo apt-get install python3-pip
Sending Data:
Here's an example of how to send data:
import serial ser = serial.Serial('/dev/ttyAMA0', baudrate=9600) ser.write(b'Hello, World!\n') ser.close()
In this example, the first line imports the PySerial library. The following line creates an instance of the Serial class and opens the serial port "/dev/ttyAMA0" with a baud rate 9600. The write method sends the data "Hello, World!\n" over the serial port. Finally, the close method is used to close the serial port.
Receiving Data:
Here's an example of how to receive data:
import serial ser = serial.Serial('/dev/ttyAMA0', baudrate=9600) while True: data = ser.readline() if len(data) > 0: print(data.decode('utf-8')) ser.close()
In this example, the first line imports the PySerial library. The following line creates an instance of the Serial class and opens the serial port "/dev/ttyAMA0" with a baud rate of 9600. The while loop continuously reads data from the serial port using the readline method until a newline character "\n" is received.
The if statement checks if there is any data received. If there is data, it is decoded using the decode method and printed to the console. Finally, the close method is used to close the serial port.
For a more in-depth look at the pySerial library, visit PySerial.
Real-world Applications:
There are many real-world applications for RS232 communication on a Raspberry Pi. Here are a few examples:
- GPS Tracking: A GPS module can be connected to the Raspberry Pi via RS232 serial communication. The location data can be received and processed using the Raspberry Pi.
- Modem Communication: A modem can be connected to the Raspberry Pi and used to send and receive data over the internet.
- Industrial Automation: You can control and monitor industrial equipment, such as PLCs, sensors, etc., using RS232 serial communication.
This tutorial taught us to use PySerial to send and receive data over RS232 serial communication on a Raspberry Pi. We also saw some real-world applications of serial communication using RS232. If you're new to Python programming, this tutorial should provide a good starting point for working with serial communication on a Raspberry Pi.