Configuring Modbus RTU Transmitter with Node-RED

Table of Contents

Introduction

Modbus RTU (Remote Terminal Unit) is a widely used communication protocol in industrial automation, renowned for its simplicity and reliability. It is particularly effective for integrating and managing various types of industrial equipment, such as Variable Frequency Drives (VFDs), Programmable Logic Controllers (PLCs), and Supervisory Control and Data Acquisition (SCADA) systems.

Overall, Modbus RTU serves as a vital communication link in industrial environments, enhancing the interoperability of VFDs, PLCs, and SCADA systems and supporting optimized operation and control.

The NCD Industrial Wireless RS485 Modbus Transmitter

The NCD Industrial Wireless RS485 Modbus Transmitter streamlines the connection to Modbus-enabled devices by converting them into seamless wireless nodes. This integration facilitates easy incorporation into the NCD wireless network, removing the necessity for cumbersome wired connections. The transmitter autonomously retrieves data from Modbus devices at user-configured intervals and transmits the information wirelessly, improving operational efficiency and connectivity.

  • Wireless Communication: Converts RS485 Modbus devices to wireless nodes.
  • Supported Commands:
    • Read Holding Registers (Function Code 03)
    • Read Input Registers (Function Code 04)
  • Power Supply: 5V to 24V DC
  • Communication Settings: Network ID, Power Level, Retries, Baud Rate, RX Timeout, Boot Time, Slave ID, Number of Registers, Function Code

Overview

The following image provides a visual representation of this article. The NCD Industrial Wireless RS485 Modbus Transmitter collects data of interest from field Modbus-RTU elements and subsequently transmits the signal via Digi-Mesh to the Enterprise IIoT Gateway or Industrial IoT Wireless Modem. This data can then be processed by Node-RED and forwarded to various cloud services such as AWS IoT, Ubidots, Blynk Io, or to other industrial protocols like MQTT and OPC-UA. Additionally, the data can be sent to a database, either a local CSV file or a cloud-based storage solution.

NCD Modbus RTU Overview

Requirements

1. The NCD Industrial Wireless RS485 Modbus Transmitter.
2. The Enterprise IIoT Gateway / The Enterprise IIoT Gateway Lite or Industrial IoT Wireless modem (Ethernet/USB).
4. Node-RED Instance (Only apply for Modem usage).

To get Modbus RTU data and configure settings (including Modbus-RTU and device settings) on the NCD Industrial Wireless RS485 Modbus Transmitter, you may need a NCD Gateway or Modem. These devices can be found at the following links:

Enterprise IIoT Gateway

Wireless Ethernet Modem

Wireless USB Modem

Modbus-RTU Pinout

The following image shows the pinout, which serves as a guide for connecting the wireless RS485 Modbus transmitter

NCD Modbus RTU transmitter pinout

The following image shows an example of a connection, including the power supply and a Modbus device.

NCD Modbus RTU Connection with device

Node-RED

Nodered Logo

Node-RED, is a visual programming tool, It provides a browser-based editor that allows creating applications by wiring predefined black box functions (nodes) . Despite its technical nature, Node-RED offers a user-friendly interface. Node-RED is an open-source project under the OpenJS Foundation. This means it’s free to use, without any monthly subscription fees. You can install and run Node-RED locally on your PC, or, in the case of our Enterprise IIoT Gateway, it comes pre-installed and runs as a service.

Sensor Data Processing and Delivery Flexibility

Every NCD sensor / transmitter packet creates a message (object) that flows through connected nodes within Node-RED. Some of these nodes allow you to:

  • Modify the message to suit your specific application needs.
  • Package the data into various protocols like MQTT, Modbus, or OPC-UA.
  • Send the data directly to your preferred cloud platform (Azure, AWS, Ubidots, Losant, etc.).
  • Inject the data directly into an SQL or NoSQL database.
  • Integrate with SCADA systems like Ignition SCADA.

NCD Library

This library handles communication to, and configuration of the NCD Wireless Sensor Line. Including the Industrial Wireless RS485 Modbus Transmitter. It can be used in conjunction with Node-RED to manage the Enterprise Wireless Sensor.

