12th Anniversary Sale 12% Off in July & August with the voucher code "SUMMER12" *excludes trade customers
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:

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.


Was this article helpful? Yes No

Please tell us how we can improve this article:

Submit

Related Expansion Boards

Order a Serial Pi Plus Photo of Serial Pi Plus
Serial Pi Plus

RS232 Serial Port for the Raspberry Pi

£13.19 Ex VAT: £10.99
Order a Serial Pi Zero Photo of Serial Pi Zero
Serial Pi Zero

RS232 serial interface for the Raspberry Pi Zero

£11.39 Ex VAT: £9.49
Order a RS485 Pi Photo of RS485 Pi
RS485 Pi

RS485 serial interface for the Raspberry Pi

£14.39 Ex VAT: £11.99

Related Articles

Serial Port setup in Raspberry Pi OS Bookworm
Configuring the serial port on the Raspberry Pi
Serial Port Loopback Test
Testing the status of the Serial Port on a Raspberry Pi
PySerial & RS232 Serial Communication
Getting Started with PySerial & RS232 Serial Communication on the Raspberry Pi