The PI-RSX module allows you to add RS-232, RS-485, or RS-422 interfaces to your Raspberry Pi. Everything is contained in a single module, and the interface type is selected using jumpers on the PCB. Let's take a look at how to use it in Python.

First, we need to tell the system that we want to use the interface. It depends on the type of Raspberry, but typically we add the following line to the configuration file /boot/firmware/config.txt:

dtoverlay=uart0,txd0_pin=14,rxd0_pin=15

The interface is connected to pins 14 and 15 on the header. The setup method may vary depending on the type of Raspberry Pi. Instructions can be found in the Raspberry OS system in the /boot/firmware/overlays/README file. Also, check the documentation for your specific Raspberry to see which interface you can use. We will use uart0.

Now for the Python program when using RS-485 or RS-422:

import serial
import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setup(18, GPIO.OUT)

ser = serial.Serial(
    port='/dev/ttyAMA0',
    baudrate = 115200,
    parity=serial.PARITY_NONE,
    stopbits=serial.STOPBITS_ONE,
    bytesize=serial.EIGHTBITS,
    timeout=1
)


GPIO.output(18,1) #set high/transmit
TxData = "Hello world"
ser.write(TxData.encode("utf8"))
time.sleep(len(TxData)*10/115200) #8 bits + start bit + stop bit
GPIO.output(18, 0) #pin set to low/receive
ReceivedData = ""
while (ReceivedData == ""):
   RxData = ser.readline();
   if (RxData != b""):
      print(RxData)

The sample application opens the serial port, sets GPIO18 (pin 12 on the 40-pin header) to control the driver direction, sends "Hello world," and then listens and prints the received data.

When using RS-232, it is the same, except that we do not need to use GPIO control.

 

Cookies

We use cookies to deliver and enhance the quality of our services.