Enterprise IIoT Gateway

All Enterprise Gateway we offer will come with the “@ncd-io/node-red-enterprise-sensors” library by default to allow quick and easy access to Node-Red and the sensor data from our sensors.

Here you can find a complete Getting Started guide for setting up and accessing Node-RED from the Enterprise IIoT Gateway:

NCD Flow

When you accessing Node-RED in the Enterprise IIoT Gateway, the first thing you will see is a basic flow containing two nodes: the “Wireless Gateway” node and the “Debug” node.

Enterprise IIoT Gateway Basic Flow

This is a preconfigured flow, meaning it is ready to receive data from the NCD sensors. When a new NCD sensor connects to the wireless network and sends data, the “Wireless Gateway” node automatically receives and processes the message. It then sends the message through its output terminal, and the message flows through the connection cable to the “Debug” node. This node receives the message and displays it in JSON format inside the “Debug” window on the right side of the Node-RED editor.

Below is a visual representation of the communication process and data flow for display within Node-RED, illustrating the interaction between the NCD sensor and the Enterprise IIoT Gateway.

NCD Modbus RTU Node-RED Flow Example
  1. At the specified intervals, the device will automatically wake up and read the designated Modbus-RTU registers.
  2. The read values will be compiled into a single response and transmitted wirelessly.
  3. The Enterprise IIoT Gateway will receive the sensor data.
  4. Within Node-RED, the Wireless Gateway node will receive, identify, and parse the sensor data. The parsed sensor message will be available in the output terminal node.
  5. The debug node will receive the parsed sensor data and display it in the debug window.

Industrial IoT Wireless to USB/Ethernet Modem

 If you are using your own Gateway or Computer you will need a USB or Ethernet Modem, Install Node-Red, and Install the @ncd-io/node-red-enterprise-sensors library manually. The NCD USB Modem connects to the USB port or of your PC, The NCD Ethernet Modem connects to your local area network and receives sensor data from NCD sensors. Use the NCD Modem for configuration of NCD long range wireless sensors.

Installing Node-RED on PC

To perform the installation process of Node-RED on Windows operating system, you can follow the steps in these articles:

Installing NCD Library from Node-RED Interface

Step 1. Access to Node-RED interface, then, inside the Node-RED flow editor you must go to the main menu (top right) and select the “Manage Palette” option.

Step 2. The “User Settings” window will be displayed, inside “Palette” you must select click on the “Install” tab.

Step 3.- In Search Field enter “@ncd-io/node-red-enterprise-sensors”.

Step 4. Click the “Install” button to start the installation process, a window will appear at the top of the screen, asking you to confirm the installation:

Step 5. Once the process is completed you will see the nodes added to palette.

See the following GIF for a visual demostration of the installation process.

NCD Flow

You need to make the necessary configurations in order to work with the Modem (USB or Ethernet), your PC and Node-RED. It is necessary to configure the “Wireless Gateway” node to specify the physical port through which the PC will communicate with the Industrial IoT Wireless Modem.

To configure this, follow these steps:

  1. Drag and drop the “Wireless Gateway” node into your Node-RED workspace.
  2. Double-click the node to access its properties.
  3. Click the “Add new NCD Gateway config node” button to configure the physical port that your PC will use to communicate with the modem.

 

The process is illustrated in the following GIF.

NCD Node-RED Adding Wireless Gateway Node

NCD Gateway config - config node

The Wireless Gateway node can be configured to communicate with both versions of the IIoT Wireless Modem. Each version requires its own specific configuration, as detailed below.

Serial connection

A serial connection is the simplest and most reliable connection to the wireless network. It doesn’t rely on any network layers like a TCP socket connection does.

TCP Socket Connection

A TCP socket connection allows the PC running Node-Red to be in an out of the way location while still receiving the sensor data from a Network Modem placed in a more central location. These communications heavily rely on the network that the Modem are on. It is recommended to put the PC and the Ethernet Modem on the same network or have an IT team to oversee the communications if this option is used.

NCD Node-RED Wireless Gateway Configutation node
NCD Node-RED Wireless Gateway Configuration Node TCP
Properties

