Processing DRO Data in Node-RED





In this guide, we will show how to parse the data coming from the Digital Readout (DRO) of our lathe in our Lathe Retrofit project which provides the X and Z Axis position data. The processed data can then be used to publish on MQTT, store in a database, or visualize on a dashboard.

Prerequisites

Before you start creating the nodes, ensure that you have installed the necessary packages in the palette. For the Serial Node-RED connection, you need the node-red-node-serialport package. For a detailed explanation on downloading palettes, consult this guide.

Nodes Needed

  • Serial In: Reads data from a local serial port.

  • Function: A JavaScript function to run against the messages being received by the node.

  • Join: Joins sequences of messages into a single message.

  • Debug: Displays selected message properties in the debug sidebar tab and optionally the runtime log.

Setup

The nodes will be set up as shown:

A Node-RED flow starting with a Serial Input, which outputs to 3 different functions: Parse DRO Data, Add Timestamp, and Add Client ID. These 3 functions output to a Join Values node which outputs to a Debug node.

Description of the Flow

The following is a detailed description of what we did in our Lathe Retrofit Project. You may need to alter some aspects of what we did given different applications of this process, but feel free to use our resources as a template.

1. Serial Input Node

  • Purpose: Reads data from the DRO via the serial port.

  • Configuration:

    • Serial port: /dev/ttyUSB1

    • Baud rate: 1200

    • Data bits: 8

    • Parity: none

    • Stop bits: 1

    • Newline: \r\n

  • Configuring Newline in Serial in Node

    • When using the Serial in node to process data from the DRO correctly, it is crucial to configure the newline characters to match the data format provided by your RS232 output.

  • Why use \r\n?

    • The RS232 output typically ends each data line with a Carriage Return (CR) and Line Feed (LF) character, corresponding to \r and \n in programming environments. Using \r\n ensures that each complete data line is correctly identified and processed. This configuration ensures that the Serial In node waits for a full line ending with \r\n before passing the data to the next node, resulting in correctly received and separate messages for each axis.

  • Configuration of the Serial In Node:

    A configuration menu for the serial-in node with the following fields populated with the following values: Name: Pi - Axes Data, Serial Port: /dev/ttyUSB1, Baud Rate: 1200, Data Bits: 8, Parity: None, Stop Bits: 1, DTR: auto, RTS: auto, CTS: auto, DSR: auto, Split input in the character: \r\n and deliver ASCII strings, Default response timeout: 10000 ms

2. Function Node: Parse DRO Data

  • Purpose: Parses the data coming from the DRO into the correct format.

  • Function Code:

    
    // Function node code to parse the DRO output
      const axisData = msg.payload.trim();  // Remove any leading/trailing whitespace and newline characters
      const parts = axisData.split(":");    // Split the data by the colon character
    
      // Check if the data is for axis 1 or 2 based on the split parts
      if (parts.length === 2) {
          const axis = parts[0].trim();     // Should be '1' or '2'
          const value = parseFloat(parts[1].trim()); // Convert the axis value to a float
    
          // Store the parsed values in a new object on the msg.payload
          if (axis === '1') {
              // Axis 1 (X-axis)
              msg.payload = { x: value };
              flow.set('x', value);
          } else if (axis === '2') {
              // Axis 2 (Z-axis)
              msg.payload = { z: value };
              flow.set('z', value);
          }
      }
    
      return msg;
            

3. Function Node: Add Timestamp

  • Purpose: Adds a timestamp to the message.

  • Function Code:

    
    const now = new Date();
      const timestamp = now.toISOString().replace('T', ' ').substring(0, 19);
      msg.payload = { timestamp: timestamp };
      msg.topic = 'timestamp';
      return msg;
            

4. Function Node: Add Client ID

  • Purpose: Adds a client ID to the message.

  • Function Code:

    
    msg.payload = { clientID: 'ATL-618EVS' };
      msg.topic = 'clientID';
      return msg;
            

5. Join Node

  • Purpose: Joins the sequences of messages into a single message.

  • Configuration:

    • Mode: Custom

    • Property: msg.payload

    • Key: msg.topic

    • Joiner: \n

    • Count: 4

  • Configuration of Join Node:

    The configuration menu for the Join Node. Name: Join Values, Mode: manual, Combine each: msg.payload, to create: a merged Object, Use existing msg.parts properly: True, After a number of message parts: 4.

6. Debug Node

  • Purpose: Displays the final parsed message in the debug sidebar.

  • Configuration:

    • Output: msg.payload

By following this guide, you will be able to process and parse the data from the DRO of the lathe in Node-RED, making it ready for further use in publishing, storing, or visualizing.