Main Content

MODBUS TCP/IP Communication Between Client and Server Devices Using NVIDIA Jetson TX2 Hardware

This example shows how to use the MATLAB® Coder™ Support Package for NVIDIA® Jetson® and NVIDIA DRIVE® Platforms to implement MODBUS® TCP/IP communication between MODBUS client and server devices. It also shows how to communicate between the two devices in four modes of operation, Client Read, Client Write, Server Read, and Server Write.

This example uses two Simulink models, namely NVIDIA Jetson TX2 MODBUS Client and NVIDIA Jetson TX2 MODBUS Server, which together utilize different modes of operation for the client and server devices.

  • Client Read — Client device reads data from the server device register(s) over TCP/IP

  • Client Write — Client device writes data to the server device register(s) over TCP/IP

  • Server Read — Server device reads data from the server device register(s) over TCP/IP

  • Server Write — Server device writes data to the server device register(s) over TCP/IP

The NVIDIA Jetson TX2 MODBUS Server model is deployed on one NVIDIA Jetson TX2 board, while the NVIDIA Jetson TX2 MODBUS Client model runs in external mode on the other NVIDIA Jetson TX2 board.

The following table categorizes the server device registers referenced by the MODBUS client and server devices.

Register Type      |   Register Size   |   Allowed Client 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

Tools, libraries, and environment variables on the target board for the compilers and libraries. For more information, see Install and Setup Prerequisites for NVIDIA Boards.

  • Two NVIDIA Jetson TX2 hardware boards.

  • Ethernet crossover cable to connect the target boards to the network.

Configure Client Model and Calibrate Parameters

This support package provides a preconfigured model for the MODBUS client.

To open the model, run this command in the MATLAB® Command Window.

open_system('nvidia_jetson_modbus_client')

The default values for the Simulink client model are preconfigured in this example depending on the server register size and the allowed client operations. The client write operation is valid on coil and holding register(s), whereas the client read operation is valid on all the register types.

For the client mode of operation, configure the IP address of the client NVIDIA Jetson TX2 board in Configuration Parameters > Hardware Implementation > Target hardware resources > Board Parameters.

Configure these parameters in Configuration Parameters > Hardware Implementation > Target hardware resources > Modbus properties.

  1. Set Communication Interface to TCP/IP.

  2. Set Mode to Client.

  3. Configure the server device IP port number in the Remote Server IP port number parameter. In this example, the parameter is set to 502.

  4. Set Received timeout (ms) to 100 seconds.

Configure MODBUS TCP/IP Client 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 Client Write blocks.

Configure these parameters in the MODBUS TCP/IP Client Write Block Parameters dialog box to write data from the slider switches to the coil registers.

  1. Enter the unique IP address of the server device in the Server address parameter. In this model, the parameter is set to 192.168.0.2.

  2. Select the Write Multiple Coils option in the Function parameter.

  3. To notify the client to write data to two coil registers, specify the address of the first coil in the Coil Address parameter. The default value is 0.

  4. Enter 3 in the Number of Coils parameter. The default positions of the slider switches are Off, On, and On.

Configure MODBUS TCP/IP Client Write Blocks for Holding Registers

To perform the 16-bit client 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 Client Write block.

Configure these parameters in the MODBUS TCP/IP Client Write Block Parameters dialog box to write data from the free-running counter and pulse generator to the holding registers.

  1. Enter the unique IP address of the server device in the Server address parameter. In this model, the parameter is set to 192.168.0.2.

  2. Select the Write Multiple Holding register option in the Function parameter.

  3. To notify the client 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.

  4. Enter 2 in the Number of Holding registers parameter.

Configure these parameters in the Counter Free-Running Block Parameters dialog box.

  1. Enter 4 in the Number of bits parameter for the counter to count from 0 to 15.

  2. Set the Sample time to 0.1 seconds.

Configure these parameters in the Pulse Generator Block Parameters dialog box.

  1. Select the Sample based option in the Pulse type parameter.

  2. Set the Sample time to 0.1 seconds.

Configure MODBUS TCP/IP Client 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 Client Read Block Parameters dialog box.

  1. Enter the unique IP address of the server device in the Server address parameter. In this model, the parameter is set to 192.168.0.2.

  2. Select the Read Multiple Coils option in the Function parameter.

  3. To notify the client to read data from coil registers, specify its address in the Coil Address parameter. The default value is 0.

  4. Set Number of Coils to 3.

  5. Set the Sample Time parameter to 0.1.

Configure MODBUS TCP/IP Client Read Block for Holding Registers

To perform the 16-bit read operation on the holding registers, configure these parameters in the MODBUS TCP/IP Client 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 Client Read block. The holding register 0, holding register 1, and the Status outputs can be viewed in the Scope.

  1. Enter the unique IP address of the server device in the Server address parameter. In this model, the parameter is set to 192.168.0.2.

  2. Select the Read Multiple Holding registers option in the Function parameter.

  3. To notify the client to read data from coil registers, specify its address in the Holding Register Address parameter. The default value is 0.

  4. Set Number of Holding registers to 2.

  5. Set the Sample Time parameter to 0.1.

