Skip to content

Modbus TCP

Giampiero Baggiani edited this page Sep 27, 2021 · 37 revisions

This sketch implements a fully functional standard Modbus TCP server, configurable via serial console, to monitor and control all I/O lines of Iono.
It is compatible with Iono Uno + Arduino Ethernet or Arduino Uno WiFi Rev 2 and with Iono MKR + Arduino MKR WiFi 1010.

You can find it here.

Console

Initial configuration of the network parameters requires any serial communication application (e.g. the Serial Monitor of the Arduino IDE) a USB cable and, for Arduino Ethernet, the USB Serial Adapter.

Set the serial port speed to 9600, 8 bits, no parity, no flow-control and connect to the Arduino’s serial port or USB.

Connect and send any character, the configuration console will be displayed:

Screenshot 2021-09-27 at 12 20 05

This sketch does not support dynamically assigned (DHCP) addresses.

With Iono Uno Ethernet you need to manually enter the MAC address of the board, while with Iono MKR you need to specify the SSID and the password of the WiFi network to use.

Enter 8 to review the configuration and confirm to save it. When the new configuration is saved, Arduino will automatically reset and restart with the new configuration.

From this point Iono will accept Modbus TCP requests on the standard 502 TCP/IP port on any Modbus address.

The configuration is permanently stored in the Arduino’s EEPROM/Flash memory and retained across restarts and power cycles.

After the first configuration the console will also be available via Telnet connecting to port 23 on the assigned IP.

Modbus address tables and functions

Relays

Use Modbus function Read Coils, at coil address 1 to 6, to read the status of the last command given to the output relays. Because the relays are latching, the status will return zero at boot, regardless of the actual position of the relays’ contacts.

Use functions Write Single Coil or Write Multiple Coils to control the relays output.

Address (decimal) Size [bit] Description R/W Functions
1 6 command output relays r/w 1,15
1 1 command relay 1 r/w 1,5,15
2 1 command relay 2 r/w 1,5,15
3 1 command relay 3 r/w 1,5,15
4 1 command relay 4 r/w 1,5,15
5 1 command relay 5 r/w 1,5,15
6 1 command relay 6 r/w 1,5,15

Analog (PWM) output

Use Modbus function Read Holding Registers at address 601 to read the status of the analog output AO1. The returned value is in mV, so the value range is 0 to 10000.

Use function Write Single Register to control the voltage output of AO1.

Address (decimal) Size [word] Description R/W Functions Type Range Unit
601 1 write analog voltage output AO1 r/w 3,6 unsigned short 0 - 10000 V/1000

Digital inputs

Digital inputs DI1 to DI6 can be read at two different address ranges, using function Read Discrete Inputs. Reading addresses 101 to 106 returns the value of the input pins filtered with a de-bounce function. This filter has a 50 ms time constant, so that a value change is reported only after being stable for at least 50 milliseconds.

Reading addresses 111 to 116 returns the instantaneous value of the input pins, with no de-bounce filter applied.

Address (decimal) Size [bit] Description R/W Functions
101 6 read digital inputs (with de-bounce) r 2
101 1 read digital input DI1 (with de-bounce) r 2
102 1 read digital input DI2 (with de-bounce) r 2
103 1 read digital input DI3 (with de-bounce) r 2
104 1 read digital input DI4 (with de-bounce) r 2
105 1 read digital input DI5 (with de-bounce) r 2
106 1 read digital input DI6 (with de-bounce) r 2
111 6 read digital inputs (no de-bounce) r 2
111 1 read digital input DI1 (no de-bounce) r 2
112 1 read digital input DI2 (no de-bounce) r 2
113 1 read digital input DI3 (no de-bounce) r 2
114 1 read digital input DI4 (no de-bounce) r 2
115 1 read digital input DI5 (no de-bounce) r 2
116 1 read digital input DI6 (no de-bounce) r 2

Analog inputs and digital counters

Use Modbus function Read Input Registers to read the values of analog inputs. Reading addresses 201 to 204 returns the voltage on AV1 to AV4 in mV, from 0 to 10000. Reading addresses 301 to 304 returns the current on AI1 to AI4 in mA/1000 (microAmperes), from 0 to 20000.

This sketch also implements counters on digital inputs. Reading input registers at address 1001 to 1006 returns unsigned short values from 0 to 65535. These counters are incremented by one (and rolled back to 0 after reaching 65535) on the positive rise of their respective inputs, after the de-bounce filter.

Address (decimal) Size [word] Description R/W Functions Type Range Unit
201 4 read analog voltage inputs r 4
201 1 read analog voltage input AV1 r 4 unsigned short 0-10000 V/1000
202 1 read analog voltage input AV2 r 4 unsigned short 0-10000 V/1000
203 1 read analog voltage input AV3 r 4 unsigned short 0-10000 V/1000
204 1 read analog voltage input AV4 r 4 unsigned short 0-10000 V/1000
301 4 read analog current inputs r 4
301 1 read analog current input AI1 r 4 unsigned short 0-20000 mA/1000
302 1 read analog current input AI2 r 4 unsigned short 0-20000 mA/1000
303 1 read analog current input AI3 r 4 unsigned short 0-20000 mA/1000
304 1 read analog current input AI4 r 4 unsigned short 0-20000 mA/1000
1001 6 read digital inputs counters r 4
1001 1 read digital input DI1 counter r 4 unsigned short 0-65535 n/a
1002 1 read digital input DI2 counter r 4 unsigned short 0-65535 n/a
1003 1 read digital input DI3 counter r 4 unsigned short 0-65535 n/a
1004 1 read digital input DI4 counter r 4 unsigned short 0-65535 n/a
1005 1 read digital input DI5 counter r 4 unsigned short 0-65535 n/a
1006 1 read digital input DI6 counter r 4 unsigned short 0-65535 n/a