Knowledge Base

The AB Electronics UK Knowledge Base provides support solutions, tutorials and troubleshooting guides.

Knowledge Base

PySerial & RS232 Serial Communication

Getting Started with PySerial & RS232 Serial Communication on the Raspberry Pi

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 expansion 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 using PySerial:

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 using PySerial:

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 https://pyserial.readthedocs.io/en/latest/pyserial.html.

Real-world Applications:

There are many real-world applications of PySerial and 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 PySerial.
  • Modem Communication: A modem can be connected to the Raspberry Pi and used to send and receive data over the internet using PySerial for communication.
  • Industrial Automation: PySerial 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 PySerial. If you're new to Python programming, this tutorial should provide a good starting point for working with serial communication on a Raspberry Pi.


Was this article helpful? Yes No

Please tell us how we can improve this article:

Submit
Created 08/02/2023 | Last Updated: 25/10/2023