How-To RS485 Serial Port
Description
The Pi-Tron CM5 has an RS485 interface on the front of the device. The serial port itself is realized through a USB-to-Serial converter chip, thus making the interface accessible through the device name ttyUSB and not the Raspberry Pi usual ttyAMA or ttyS interfaces. The same applies to the RS232 interface on the Pi-Tron CM5 which is covered on this page How-To RS232 Serial Port.
Requirements
- Development computer with network access to the Pi-Tron CM5 or alternatively connect an HDMI monitor, mouse and keyboard.
- To login into the Pi-Tron CM5 via network, it is requires that the SSH server is running.
- 24 volts power supply with 2 pin power plug for the Pi-Tron CM5.
- Common baud rates are supported, 115200 baud are recommended.
How-To
The RS485 serial port should be available right out of the box, when using Raspberry Pi OS Bookworm or later.
Identifying the serial ports
Since the Pi-Tron CM5 has two serial ports, the first step is to identify which ttyUSB device is which serial port.
Log into the Pi-Tron CM5 and issue the following command (output shortened):
ls -la /dev/serial/by-path/*
/dev/serial/by-path/platform-fe9c0000.xhci-usb-0:1.3:1.0-port0 -> ../../ttyUSB0
/dev/serial/by-path/platform-fe9c0000.xhci-usb-0:1.3:1.1-port0 -> ../../ttyUSB1
The result from this command shows that one serial port is connected to the onboard USB controllers port "1.3:1.0" and the other one to port "1.3:1.1".
From the electrical wiring and the used USB-to-Serial converter chip, the link between the hardware path and the serial port name is in this instance, that the RS485
interface is accessible via ttyUSB1
and the RS232 port via ttyUSB0.
Note on USB devices
Adding more serial devices to the Pi-Tron CM5 via USB ports, could lead to a change in the device name sequence. Make sure to check the ttyUSB links after every boot. Alternatively have a look at the How-To Device Management page on how to create persistent device names.
Connector X12 Pins
Connector X12 where the RS485 serial port is located has the following pin layout.
Bus Termination
The bus termination for the RS485 interface can be turned on by setting the switch number 6 on the S1 switch to ON.
Using the RS485 serial port
The echo command can be used to send out and cat to receive data. Of course there are many other options and possibilities, but for this guide we keep it simple.
Send out Hello World!:
echo "Hello World!" > /dev/ttyUSB1
Receive data:
cat < /dev/ttyUSB1
RS485 Send/Receive automatic switch
The Pi-Tron CM5's RS485 serial port is fitted with an automatic switch for the send and receive direction of the RS485 port. There is no need for manual switching. This circumstance also makes the RS485 serial port on the Pi-Tron CM5 Modbus RTU capable.
This concludes the How-To RS485 Serial Port. Feel free to connect the Pi-Tron CM5's serial port to a computer for example and establishing a communication connection or try to communicate with another RS485 serial device or try a Modbus RTU connection.
Restrictions
- Note: USB serial devices of the same type are assigned the same device base name (ttyUSB) and only differentiated through an appended number, which is incremented with each found device. This results in device sequences such as ttyUSB0, ttyUSB1 and so on. Although these devices are connected to the same bus and cannot physically switch their order, the drivers however might change the naming sequence regardless under certain circumstances. If such a behavior cannot be handled in software, have a look at the How-To Device Management page on how to create persistent device names.
Related documentation
- Covering all aspects of the Raspberry Pi, the official documentation: https://www.raspberrypi.com/documentation
- Manpages ls: https://manpages.debian.org/bullseye/coreutils/ls.1.en.html
- Manpages udevadm: https://manpages.debian.org/buster/udev/udevadm.8.en.html
- Manpages echo: https://manpages.debian.org/buster/coreutils/echo.1.en.html
- Manpages cat: https://manpages.debian.org/bullseye/coreutils/cat.1.en.html
- Modbus Organization: https://modbus.org
- Python Modbus modules on pypi: https://pypi.org/search/?q=pyModbus