CODESYS - the IEC 61131-3 automation software

Raspberry 3 + Arduino Uno USB + MODBus RTU
Page 1 of 1

Author:  marv_nene [ Tue May 03, 2016 7:08 am ]
Post subject:  Raspberry 3 + Arduino Uno USB + MODBus RTU

I'm using the following setup:
- Raspberrry Pi 3 (Modbus RTU Master)
- Codesys Runtime (Current Version)
- Arduino UNO (Modbus RTU Slave)

The Raspberry Pi controls the logic and the Arduino uno is for the periphery. They're connected per USB. Communication over modbus rtu works between these two and I testet it with different bitrates.
Modbus RTU works after every code upload quite well.
But after every cold boot, codesys does not recognize the Arduino as Slave.
In this case, a "cold reboot" helps to fix the the problem in most cases.

Have anyone an idea where the problem is or a guess how to fix it?
Thanks :)

Author:  Edwin Schwellinger [ Wed May 04, 2016 5:05 am ]
Post subject:  Re: Raspberry 3 + Arduino Uno USB + MODBus RTU

I would recommend to check:
1. If the startup is done and the communication stucks with modbus rtu, Login with CODESYS and check the device tree
- which components have a green\red sign (Modbus Master / Modbus Slave / Modbus Comport)
- Then add the name of the slave (the name in the devicetree) to the watchwindow and check the communications states, if there is an slave error you should see this
Maybe you give us a few screenshots of these states to see what happens

Author:  marcys [ Sat May 28, 2016 5:48 pm ]
Post subject:  Re: Raspberry 3 + Arduino Uno USB + MODBus RTU

Hello marv_nene,

Would You like to post screen how you connect raspberry with arduino and how you configure it?
I want to do the same to control my home


Author:  marv_nene [ Tue May 31, 2016 2:38 pm ]
Post subject:  Re: Raspberry 3 + Arduino Uno USB + MODBus RTU

@Edwin, thanks for your response, but I figured out a solution:
   Modbus_Slave_COM_Port.xReset := FALSE;
xInit := TRUE;
IF Modbus_Slave_COM_Port.xError THEN
   Modbus_Slave_COM_Port.xReset := TRUE;
   Modbus_Master_COM_Port.xResetComPort := TRUE;
   Modbus_Master_COM_Port.xStop := TRUE;
   Modbus_Slave_COM_Port.xReset := FALSE;
   Modbus_Master_COM_Port.xResetComPort := FALSE;
   Modbus_Master_COM_Port.xStop := FALSE;

I guess, the problem is the following: When the raspberry boots, the COM port for the arduino is initialized after the start of the codesys runtime, or it seems it needs longer then expected. So, the automatic reconnection doesn't work for me. When I do it manually, it works. This code is executed every 5 seconds.

I will respond you later.

Author:  marcys [ Wed Jun 01, 2016 6:12 pm ]
Post subject:  Re: Raspberry 3 + Arduino Uno USB + MODBus RTU

Hi marv_nene,

Ok, Thank You. I will wait for response.

Author:  marv_nene [ Thu Jun 09, 2016 7:54 am ]
Post subject:  Re: Raspberry 3 + Arduino Uno USB + MODBus RTU

Hello Marcys,
so, first you have to configure the USB Com Port of your connected Arduino described in this

For ModBus on Arduino, you can use the ModBusIno Library from stephane @[url]
[/url]. It works for me with
Baud : 38400
Parity: none
Data Bits: 8
Stop bits: 2

In Codesys add a Com-Port-> Modbus RTU Master-> Modbus Slave

In the Modbus Master deactivate the "auto reconnect" and use my code above.
A good idea is, to create a task for it, which circles all 30 sec.

Hope, this will help you.

Author:  marcys [ Fri Jun 10, 2016 6:20 am ]
Post subject:  Re: Raspberry 3 + Arduino Uno USB + MODBus RTU

Hello marv_nene,