Configure MODBUS TCP/IP Client 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 Client Read Block Parameters dialog box.

  1. Enter the unique IP address of the server device in the Server address parameter. In this model, the parameter is set to 192.168.0.2.

  2. Select the Read Discrete input option in the Function parameter.

  3. To notify the client to read data from one discrete input register, specify its address in the Discrete Input Address parameter. The default value is 0.

  4. Set the Sample Time parameter to 0.1.

Configure MODBUS TCP/IP Client 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 Client Read Block Parameters dialog box.

  1. Enter the unique IP address of the server device in the Server address parameter. In this model, the parameter is set to 192.168.0.2.

  2. Select the Read Input register option in the Function parameter.

  3. To notify the client to read data from one input register, specify its address in the Input Register Address parameter. The default value is 0.

  4. Set the Sample Time parameter to 0.1.

Configure Server Model and Calibrate Parameters

This support package provides a preconfigured model for the MODBUS server.

To open the model, run this command in the MATLAB Command Window.

open_system('nvidia_jetson_modbus_server')

The default values for the Simulink server model are preconfigured in this example.

For the server mode of operation, configure the IP address of the server NVIDIA Jetson TX2 board in Configuration Parameters > Hardware Implementation > Target hardware resources > Board Parameters.

Configure these parameters in Configuration Parameters > Hardware Implementation > Target hardware resources > Modbus properties.

  1. Set Communication Interface to TCP/IP.

  2. Set Mode to Server.

  3. Configure the client device IP port number in the Local IP port number parameter. In this example, the parameter is set to 502.

  4. 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 Server Read Block for Coil Registers

To perform the 1-bit server read operation on the coil registers, configure these parameters in the MODBUS TCP/IP Server Read Block Parameters dialog box for each coil register.

  1. Select the Read Coil option in the Function parameter.

  2. Set the Coil Address parameter to 0.

  3. Set the Sample Time parameter to 0.1.

Similarly, configure the other two MODBUS TCP/IP Server 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 Server Read block. The NVIDIA Jetson TX2 GPIO Write pins are mapped to the server coil register address. Configure the NVIDIA Jetson TX2 Board and GPIO number parameters.

Configure MODBUS TCP/IP Server Read Block for Holding Registers

To perform the 16-bit server read operation on the holding registers, configure these parameters in the MODBUS TCP/IP Server Read Block Parameters dialog box for the free-running counter and the pulse generator output.

  1. Select the Read Holding register option in the Function parameter.

  2. Set the Holding Register Address parameter to 0.

  3. Set the Sample Time parameter to 0.1.

Similarly, configure the other MODBUS TCP/IP Server Read blocks and set the Holding Register Address parameter to 1, while the other parameters remain the same.

Configure MODBUS TCP/IP Server Write Block for Discrete Input Registers

Double-click the subsystem connected to the Data output port of the MODBUS TCP/IP Server 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 server write operation on the discrete input register, configure these parameters in the MODBUS TCP/IP Server Write Block Parameters dialog box for the discrete input register.

  1. Select the Write Discrete Input option in the Function parameter.

  2. Set the Discrete Input Address parameter to 0.

Configure MODBUS TCP/IP Server Write Block for Input Registers

Double-click the subsystem connected to the Data output port of the MODBUS TCP/IP Server 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 server write operation on the input register, configure these parameters in the MODBUS TCP/IP Server Write Block Parameters dialog box for input register.

  1. Select the Write Input Register option in the Function parameter.

  2. Set the Input Register Address parameter to 0

Run Model on Client and Server Devices

1. Connect the NVIDIA Jetson TX2 boards acting as the server device and the client device to the host computer.

2. On the Hardware tab of the NVIDIA Jetson TX2 MODBUS Server Simulink model, click Build, Deploy & Start.

3. On the Hardware tab of the NVIDIA Jetson TX2 MODBUS Client Simulink model, click Monitor & Tune.

4. For the coil register:

  • In the MODBUS TCP/IP Client Write block, change the position of the slider switches to write data to the coil registers.

  • In the MODBUS TCP/IP Client 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 Client 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 Client 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 Client 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 Client 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 Client Simulink Model

In the NVIDIA Jetson TX2 MODBUS Client 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 client Simulink Model

Other Things to Try

  • Use MODBUS server devices, such as temperature sensors, humidity sensors, and so on, and communicate real-time data to the NVIDIA Jetson TX2 board acting as the client device.

  • Implement a monitoring system using a PID controller as the server device and the NVIDIA Jetson TX2 board as the client device.

See Also

Blocks