1. Name

  • This allows you to enter a human readable name for identifying this config node
  • If no name is chosen it will default to the text for the Serial Port field
  •  

2. Modem Type

  • This allows you to choose between USB (Serial) or TCP (Ethernet/Network)
  • If you are using an USB Modem you will want to choose USB (Serial)
  •  

3. Serial Port

  • Select the correct serial port that your USB Modem/Wireless Module is mounted to.

4. Baud Rate

  • Do not choose anything other than 115200 unless you have changed the baud rate on the Modem/Module manually.

2. Modem Type

  • This allows you to choose between USB (Serial) or TCP (Ethernet/Network)
  • If you are using an Ethernet Modem you will want to use TCP (Ethernet/Network)
  •  

3. TCP Port

  • Select the TCP Port that the modem is listening on
  • The default TCP Port for our Ethernet Modems is 2101
  •  

4. IP Address

  • The IP address of the Ethernet Modem on the network
  • It is recommended to have the Ethernet Modem use a Static IP address so that this value never needs to change.
  •  

5. Network ID

  • Network IDs allow you to have multiple groups of sensors, repeaters, and Gateway/Modems in a single area without interfering with each other
  • The default value is 7FFF
  • We do not recommend changing this value without specific purpose
  • Make sure to change the Network ID of the sensors you want to be on this network BEFORE changing the Network ID here.
  •  
  •  

6. Add RSSI to incoming packets

  • Selecting this box will add a signal strength indicator to all incoming comms
  • Selecting this option will have a negative, but generally negligible effect on the Wireless Communication throughput
  •  

Once you have completed configuring the “Wireless Gateway” node, click the “Add” and “Done” buttons. Then you could add a debug node and connect it, in order to see the incoming messages. Finally, click the “Deploy” button to save and apply the changes.

If the configurations are correct and the “Wireless Gateway” node successfully establishes communication with the IIoT Wireless Modem, a message will appear in the “debug” window displaying the modem’s MAC address, as shown in the following image:

NCD Node-RED Basic Flow Modem

An overview of the Industrial IoT Wireless Modem architecture is provided in the following image:

NCD Modbus RTU Node-RED Flow Example Modem

Linking NCD device to Node-RED

Once you power on your Industrial Wireless RS485 Modbus Transmitter device, it will automatically send the RUN and SENSOR DATA messages to all potential NCD Wireless receivers (Gateways or Modems) in broadcast mode.

RUN Message

A RUN message received simply indicates that the sensor indicated in the msg.payload.mac is powered up and communicating with the wireless network. An example RUN message is showing in the next image:

NCD Modbus RTU RUN Message

Wireless Gateway Sensor Data Message

All messages from the Wireless Gateway Node will come through as Javascript Objects and are output by default as JSON representations when viewed or sent over most protocols. In Node-Red the primary object to reference the message will be msg. You can access sub-properties using msg.payload or msg.payload.nodeId etc.

