Node-RED with Modbus Tutorial (part 1)

4 min read 1 year ago
Published on Aug 02, 2024 This response is partially generated with the help of AI. It may contain inaccuracies.

Table of Contents

Introduction

This tutorial will guide you through creating a simple example using Node-RED and Modbus via TCP/IP. We will set up a Modbus server, read from and write to registers, and utilize Node-RED for real-time data handling. This is particularly useful for those looking to integrate industrial automation systems or IoT applications.

Step 1: Setting Up Node-RED with Modbus Server

  1. Add a Modbus server node to your Node-RED flow:

    • Double-click the node to configure it.
    • Set the port to the default Modbus TCP port, typically 502.
    • Enable a listener to monitor outputs from the server.
  2. Inject a trigger:

    • Use an inject node to send an empty string to start the server.
    • Deploy your flow to activate the server.
  3. Check for errors:

    • If you encounter issues, verify that the port is not blocked on your system.
    • You can troubleshoot the flow files located in the Node-RED directory.

Step 2: Configuring the Modbus Reader

  1. Add a Modbus Flex Getter node:

    • This is preferred over the standard Getter due to its robustness.
    • Connect it to the Modbus server node.
  2. Configure the Modbus Flex Getter:

    • Set the function code to 3 (read holding registers).
    • Specify the unit ID, starting address, and the quantity of registers to read.
    • For example, to read 4 registers, your configuration might look like this:
      {
        "unitid": 1,
        "fc": 3,
        "address": 0,
        "quantity": 4
      }
      
  3. Deploy the flow and check the output:

    • Use the inject node to trigger a read.
    • You should see the values from the specified registers.

Step 3: Writing Values to Modbus Registers

  1. Add a Modbus Flex Writer node:

    • This node allows you to write values to Modbus registers.
  2. Configure the Modbus Flex Writer:

    • Set the function code to 16 for writing multiple registers.
    • Specify the unit ID and starting address.
    • Prepare the payload with integer values to write.
  3. Use a Join node to create an array:

    • This node can combine multiple values into a single array for writing.
    • Ensure the array length matches the number of values you wish to write.
  4. Deploy the flow and test writing values:

    • Inject an array of values (e.g., [1, 2]) to write to the specified registers.
    • Confirm the values by reading the registers again.

Step 4: Implementing Random Number Generation

  1. Install the random node:

    • Use the terminal to install the node by navigating to the Node-RED directory.
    • Run the command:
      npm install node-red-contrib-random
      
  2. Create a flow to generate random values:

    • Set the random node to generate values from 0 to 65535 (2^16).
    • Configure the inject node to trigger the random number generation every second.
  3. Deploy the flow to see random values being generated and written to the Modbus registers in real-time.

Step 5: Enhancing Your Flow

  1. Add comments and labels:

    • Clearly label nodes and add comments to explain their purpose.
    • This will make your flow easier to understand for future reference.
  2. Test and debug your flow:

    • Ensure all nodes are connected correctly and troubleshoot any issues that arise.

Conclusion

You have successfully created a basic Node-RED flow that interacts with a Modbus server, allowing you to read from and write to registers. By implementing random number generation, you can simulate real-world data inputs. For further learning, consider exploring more advanced Modbus features or integrating actual hardware systems into your flows.