In this article I'll describe how to implement a Modbus RTU communication over RS485 line between Raspberry PI 2 ( Master) and a PC (Slave) using the IEC61131 Codesys IDE / runtime
 
Raspberry+CoDeSys+USBto485
|
rs485
Sniffing1
|
| rs485
|
 232to485 +Laptop plc sim
 
This is the hardware / software configuration:

RPI2:
  • Raspbian Jessie LITE v. 2015-11-21 ; kernel version 4.1 
  • Codesys RPI runtime  image v.3.5.8.0
  • USB / RS485 adapter (ch341-uart)USBto485
 
 
PC:
  • RS232 / RS485 (STM485) serial adapter232to485
  • PuTTY v.0.66: xterm emulator for remote connection to Raspberry via SSH
  • Hercules v.3.2.8 : free Ethernet / serial test utility (yo can download from here )
  • Modbus PLC Simulator : Embedded Intelligence PLC SIM v. 8.20 (you can download from here )
  • Codesys IDE v. 3.5.8.0

DEVELOPMENT:

  • After booting the RPI, using PuTTY over SSH, I get the remote connection: using user: pi and password raspberry (default in Raspbian) I get into the OS

PuTTY1

 

  • Check for the properly data transmission over the 485 line between RPI and PC:at the end of the USB/485 adapter installation the ttyUSB0 is detected into Raspbian /dev directory
cd dev
 
  • Typing dmesg | grep tty command it returns the "usb 1-1.3: ch341-uart converter now attached to ttyUSB0"
dmesg 1
 
  • Then, using Hercules, in order to verify the work of the USB/RS485 and RS232/485 adapters, I try to exchange some character from RPI --> to PC typing

echo RPI to PC > / dev/ttyUSB0

echo RPI to PC 1 > / dev/ttyUSB0

echo RPI to PC 2 > / dev/ttyUSB0

TestTTYUSB 1
 
  •  As suggested into Codesys forum, using an Adapeter for serial communciation, it's necessary to edit the /etc/CODESYSControl.cfg file adding  the following lines:
 
[SysCom]
Linux.Devicefile=/dev/ttyUSB
portnum := COM.SysCom.SYS_COMPORT1;
 
by typing  sudo nano /etc/CODESYSControl.cfg
 CodesysControlcfg1
Reboot is mandatory, alternatively stop then restart the Codesys runtime service with following commands:
sudo /etc/init.d/codesyscontrol stop
and then
sudo/etc/init.d/codesyscontrol start
RestartService
 
 
  • Then create a new Codesys project inclusing a Modbus serial intereface configured with 9600,8,N,1 Prj 1a
 
  • Add the Master Modbus RTU
Prj 2
 
 
  • Add the Slave Modbus RTU (in my sample is the Embedded Intelligence PLC SIM v. 8.20 running on PC) Prj 3
 
  • Define reading channel on Slave side  (I defined a read holding register channel from register 0 (40001) leght 8 (till 40008) Prj 4
 
  • Include a Visualization page to show the registers reading realtime changing

 Prj 5

 
  • Build, go online and then look at following video for the application running