Sensor data is the message that will come from a Wireless Gateway Node after RUN message. These messages can be discerned from other message types by the topic which will always be “sensor_data” to the right is an example of a Sensor Data message.

  • nodeID
    • A user configurable parameter allowing a user to input a simple id for a particular sensor
    • For a unique identifier it is recommended to use the “addr” property as it is tied to the individual sensor’s wireless module and will always be unique
  • firmware
    • This identifies the firmware version of the sensor sending the packet
  • battery
    • The current voltage level of the batteries at the time of transmission
    • The batteries that come with the sensors drop off quickly once they reach 2.6 volts
  • battery_percent
    • The current battery percent at the time of transmission
  • counter
    • The number of transmissions since boot or counter rollover
    • The counter will rollover after a counter value of 255
  • sensor_type
    • The machine identifiable type of the sensor
    • It is recommended to use this property to dictate dashboard generation and/or data integrity checks
  • sensor_data
    • Object containing all sensor data related to this sensor
  • sensor_name
    • Human Readable Sensor Type Identifier
  • type
    • An easily passed message type declaration
    • This property will be a duplicate of msg.topic
  • addr
    • The unique identifier of the sensor that transmitted the data
  • received
    • Epoch indicator of when the data was received by Node-Red
  • original
    • Auxiliary information on the packet and underlying protocol
  • modem_mac
    • The unique identifier of the Gateway/Modem that received the data
    • Primarily used to tie locations/projects to sensors/sensor data
				
					{
  "topic": "sensor_data",
  "payload": {
    "nodeId": 0,
    "firmware": 3,
    "battery": "3.28",
    "battery_percent": "98.56",
    "counter": 26,
    "sensor_type": 539,
    "sensor_data": {
      "subdevice_type": 0,
      "number_of_registers": 0,
      "status_24_31": 0,
      "status_16_23": 0,
      "status_8_15": 0,
      "status_0_7": 0,
      "data": [
          0: 0,
          1: 0,
          2: 0,
          3: 0,
          4: 0,
          5: 0,
          6: 0,
          7: 0,
          9: 0,
          10: 0
          ...
       ]
    },
    "sensor_name": "RS-485 Modbus Wireless Converter",
    "type": "sensor_data",
    "addr": "00:13:a2:00:42:37:73:52",
    "received": 1721424191583,
    "original": {...},
    "modem_mac": "00:13:A2:00:41:F5:2C:D3"
  },
  "time": 1721424191584,
  "_msgid": "d7f3341ef39225df"
}

				
			

msg.payload.sensor_data - Breakdown

  •  sub_device_type
    • Byte to indicate what kind of User-defined Modbus RTU device it’s connected to.
  • number_of_registers
    • The number of registers read, up to a maximum of 32 registers in one operation.
  • status_0_to_31
    • Represents the status of the registry query: 0 indicates no error, while 1 indicates an error.
  • data
    • A variable-length array, it contains the value obtained from the Modbus-RTU registers as requested by the user.

Wireless Device Node - Configuration

Our Node-Red library lets you configure any of sensor easily using the “Wireless Device” node, simply by adjusting a few fields in your Node-RED flow and triggering a configuration update either through the CFG button on the sensor or waiting for a FLY message sent.

To enable the NCD Industrial Wireless RS485 Modbus Transmitter to communicate with the Modbus RTU device via RS485, it is essential to configure the parameters using Node-RED. Begin the configuration process by dragging the “Wireless Device” node into the workspace.

NCD Modbus RTU Wireless Device

You can edit a Wireless Device node by double clicking the node once it is placed in a flow.

Example - Gateway

NCD Wireless Device gateway example

Example - Modem USB

NCD Wireless Device COM example

These configurations are responsible for configuring the Wireless Device node and how Node-RED should interact with the Industrial Wireless RS485 Modbus Transmitter.

  • Name
    • Arbitrary name of this node
    • Primarily used for easy identification in the flow
  • Serial Device*
    • The primary communication interface used to receive data from the sensor
    • Clicking the edit icon will take you to a submenu to edit the particulars of the device chosen in the Serial Device dropdown
    • To add a new configuration option you will want to choose “Add new ncd-gateway-config…” from this dropdown
  • Serial Device for Config
    • A secondary communication interface used to configure sensors without losing sensor data
    • If no option is selected the communication interface selected under Serial Device will be used for configuration
    • A second modem will be required to make full use of this option
  • Mac Address
    • Using this field allows you to choose a specific sensor that corresponds to this Wireless Device node
    • Leaving this field blank allows any sensor of the type chosen under the Sensor Type field to correspond with this Wireless Device Node
    • A search icon on the right can be clicked to search for Wireless Devices that the Serial Device has heard from to auto fill information for that device
  • Sensor Type*
    • This field limits the sensors that correspond to this Wireless Device node to a particular type of sensor
  • Auto Config
    • Checking the box next to this field tells Node-Red that the sensor(s) corresponding to this Wireless Device node should be configured when put into Configuration Mode manually
  • OTF Config
    • Checking the box next to this field AND having Auto Config checked tells Node-Red that the sensor(s) corresponding to this Wireless Device node should be configured when a FLY message is detected

*Required

Example for using with Enterprise IIoT Gateway.

Example for using with Modem USB in Windows PC.