Would you like to post some screenshot and code example. I did what you wrote but it does not work for me:-(


Author:  sandro66 [ Fri Jul 15, 2016 4:52 pm ]
Post subject:  Re: Raspberry 3 + Arduino Uno USB + MODBus RTU

You need to set .xResetComPort and .xStop flags for a minimun of 5 sec.

Hope, this will help you.


Author:  alverman [ Wed May 17, 2017 10:21 am ]
Post subject:  Re: Raspberry 3 + Arduino Uno USB + MODBus RTU

I tried inserting the code in a task to check the status of the arduino connection but it does not work automatically.
When I unplug the arduino usb cable I can not see Modbus_Slave_COM_Port.xError switched from FALSE to TRUE so when connecting the cable the connection is not restored.
If I force the state of xError to TRUE and then put it back to FALSE the connection re-establishes.

Where am I wrong?

Thank you, Alberto

Author:  elconfa [ Thu Nov 22, 2018 2:41 pm ]
Post subject:  Re: Raspberry 3 + Arduino Uno USB + MODBus RTU

Hi, I'm trying to connect Raspberry 3 B + to Arduino Mega via USB cable. In Arduino I loaded the sample program of the Modbusino library. I added the inscription:

Linux.Devicefile = / dev / ttyUSB
portnum: = COM.SysCom.SYS_COMPORT1;

in the CODESYSControl.cfg file.

Arduino is programmed directly by Raspberry and the Arduino IDE sees the card connected on the /dev/ttyUSB0 port.
I tried at various speeds, but I can not read or write anything.

Could someone post a sample code for Arduino and for Raspberry Codesys to try?

In next post I will submit all my project.

Thanks in advance to everyone that would help me.


[SOLVED] Now it work. From Raspberry to Arduino Mega it is possible to control all Input and Output

The code for Arduino:

* Modbus slave example 2:
* The purpose of this example is to link the Arduino digital and analog
* pins to an external device.
* Recommended Modbus Master: QModbus
* Editado al español por LuxARTS

//Incluye la librería del protocolo Modbus
#include <ModbusRtu.h>
#define ID 1

//Crear instancia
Modbus slave(ID, 0, 0); //ID del nodo. 0 para el master, 1-247 para esclavo
//Puerto serie (0 = TX: 1 - RX: 0)
//Protocolo serie. 0 para RS-232 + USB (default), cualquier pin mayor a 1 para RS-485
boolean led;
int8_t state = 0;
unsigned long tempus;

uint16_t au16data[8]; //La tabla de registros que se desea compartir por la red

Configuración del programa
void setup() {
io_setup(); //configura las entradas y salidas

slave.begin(19200); //Abre la comunicación como esclavo
tempus = millis() + 100; //Guarda el tiempo actual + 100ms
digitalWrite(13, HIGH ); //Prende el led del pin 13 (el de la placa)

Inicio del programa
void loop() {
//Comprueba el buffer de entrada
state = slave.poll( au16data, 8 ); //Parámetros: Tabla de registros para el intercambio de info
// Tamaño de la tabla de registros
//Devuelve 0 si no hay pedido de datos
//Devuelve 1 al 4 si hubo error de comunicación
//Devuelve mas de 4 si se procesó correctamente el pedido

if (state > 4) { //Si es mayor a 4 = el pedido fué correcto
tempus = millis() + 50; //Tiempo actual + 50ms
digitalWrite(13, HIGH);//Prende el led
if (millis() > tempus) digitalWrite(13, LOW );//Apaga el led 50ms después

//Actualiza los pines de Arduino con la tabla de Modbus

void io_setup() {
pinMode(13, OUTPUT);
digitalWrite(13, HIGH ); //Led del pin 13 de la placa

Enlaza la tabla de registros con los pines
void io_poll() {

au16data[0] = au16data[4] + au16data[5];
au16data[1] = au16data[6] + au16data[7];
au16data[2] = au16data[4] + au16data[6];
au16data[3] = au16data[5] + au16data[7];


Page 1 of 1 All times are UTC+01:00
Powered by phpBB® Forum Software © phpBB Limited