Skip to content

Modbus RTU Slave

Giampiero Baggiani edited this page Nov 24, 2022 · 50 revisions

This sketch implements a fully functional standard Modbus RTU slave, configurable via serial console and via Modbus, to monitor and control all I/O lines of the Iono Uno, Iono MKR and Iono RP modules. Moreover it allows for reading data from 1-Wire Dallas temperature sensors and Wiegand devices (keypads, card readers, biometric readers, ...) connected to Iono.

Dependencies:

Console

The configuration console can be accessed through Iono's RS-485 interface or USB port, using any serial communication application (e.g. the Serial Monitor of the Arduino IDE).

Set the communication speed to 9600, 8 bits, no parity, no flow-control and connect the cable.

When Iono is powered-up or reset, you can enter console mode by sending five or more consecutive space characters within 15 seconds from reset. If any other character is received, Iono will enter the normal Modbus RTU mode.

=== Sfera Labs - Modbus RTU Slave configuration menu - vX.X ===
0. Print current configuration
1. Speed (baud)
2. Parity
3. Modbus device address
4. Input/Output rules
5. Save configuration and restart
Type a menu number (0, 1, 2, 3, 4, 5): 0

Current configuration:
Speed (baud): 115200
Parity: Even
Modbus device address: 2
Input/Output rules: - - - - - -

Enter 0 to show the current configuration, 1 to set the serial port speed, 2 to set the serial port parity (Even - 1 stop bit, Odd - 1 stop bit, None - 2 stop bits), and 3 to set the unit's Modbus address.

Enter 4 to define the Input/Output rules. With these rules you can configure each one of the digital inputs to control the corresponding output relay. The rules string consists of six characters, where the leftmost character represents the rule for DI1/DO1 and the rightmost character for DI6/DO6 (for Iono MKR only the the first four rules apply). The possible rules are:

F: follow - the relay is closed when input is high

I: invert - the relay is closed when input is low

H: flip on L>H transition - the relay is flipped at any input transition from low to high

L: flip on H>L transition - the relay is flipped at any input transition from high to low

T: flip on any transition - the relay is flipped at any input transition, both high to low and low to high

-: no rule - no control rule set for this relay.

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

The configuration is persisted in memory and retained across restarts and power cycles.

Modbus address tables and functions

In the following, for the "Functions" column:
1 = Read coils
2 = Read discrete inputs
3 = Read holding registers
4 = Read input registers
5 = Write single coil
6 = Write single register
15 = Write multiple coils
16 = Write multiple registers

Configuration

Use function Write Single Register or Write Multiple Registers to set the new configuration parameters, then write 0xABCD at address 3000 to save it.

Address (decimal) Size [word] Description R/W Functions Type Range
3000 1 write 0xABCD to save the new configuration written in the registers below. This register can only be written individually, i.e. using function 6, or function 16 with a single data value. After positive response the unit is restarted and the new configuration is applied w 6,16 unsigned short n/a
3001 1 Modbus unit address r/w 3,6,16 unsigned short 1-247
3002 1 Modbus baud rate:
1 = 1200
2 = 2400
3 = 4800
4 = 9600
5 = 19200
6 = 38400
7 = 57600
8 = 115200
r/w 3,6,16 unsigned short 1-8
3003 1 Modbus parity/stop bits:
1 = even, 1 stop bit
2 = odd, 1 stop bit
3 = none, 2 stop bits
r/w 3,6,16 unsigned short 1-3
3004 1 DI1/DO1 rule (see below) r/w 3,6,16 unsigned short 'F' (0x46), 'I' (0x49), 'H' (0x48), 'L' (0x4C), 'T' (0x54), '-' (0x2D)
3005 1 DI2/DO2 rule (see below) r/w 3,6,16 unsigned short 'F' (0x46), 'I' (0x49), 'H' (0x48), 'L' (0x4C), 'T' (0x54), '-' (0x2D)
3006 1 DI3/DO3 rule (see below) r/w 3,6,16 unsigned short 'F' (0x46), 'I' (0x49), 'H' (0x48), 'L' (0x4C), 'T' (0x54), '-' (0x2D)
3007 1 DI4/DO4 rule (see below) r/w 3,6,16 unsigned short 'F' (0x46), 'I' (0x49), 'H' (0x48), 'L' (0x4C), 'T' (0x54), '-' (0x2D)
3008 1 DI5/DO5 rule (see below) - Iono Uno only r/w 3,6,16 unsigned short 'F' (0x46), 'I' (0x49), 'H' (0x48), 'L' (0x4C), 'T' (0x54), '-' (0x2D)
3009 1 DI6/DO6 rule (see below) - Iono Uno only r/w 3,6,16 unsigned short 'F' (0x46), 'I' (0x49), 'H' (0x48), 'L' (0x4C), 'T' (0x54), '-' (0x2D)

For Input/Output rules (registers 3004 - 3009) the value corresponds to the ASCII value of the character representing the rule, as described above for the Console configuration.

Monitoring and control

See here for all the other registers provided by the IonoModbusRtuSlave library, included in this application.