NCD Modbus RTU Node-RED Wireless Device Config Mode

Configuration

These configurations are transmitted to the sensor when Node-Red configures the sensor. Node-Red will need to configure the sensor in order for these settings to go into effect.

General Sensor Configuration

Modbus RTU Configurations

  • Wait for Network Formation
    • This option sends three commands to all local devices on the wireless network to help form the mesh network
  • Destination Address
    • This option will tell the sensor to ONLY send its data to a single receiver.
  • Network ID
    • This option allows you to change the Network ID that the sensor will use to report its data while in RUN module
    • You will need to make sure that a Gateway or Modem is configured to use this network ID in order to receive sensor data from it after configuration
  • Node ID and Delay
    • Node ID
      • Node ID allows you to set an easy to understand unique ID for a particular sensor
    • Delay
      • Delay is the time in seconds between sensor data reading transmissions
  • Power
    • This option allows you to set the transmission power level of the sensor
  • Retries
    • Sets the maximum number of retries the sensor will attempt before considering a packet transmission failed
  1. Set Stay On Mode:
    • Parameter to keep the device on, preventing it from entering sleep mode.
  2. Set Baud Rate:
    • Configure the communication speed for the RS485 port.
  3. Set RX Timeout:
    • Define the RS485 read/write timeout in milliseconds. By default, RS485 devices are in read mode. The timeout ensures the device waits for the specified duration before switching modes.
  4. Set Boot Time:
    • Set the boot time in seconds to allow the sensor to initialize before reading data. For example, if the user wants to read the device every 30 seconds but requires a 2-second boot time, this setting will be configured accordingly.
  5. Set Slave ID:
    • Assign the Modbus slave ID. Each Modbus slave device requires a unique converter, as this device cannot read from multiple slaves simultaneously.
  6. Set Sub Device Type:
    • User-defined byte to indicate the type of Modbus RTU device being connected. This is useful for decoding the message.
  7. Set Number of Read Retries:
    • Number of retries for Modbus RTU register queries (Range: 1 to 3).
  8. Set Function Code:
    • Choose between Function Code 0x03 (Read Holding Registers) and Function Code 0x04 (Read Input Registers).
  9. Set Read Registers:
    • Specify the number of registers to read, up to a maximum of 32 registers in one operation.
    • Register Read Field: Set the individual Register to read.

To configure the parameters of the Industrial Wireless RS485 Modbus Transmitter, the first step is to set the Modbus parameters within the “Wireless Device” node. In this article, we will use the following configurations to illustrate the process.

Set Baud Rate: We will modify the RS485 baud rate to 11200.

NCD Modbus RTU Node-RED Configuration example 1

Set Slave ID: We will set the “Slave ID” as 13.

NCD Modbus RTU Node-RED Configuration example 1

Set Read Parameter: We will set the “0x04 – Read Input Register” function code. This function code is used to read from 1 to 125 contiguous input registers in a remote device.

NCD Modbus RTU Node-RED Configuration example 3

Set Read Registers: We will set the number of registers to read, in this case 6. You can use a drop-down menu.

NCD Modbus RTU Node-RED Configuration example 4

Registers: We will set the address of each of the registers of interest.

NCD Modbus RTU Node-RED Configuration example 5

Once you have selected the checkboxes for each parameter you wish to configure and entered or selected the appropriate values, you must click the “Done” button and then press the “Deploy” button to save and apply the changes.

NCD Modbus RTU RS485 parameters

Configuration modes

After configuring the RS485 parameter settings within the “Wireless Device” node using Node-RED, now you must either wait or activate the configuration mode on the Industrial Wireless RS845 Modbus Transmitter.

The Industrial IoT RS485 Modbus transmitter offers two methods for entering configuration mode: manual and automatic. You may use either method to configure any parameter of the sensor. This document provides detailed instructions for activating both configuration modes.

Manual configuration

Industrial IoT RS485 Modbus Transmitter can be manually put into configuration mode by pressing and releasing the Reset button on the transmitter and then immediately holding the CFG button on the transmitter for 2-15 seconds.

