Skip to content

Modbus RTU Slave

Giampiero Baggiani edited this page Aug 7, 2018 · 50 revisions

This sketch implements a fully functional standard Modbus RTU slave, configurable via serial console, to monitor and control all I/O lines of the Iono Arduino and Iono MKR modules.

You can find it here.

Console

The configuration console can be accessed through Iono's RS-485 port or Arduino's 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 to Arduino.

When Iono is powered-up or reset, you can enter console mode by typing 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.

Enter 0 to show the current configuration, 1 to 3 to set the serial port speed, serial port parity, and the Modbus address

Sfera Labs - Iono Arduino/MKR - Modbus RTU Slave configuration menu
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 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, Arduino will automatically reset and restart with the new configuration.

The configuration is permanently stored in the Arduino's EEPROM memory, and retained across restarts and power cycles.

Modbus address tables and functions

Relays

Use Modbus function Read Coils, at coil address 1 to 6 (4 for MKR), to read the status of the output relays.

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 mV

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 25 ms time constant, so that a value change is reported only after being stable for at least 25 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 (30000 for MKR). Reading addresses 301 to 304 returns the current on AI1 to AI4 in mA/1000 (µA), from 0 to 20000 (25000 for MKR).

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-30000 mV
202 1 read analog voltage input AV2 r 4 unsigned short 0-30000 mV
203 1 read analog voltage input AV3 r 4 unsigned short 0-30000 mV
204 1 read analog voltage input AV4 r 4 unsigned short 0-30000 mV
301 4 read analog current inputs r 4
301 1 read analog current input AI1 r 4 unsigned short 0-25000 µA
302 1 read analog current input AI2 r 4 unsigned short 0-25000 µA
303 1 read analog current input AI3 r 4 unsigned short 0-25000 µA
304 1 read analog current input AI4 r 4 unsigned short 0-25000 µA
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
Clone this wiki locally