Introduction
Codesys Runtime for Linux ARM SL allows you to program your Raspberry Pi according t the IEC 61131-6 standard and use it as an Industrial Controller. The Runtime installation can be downloaded form the Codesys Store website and can be used with a trial license to test it out, so there is no need of buying a license right away
After installing Codesys Runtime for Linux Arm, you are now free to program your raspberry pi as a PLC in from the Codesys environment and even implement your desired industrial communication protocols. One of the most common industrial protocols is the Modbus RTU. This is a Serial based protocol and uses the serial interface of your device to exchange information with a remote device via a serial communication channel
For standard Industrial Controllers that come already installed with Codesys runtime, you don’t need any extra configuration to access your serial interfaces, since the particular vendor will have implemented all the necessary for this functionality. All you you to do is to connect the remote device to a physical serial port and then communication can be established via the corresponding COM port number.
However, for a generic Codesys Runtime installation such as Codesys Control Win v3 (Windows) and Codesys Control for Linux for ARM (Linux) you need to do some extra configurations to expose your Serial interfaces to Codesys. See, codesys runtime identifies serial interfaces as based on their COM port number. For windows, its a lot easier, because when you connect a device to a serial port (USB, RS232, etc) in windows, windows will assign a COM port number to that interface and using this number, you can establish serial communication to the remote device that is connected to this interface. For Linux However, its a bit different
Linux does not automatically assign a COM port numbers to its serial interfaces. therefore the user has to manually map the desired serial ports. The goal of this guide therefore is to show how to map desired serial interfaces to different COM ports, and establish Modbus RTU communication with a report device. The guide particular addresses serial communication via the GPIO serial pins
This guide uses a Raspberry PI 3B+ but the same can be applied for other models. Raspberry Pi 2/3 have two UARTs (uart1 and uart0) that are accessible via GPIO pin 14 and Tx and pin 15 as RX. By default, UART0 is reserved for the On-board Bluetooth module and the UART1 (Mini UART) reserved for Linux Console.
Hardware Configuration
The serial interface is enabled over the GPIO pin 14 and Tx and pin 15 as RX however, you cannot connect the RS232 or the RS485 connections directly to these pins because of the voltage level differences between these serial two architectures and what the raspberry pi can support. Instead, a conversation must be done to convert the voltage level of the RS232 or the RS 485 to a level the raspberry pi can handle (3.3 to 5v). To do this , a RS 232/RS485 to TTL Converter can be used. For RS232 a MAX3232 can be used for this purpose
Software Configuration
Step 1: Enable the Serial Port
The first step is to enable the serial port on your raspberry pi which will permit serial communication via either the UART0 or UART1. To do this, we run the following command in the terminal
pi@raspberrypi:~ $ sudo raspi-config
When the configuration tool opens, select option 3: Interface Options
Then select Interface option 3: Serial Port
Select No wen prompted to allow a login shell to be accessible over serial
Select Yes when prompted to enable serial port hardware
Select Finish after this configuration
Step 2: Disable Bluetooth Module connection to UART interface
As mentioned before, by default one of the UART serial interfaces is connected to the login shell by default and the other is connected to the Bluetooth Module. So it is important to disconnected the Bluetooth module form the URAT interface and free it so that it can be connected to the Hardware serial interface (Serial Ports 14 and 15). To do this, you should edit the config.txt file located in the boot folder and add the following line
pi@raspberrypi:~ $ sudo nano /boot/config.txt
dtoverlay=diasble-bt
Step 3: Identify which UART interface is connected to the Hardware serial interface
Now you need to identify with UART interface is connected to the serial Hardware Interface. The hardware serial interface is usually identified with a symbolic link Serial0 and the Bluetooth module connection interface as Serial1, if it exists. Use the following commands
pi@raspberrypi:~ $ ls /dev
pi@raspberrypi:~ $ ls -l /dev
From the above image, you can clearly see which UART interface is connected to Serial0 and Serial 1
Step 4: Map COM Port numbers to Serial Interfaces
Now that we have identified which UART interface we are interested in, we can map this Interface to a COM Port Number. To do this, we must edit the CODESYSControl_User.cfg file found in the /etc directory. Add the following code to the file
sudo nano /etc/CODESYSControl_User.cfg
[SysCom]
Linux.Devicefile.1=/dev/ttyAMA0
Linux.Devicefile.2=.dev/ttyUSB0
With the above code, we have mapped /dev/ttyAMA0 to COM Port 1 as seen on line 1. If you have multiple serial interfaces, use the same procedure, making sure that you map each Interface on a different unique COM Port number. We can also map USB serial interfaces to COM port numbers as can be seen on line 2 above
After the editing process above, your CODESYSControl_User.cfg should look like this. You can now proceed and implement Modbus RTU communication in Codesys.