If the Wireless Gateway node in Node-Red is set to configuration mode then you will receive a sensor_mode message from the sensor indicating it is now in Configuration Mode and can be configured. Node-Red will begin configuring that sensor according to the settings entered on the Wireless Device node.

Wireless Gateway Node in Configuration Mode with the Mode Toggle button highlighted.
Logic Diagram for Manual Configuration of Sensors
Automatic configuration

The Industrial Wireless RS485 Modbus Transmitter supports configuration while in Run Mode. We call this configuration OTF or On the Fly. This configuration is initiated by a sensor_mode message from the Sensor with the mode of FLY. These messages are sent over the standard Run Mode wireless network so there is no need to change the Wireless Gateway nodes mode or manually manipulate the buttons on the sensor.

This FLY message tells Node-Red that the sensor is ready for configuration changes. Node-Red has two seconds to respond with a configuration or extend this temporary configuration mode.

If you have a Wireless Device node with Auto Config and OTF Config corresponding to the sensor that sent the FLY message then Node-Red will begin configuring that sensor according to the settings entered on the Wireless Device node.

Logic Diagram for FLY Mode Configuration of Sensors

Configurin Modbus RTU parameters - FLY Mode

Now we are going to perform an example of the process of setting Modbus RTU parameters, as well as interpreting the Sensor Data Message, in order to process it later through a function node.

FLY mode allows for parameter settings to be adjusted without needing to place the transmitter in “configuration mode.” Once the check-boxes are enabled and the “Done” and “Deploy” buttons are clicked to save and apply the changes, it is necessary to wait for the next incoming message from the Modbus RTU transmitter. We will first see the message “sensor_data,” followed by the message “sensor_mode” indicating that the system is in “FLY” mode.

NCD Modbus RTU Node-RED Wireless Device node - Debug configuring - 1

By clicking on the message within the debug window, you can expand it to view the complete details. Inside the FLY message, you will find the MAC address of the device sending the message, the device type (NCD), the node ID, the FLY mode itself, and a timestamp indicating when the message was sent.

NCD Modbus RTU Wireless Device FLY message

Gateway/modem responds with a command, this is the gateway/modem telling the sensor that it should go into configuration mode.

NCD Modbus RTU Wireless Device - Config enter OTF

Sensor/transmitter responds with an OTN package. This lets the gateway know that the sensor is in config mode and ready for config commands. The sensor will stay in this mode until ~60  seconds have passed OR the modem sends a command to end the configuration.

NCD Modbus RTU Wireless Device OTN message

At this point the Gateway/Modem will send all of the configuration commands. For each configuration command sent by the gateway or node, the Modbus RTU transmitter responds with an “ACK” acknowledgment message.

NCD Modbus RTU Wireless Device ACK message

In this example, we configured five parameters, resulting in five “ACK” acknowledgment messages.

NCD Modbus RTU Wireless Device Configuring Process

Once all configurations have been made you will see a message “Config Results” where you can see the results of the configuration of the parameters. Then the modem will send a command, this tells the modbus rtu transmitter to go to sleep and resume normal operation.

NCD Modbus RTU Wireless Device Configuring Process - results

The Modbus RTU Transmitter responds with an OTF message, this indicates to the modem that it is no longer accepting configuration commands.

NCD Modbus RTU Wireless Device OTF message

The following image is a screenshot of the entire procedure, displaying the incoming messages within the debug window.

NCD Modbus RTU Node-RED Wireless Device Configuring - complete

The following GIF image visually represents this process, demonstrating the automatic configuration of five RS485 parameters for the Modbus RTU transmitter.

Once the configuration process is complete, the next time the Wireless Modbus RTU transmitter sends data, you will see output similar to the following. You may notice that the configurations have been successfully applied. In this case, you can observe that there are no errors in the register readings, and the Wireless Modbus RTU Transmitter is accessing and transmitting six registers along with their current values:

Register in DEC
NCD Modbus RTU new payload after configuration
Registers in HEX
NCD Modbus RTU new payload after configuration - HEX

Using function node to filter incoming data

