MODBUS TCP/IP Communication Between Master and Slave Devices Using Raspberry Pi Hardware
This example shows how to use the Simulink® Support Package for Raspberry Pi® Hardware to implement MODBUS® TCP/IP communication between MODBUS master and slave devices. It also shows how to communicate between the two devices in four modes of operation, Master Read, Master Write, Slave Read, and Slave Write.
This example uses two Raspberry Pi Simulink models, namely Raspberry Pi MODBUS Master and Raspberry Pi MODBUS Slave, which together utilize different modes of operation for the master and slave devices.
Master Read — Master device reads data from the slave device register(s) over TCP/IP
Master Write — Master device writes data to the slave device register(s) over TCP/IP
Slave Read — Slave device reads data from the slave device registers over TCP/IP
Slave Write — Slave device writes data to the slave device registers over TCP/IP
The Raspberry Pi MODBUS Slave model is deployed on one Raspberry Pi board, while the Raspberry Pi MODBUS Master model runs in external mode on the other Raspberry Pi board.
This table categories the different slave device registers referenced by the MODBUS master and slave devices.
Register Type | Register Size | Allowed Master Operation on Register ----------------------------------------------------------------------------------- Coil | 1-bit | Read and Write Discrete Input | 1-bit | Read Holding Register | 16-bit | Read and Write Input Register | 16-bit | Read
Prerequisites
For more information on how to use the Simulink Support Package for Raspberry Pi Hardware to run a Simulink model on Raspberry Pi hardware, see Getting Started with Simulink Support Package for Raspberry Pi Hardware.
Required Hardware
Two Raspberry Pi hardware boards
Two USB cables
Configure MODBUS Master Simulink Model and Calibrate Parameters
This support package provides a preconfigured model for the MODBUS master.
To open the model, run this command in the MATLAB® Command Window.
open_system('raspberrypi_modbus_master')
The default values for the Simulink master model are preconfigured in this example depending on the slave register size and the allowed master operations. The master write operation is valid on coil and holding registers, whereas the master read operation is valid on all the register types.
For the master mode of operation, configure the IP address of the master Raspberry Pi board in Configuration Parameters > Hardware Implementation > Target hardware resources > Board Parameters.
Configure these parameters in Configuration Parameters > Hardware Implementation > Target hardware resources > Modbus properties.
Set Communication Interface to
TCP/IP
.Set Mode to
Master
.Configure the slave device IP port number in the Remote Slave IP port number parameter. In this example, the parameter is set to
502
.Set Received timeout (ms) to
100
seconds.
Configure MODBUS TCP/IP Master Write Blocks for Coil Registers
To perform the 1-bit write operation on the coil registers, change the position of the slider switches. The On
position depicts that data 1
is written on these registers, whereas the Off
position depicts that data 0
is written on these registers. Three slider switches are used in this model and their output is multiplexed and fed as an input to the MODBUS TCP/IP Master Write blocks.
Configure these parameters in the MODBUS TCP/IP Master Write Block Parameters dialog box to write data from the slider switches to the coil registers.
Enter the unique IP address of the slave device in the Slave address parameter. In this model, the parameter is set to
172.19.66.177
.Select the
Write Multiple Coils
option in the Function parameter.To notify the master to write data to two coil registers, specify the address of the first coil in the Coil Address parameter. The default value is
0
.Enter
3
in the Number of Coils parameter. The default positions of the slider switches areOff
,On
, andOn
.
Configure MODBUS TCP/IP Master Write Blocks for Holding Registers
To perform the 16-bit master write operation on the holding registers, this example uses a counter whose count value increments from 0 to 15 and a pulse generator. The outputs from the free-running counter and the pulse generator are multiplexed and fed as an input to the MODBUS TCP/IP Master Write block.
Configure these parameters in the MODBUS TCP/IP Master Write Block Parameters dialog box to write data from the free-running counter and pulse generator to the holding registers.
Enter the unique IP address of the slave device in the Slave address parameter. In this model, the parameter is set to
172.19.66.177
.Select the
Write Multiple Holding register
option in the Function parameter.To notify the master to write data to the holding registers, specify the address of the first coil register in the Holding Register Address parameter. The default value is
0
.Enter
2
in the Number of Holding registers parameter.
Configure these parameters in the Counter Free-Running Block Parameters dialog box.
Enter
4
in the Number of bits parameter for the counter to count from 0 to 15.Set the Sample time to
0.1
seconds.
Configure these parameters in the Pulse Generator Block Parameters dialog box.
Select the
Sample based
option in the Pulse type parameter.Set the Sample time to
0.1
seconds.
Configure MODBUS TCP/IP Master Read Block for Coil Registers
To perform the 1-bit read operation on the coil registers, three lamp indicators are used to signify the 1-bit read operation from the coil registers. To read this data from the coil registers, configure these parameters in the MODBUS TCP/IP Master Read Block Parameters dialog box.
Enter the unique IP address of the slave device in the Slave address parameter. In this model, the parameter is set to
172.19.66.177
.Select the Read Multiple Coils option in the Function parameter.
To notify the master to read data from coil registers, specify its address in the Coil Address parameter. The default value is
0
.Set Number of Coils to
3
.Set the Sample Time parameter to
0.1
.
Configure MODBUS TCP/IP Master Read Block for Holding Registers
To perform the 16-bit read operation on the holding registers, configure these parameters in the MODBUS TCP/IP Master Read Block Parameters dialog box. The free-running counter and the pulse generator output data that is written on the holding registers can be read using the MODBUS TCP/IP Master Read block. The holding register 0, holding register 1, and the Status outputs can be viewed in the Scope.
Enter the unique IP address of the slave device in the Slave address parameter. In this model, the parameter is set to
172.19.66.177
.Select the Read Multiple Holding registers option in the Function parameter.
To notify the master to read data from coil registers, specify its address in the Holding Register Address parameter. The default value is
0
.Set Number of Holding registers to
2
.Set the Sample Time parameter to
0.1
.
Configure MODBUS TCP/IP Master Read Block for Discrete Input Registers
The data read from the discrete input registers is high when the count value from the free-running counter is greater than or equal to 8. After the count value of 15, the output from the block is reset to 0. This data is valid only if the Status port value is 1, indicating a successful read operation and the presence of valid data on its data port. A lamp indicator is used to signify the 1-bit read from the discrete input register. To perform the 1-bit read operation on the discrete input register, configure these parameters in the MODBUS TCP/IP Master Read Block Parameters dialog box.
Enter the unique IP address of the slave device in the Slave address parameter. In this model, the parameter is set to
172.19.66.177
.Select the Read Discrete input option in the Function parameter.
To notify the master to read data from one discrete input register, specify its address in the Discrete Input Address parameter. The default value is
0
.Set the Sample Time parameter to
0.1
.
Configure MODBUS TCP/IP Master Read Block for Input Registers
The data read from the input registers is high when the data read from the holding register corresponding to the pulse generator output is low. Similarly, the data read from the input registers is low when the data read from the holding register corresponding to the pulse generator output is high. This data is valid only if the status port value is 1
, indicating a successful read operation and the presence of valid data on its data port. Observe the output in the scope. To perform the 16-bit read operation on the input register, configure these parameters in the MODBUS TCP/IP Master Read Block Parameters dialog box.
Enter the unique IP address of the slave device in the Slave address parameter. In this model, the parameter is set to
172.19.66.177
.Select the Read Input register option in the Function parameter.
To notify the master to read data from one input register, specify its address in the Input Register Address parameter. The default value is
0
.Set the Sample Time parameter to
0.1
.
Configure MODUS Slave Simulink Model and Calibrate Parameters
This support package provides a preconfigured model for the MODBUS slave.
To open the model, run this command in the MATLAB Command Window.
open_system('raspberrypi_modbus_slave')
The default values for the Simulink slave model are preconfigured in this example.
For the slave mode of operation, configure the IP address of the slave Raspberry Pi board in Configuration Parameters > Hardware Implementation > Target hardware resources > Board Parameters.
Configure these parameters in Configuration Parameters > Hardware Implementation > Target hardware resources > Modbus properties.
Set Communication Interface to
TCP/IP
.Set Mode to
Slave
.Configure the master device IP port number in the Local IP port number parameter. In this example, the parameter is set to
502
.Select all the register types. The default Start address and Quantity parameters for these register types are configured as shown in the table:
Register Type | Start Address | Quantity -------------------------------------------------------- Coil | 0 | 3 Discrete Input | 0 | 1 Holding Register | 0 | 2 Input Register | 0 | 1
Configure MODBUS TCP/IP Slave Read Block for Coil Registers
To perform the 1-bit slave read operation on the coil registers, configure these parameters in the MODBUS TCP/IP Slave Read Block Parameters dialog box for each coil register.
Select the Read Coil option in the Function parameter.
Set the Coil Address parameter to
0
.Set the Sample Time parameter to
0.1
.
Similarly, configure the other two MODBUS TCP/IP Slave Read blocks and set the Coil Address parameter to 1
and 2
, respectively, while the other parameters remain the same.
Double-click the subsystem connected to the Data output port of the MODBUS TCP/IP Slave Read block. The Raspberry Pi GPIO Write pins are mapped to the slave coil register address. Configure the Raspberry Pi Board and GPIO number parameters.
Configure MODBUS TCP/IP Slave Read Block for Holding Registers
To perform the 16-bit slave read operation on the holding registers, configure these parameters in the MODBUS TCP/IP Slave Read Block Parameters dialog box for the free-running counter and the pulse generator output.
Select the Read Holding register option in the Function parameter.
Set the Holding Register Address parameter to
0
.Set the Sample Time parameter to
0.1
.
Similarly, configure the other MODBUS TCP/IP Slave Read blocks and set the Holding Register Address parameter to 1
, while the other parameters remain the same.
Configure MODBUS TCP/IP Slave Write Block for Discrete Input Registers
Double-click the subsystem connected to the Data output port of the MODBUS TCP/IP Slave Read block. The data written in to the discrete input registers is high when the data read from the holding register corresponding to the free-running counter is greater than or equal to 8. After the count value of 15, the output from the block is again reset to 0. This data is valid only if the Status port value is 1, indicating a successful read operation and the presence of valid data on its data port. To perform the 1-bit slave write operation on the discrete input register, configure these parameters in the MODBUS TCP/IP Slave Write Block Parameters dialog box for the discrete input register.
Select the Write Discrete Input option in the Function parameter.
Set the Discrete Input Address parameter to
0
.
Configure MODBUS TCP/IP Slave Write Block for Input Registers
Double-click the subsystem connected to the Data output port of the MODBUS TCP/IP Slave Read block. The data read written in to the input registers is high when the data read from the holding register corresponding to the pulse generator output is low. Similarly, the data read from the input registers is low when the data read from the holding register corresponding to the pulse generator output is high. This data is valid only if the status port value is 1
, indicating a successful read operation and the presence of valid data on its data port. To perform the 16-bit slave write operation on the input register, configure these parameters in the MODBUS TCP/IP Slave Write Block Parameters dialog box for input register.
Select the Write Input Register option in the Function parameter.
Set the Input Register Address parameter to
0
Run MODBUS Simulink Model on Master and Slave Devices
1. Connect the Raspberry Pi boards acting as the slave device and the master device to the host computer.
2. On the Hardware tab of the Raspberry Pi MODBUS Slave Simulink model, in the Mode section, select Run on board and then click Build, Deploy & Start.
3. On the Hardware tab of the Raspberry Pi MODBUS Master Simulink model, in the Mode section, select Run on board and then click Monitor & Tune.
4. For the coil register:
In the MODBUS TCP/IP Master Write block, change the position of the slider switches to write data to the coil registers.
In the MODBUS TCP/IP Master Read block, verify the status of the read operation on the coil registers. Observe the lamp displaying the corresponding data obtained from the coil registers. Verify if the lamp status exhibits the correct data written on the coil registers.
5. For the holding register:
In the MODBUS TCP/IP Master Write block, the counter writes data ranging from 0 to 15 to the holding register 0. Pulses are generated with amplitude 1 and written on the holding register 1.
In the MODBUS TCP/IP Master Read block, check the status of the read operation on the holding registers 0 and 1. Observe the counter value increment from 0 to 15 corresponding to the data obtained from the holding register 0 and verify if the same value is displayed in the scope. Observe the pulse output on the holding register 1 and verify if the same value is displayed in the scope.
6. For the input register:
In the MODBUS TCP/IP Master Read block, check the status of the read operation on the input register. Observe the inverted pulse output corresponding to the data obtained from the pulse generator and compare this value with the data obtained from the holding register 1.
7. For the discrete input register:
In the MODBUS TCP/IP Master Read block, check the status of the read operation on the discrete input register. Observe the pulse output corresponding to the data obtained from the free-running counter and compare this value with the data obtained from the holding register 0.
Simulation Output for MODBUS Master Simulink Model
In the Raspberry Pi MODBUS Master Simulink model, on the Hardware tab, click Data Inspector in the Review Results section to view and map the various outputs from the model. This image maps the outputs of the Holding register 0 versus Discrete Input 0 and Holding register 1 versus Input register 0 for better visibility.
Simulink Data Inspector Output for MODBUS Master Simulink Model
Other Things to Try
Use MODBUS slave devices, such as temperature sensors, humidity sensors, and so on, and communicate real-time data to the Raspberry Pi board acting as the master device.
Implement a monitoring system using a PID controller as the slave device and the Raspberry Pi board as the master device.