The Modbus protocol is often used in industrial sites for data collection and remote control of various devices. Many developers in the Node-RED community have provided various types of Modbus nodes for the Modbus protocol, including "node-red-node-arc-modbus" for edge acquisition and "node-red-contrib-modbus" (https://flows.nodered.org/node/node-red-contrib-modbus), a general Modbus protocol stack. Next, we will demonstrate the access of Modbus IO devices to the Node-RED gateway through the general Modbus protocol stack.

Related article:
Communication protocol: ModBus RTU protocol and ModBus TCP protocol
3 steps to learn the TCP/IP protocol of the communication protocol

As with the previous way of adding nodes, first select the menu, enter "Node Management", enter the node name "node-red-contrib-modbus", select the corresponding node after the search is completed, click Install, and a series of Modbus operation nodes will appear after the installation is completed.
This article mainly describes the static operation nodes "Modbus-Read", "Modbus-Write", "Modbus-Response", and the dynamic operation nodes "Modbus-Flex-Connector", "Modbus-Flex-Getter", and "Modbus-Flex-Write". The above nodes can be used to realize the collection and control of remote IO data.
"Modbus-Read", the gateway acts as the host, configures the static connection parameters and static Modbus parameters, and reads the specified register data of the specified Modbus slave through the network, serial port, and the configured protocol (Modbus RTU and Modbus TCP).
"Modbus-Write", the gateway acts as the host, configures the static connection parameters and static Modbus parameters, and configures the specified register data of the specified Modbus slave through the network, serial port, and the configured protocol (Modbus RTU and Modbus TCP) and the parameters passed in.
Both "Modbus-Read" and "Modbus-Write" need to configure the gateway to connect to the device in advance before use. The left picture shows the configuration through the serial port query, and the right picture shows the configuration through the network query;


The "Modbus-Read" node needs to configure the device address (Unit-id). I configure it as 104 here, and you can also not enter it. If you do not enter it, the default device address configured during connection will be used. You also need to configure the function code used. I use the integer value of AO as an example (querying the register table of the IO device can get AO stored in 40100-40103), so FC (function code) is configured as 3 (read holding register). If AI is read, the configuration FC is 4 (read input register, query the register table of the IO device to get AO stored in 30100-30103), configure the node address to 100, the number of registers to 4, and the automatic polling to 10s (you can adjust the time base to milliseconds, seconds, minutes, and hours according to your needs), and select the connection parameters configured in the previous step.

"Modbus-Write" and "Read" both have address, register address, quantity, and connected server. The same parameter configuration method is the same and will not be explained separately. The write node also needs to pass in data, as shown in the right figure, passing in 4 register parameters.


"Modbus-Response" is used to output the return data of the front-end Modbus node and display it in the Node-RED console. It is generally not used.
"Modbus-Flex-Connector" is a node used to dynamically establish a connection with a Modbus device. Using this node requires passing in the parameters required for the connection and configuring the space for storing connection information (configure a new server node, IP and port can be configured arbitrarily, and the remaining parameters are adjusted according to actual needs. Here, TCP connection is used and communication is carried out through the Modbus TCP protocol, and the timeout period is 1000). The remaining parameters can be passed in through the front-end node.

Add the "inject" node, add the "payload" object, enter the JSON data {"connectorType":"TCP","tcpHost":"192.168.88.35","tcpPort":502}, click deploy, and you can manually trigger the connection;


After the connection is successful, the debug log interface outputs the following data;

"Modbus-Flex-Getter" is used to dynamically query IO device parameters. It is necessary to pass in the device address, function code, first address register, and number of registers. Currently, the control supports 01, 02, 03, and 04 function codes. Like dynamic connection, add an "inject" to the front end of the node to pass in Modbus request parameters;


"Modbus-Flex-Write" is used to dynamically write IO device data. It is necessary to pass in the device address, function code, first address register, number of registers, and operation data. Currently, the control supports single operation 05 and 06 and continuous operation 15 and 16 function codes. Like other dynamically connected nodes, add an "inject" to the front end of the node to pass in Modbus request parameters and data;