Once you have configured the Wireless Modbus RTU Transmitter and are receiving data from the Modbus device registers connected to the transmitter, you may want to process these registers individually. In Node-RED, there are several approaches for this. One approach is to use the “Function” node, which allows you to add JavaScript functions to access and manipulate the data. Another approach is to use individual nodes such as the “Switch” and “Change” nodes. Below is an example demonstrating how to focus on and manipulate register data using these two approaches.

Sub device type - FUNCTION NODE

To use the Function node, locate it in the nodes palette. Within the “Function” group, click and hold the “Function” node, then drag it to your workspace, as shown in the following image:

NCD Modbus RTU Node-RED Adding Function Node

Next, connect the output of the “Wireless Gateway” node to the input of the “Function” node, as shown in the following image:

NCD Modbus RTU Node-RED Adding Function Node - adding

At this point, you may no longer need to observe the incoming and processed messages from the “Wireless Device” node. To manage this, you can individually disable the display of messages from the “Debug” nodes by clicking the virtual button on the right side of each “Debug” node. This will prevent the display of incoming messages from the “Wireless Device” node in the debug window.

NCD Modbus RTU Node-RED Adding Function Node - disable debug node
Nodered Deploy Button

If you wish to clear the current messages in the “Debug” window to focus on new ones, you can do so by clicking the “Clear messages” icon within the “Debug” tab.

NCD Modbus RTU - Clear Debug Window
NCD Modbus RTU - Clear Debug Window

In this example, we will focus on the “sub_device_type” message. To do this, expand the incoming message from the Wireless Modbus RTU Transmitter and locate the “sub_device_type” property.

NCD Modbus RTU complete message

In the debug window, positioning the mouse cursor over the property of interest will reveal three buttons. You can use the first button to copy the “Path” of the property. We will explain the purpose of copying this property later.

NCD Modbus RTU copying path sub device type

Double-click on the “Function” node to access its properties. You will see a text input field where you can add and execute JavaScript code in the “On Message” window. Paste the “Path” that was copied in the previous step. You should see the string payload.sensor_data.subdevice_type.

NCD Modbus RTU adding path to function node

First, we need to prepend “msg.” to the “path” to complete the message. Next, add a conditional “if” statement to evaluate whether the value of the property msg.payload.sensor_data.subdevice_type is equal to “0”. If the condition is true, the code within this conditional will be executed.

NCD Modbus RTU adding conditional to function node

Access Registers - FUNCTION NODE

Once we have filtered the messages by the type of Modbus RTU device, the next step is to decode the registers. This allows us to perform operations or convert the data into a human-readable format. In this example, we will add some functions to illustrate how this can be done. You are encouraged to add your own functions or operations on the registers using JavaScript operators as needed.

We will use the same procedure as in the previous step to access the properties within the message. In this case, we will focus on accessing the values of the Modbus registers. As before, copy the “path” of the register of interest and paste it into the Function node, prepending “msg.” as shown in the following image:

NCD Modbus RTU Copy path registers
NCD Modbus RTU Paste path registers
NCD Modbus RTU Copy path registers
NCD Modbus RTU Paste path registers

We may want to filter incoming messages that correspond only to the sensors/transmitters, for this we can add a conditional “if(msg.topic === “sensor_data”)”.

Next, we create new properties within msg.payload to identify the input registers. Each register value is assigned to these new properties, as illustrated in the following image:

NCD Modbus RTU Process incoming data example

We will disable the messages from the debug node connected to the output of the “Wireless Gateway” node and focus only on the messages that pass through the “Function” node. In this case, this corresponds to the “Debug 4” node.

NCD Modbus RTU Process incoming data example flow

The next time a new message arrives from the Wireless Modbus RTU Transmitter, we will be able to observe registers 1, 2, and 3 as follows:

NCD Modbus RTU Process incoming data example debug window

You can modify the property identifier, add text, or perform operations on the value of individual records, depending on your application. An example is shown in the following image:

NCD Modbus RTU Process incoming data example human-readable code

Remember that each time you modify the code to condition or decode the Modbus registers within the Function node, you must click the “Deploy” button to save and apply the changes.

NCD Modbus RTU Process incoming data example human-readable

Sub device type - SWITCH NODE

As mentioned earlier, Node-RED is a visual programming tool that allows us to create logic for our application without requiring extensive programming knowledge. In this example, we will implement the same logic as used in the “Function” node, but this time using “Switch” and “Change” nodes.

As with the Function node, to use the “Switch” node, click and hold the node, then drag it to your workspace.

Node-RED Switch node

We need to connect the output of the “Wireless Gateway” node to the input of the “Switch” node, as shown in the following image:

NCD Modbus RTU switch flow filter by device

To access the properties of the “Switch” node, double-click on it. First, add a filter that allows data to flow through the “Switch” node only if the property msg.topic is equal to “sensor_data”. This indicates that the incoming message is from a sensor or transmitter. This setup is illustrated in the following image:

NCD Modbus RTU switch flow filter by device

Next, we will add a new “Switch” node and connect it to the output of the first “Switch” node. This new node will be used to filter the incoming messages based on the sub-device type of the Modbus RTU. This setup is particularly useful when you have multiple Wireless Modbus RTU Transmitters linked, as it allows you to identify the originating device of the messages.

NCD Modbus RTU switch flow filter by device


Similar to the approach used with the “Function” node, we will copy the path corresponding to the “subdevice_type” property and paste it into the property field of the “Switch” node. We will then add three rules using the “Add rule” button located at the bottom left of the “Switch” node. Select the “==” operator and specify the value to compare against. In this case, we are simulating three Wireless Modbus RTU Transmitters, each assigned a “subdevice_type” property with values 0, 1, and 2. As a result, incoming messages from device “0” will flow through output 1 of the “Switch” node, messages from device “1” will flow through output 2, and so on.

NCD Modbus RTU filter with switch node

By clicking the “Done” button, you should be able to observe something similar to the following, noting that the “Switch” node now has three output terminals.

NCD Modbus RTU Node-RED Adding Switch node

Access Registers - CHANGE NODE

Now that we have the logic to filter incoming messages to correspond only to data from the transmitter, as well as a filter for each type of Modbus RTU device connected to the network, the next step is to use the “Change” node to access and decode the Modbus RTU registers of interest. To use the “Change” node, click on it and drag it to your workspace.

NCD Modbus RTU Adding Change Node

We will connect output 1 of the “Switch” node to the input of the “Change” node. This setup ensures that the “Change” node will only access properties and messages from the Modbus RTU device of type “0” as specified by the user. Our flow will then appear as follows:

NCD Modbus RTU Adding Change Node

We will access the properties of the “Change” node. First, we will “copy” all values from the “msg.payload” message to a new property named “msg.data” Next, we can delete the “msg.payload” property, as we will later restructure the content of “msg.payload.”

It is possible to use JSONata functions within the “Change” node. To utilize JSONata in the “Change” node, select the “JSONata” type from the dropdown menu located on the left side of the value field. Using JSONata allows us to add operators for decoding registers within this node. In this case, we will only concatenate text (units) corresponding to each register, using the “&” operator to concatenate the text.

NCD Modbus RTU Adding Change Node - properties
NCD Modbus RTU using change node and JSONata

Now, you should add and connect a “Debug” node to the output of the “Change” node in order to observe the messages from this node within the “Debug” tab, as shown in the following image:

NCD Modbus RTU Adding Change Node

To save and apply the changes, click the “Deploy” button. Once a new message arrives and corresponds to “subdevice_type -> 0,” you will be able to observe the following message in the “Debug” tab.

Summary

The purpose of this article is to visually and intuitively demonstrate how to perform the physical connection, including the pinout, as well as the configuration of some RS485 parameters for the “Wireless Modbus RTU Transmitter” device using Node-RED and our library “@ncd-io/node-red-enterprise-sensor.” Subsequently, we illustrated how to create basic logic for filtering, accessing, and processing incoming messages from the Wireless Modbus RTU Transmitter in Node-RED using either the Function node or the Switch and Change nodes. The goal of this article is to provide you with the foundational knowledge needed to customize the flows according to your application.

 

Thank you, NCD Team.

Share this on:
You Might Also Like: