Skip to content

Serial Port Nodes over NATS for Node-RED ​

@rosepetal/node-red-contrib-serialport provides 3 nodes for Node-RED (serial in, serial out and the configuration node they share) that mirror the stock Node-RED serial nodes over NATS: a separate engine, normally a Docker container, opens the physical serial port, and Node-RED only publishes the writes and subscribes to the reads.

Installation ​

bash
cd ~/.node-red
npm install rosepetal-ai/node-red-contrib-serialport

Restart Node-RED afterwards. The package is not published on npmjs.com: the command installs it from its GitHub repository, which is private, so npm needs git credentials with access to it.

The nodes do not open the serial port themselves. They need a NATS server and the serial engine: see Serial engine.

Install script ​

On a host that runs both Node-RED and the engine, install.sh at the root of a clone of the repository does the whole setup. It needs docker, npm and node, and a running Docker daemon.

bash
./install.sh
  1. It pulls the engine image of the package version, or builds it from engine/ when the pull fails, and tags it rosepetal-serial-engine: followed by the version.
  2. It runs npm install of the clone in the Node-RED user directory: NODE_RED_DIR, else NODE_RED_HOME, else ~/.node-red.
  3. Unless the rosepetal-serial-engine container is already running, it starts it if it exists, or launches it from that image with --network host --privileged and the NATS_URL, SUBJECT_ROOT and STATUS_HEARTBEAT_MS of its own environment (defaults nats://localhost:4222, rosepetal.serial and 5000).

The container it launches has no restart policy. Unless ROSEPETAL_ENGINE_AUTOSTART=false, Node-RED starts it again when Node-RED starts: see Docker engine.

Requirements ​

  • Node.js >= 18
  • A NATS server that both Node-RED and the engine can reach (nats://localhost:4222 by default)
  • The serial engine on the host that has the serial device: the container built from the engine/ folder of the repository, or another service that implements the NATS channels
  • Docker, to build and run the engine as a container

Nodes ​

rosepetal-serial-in and rosepetal-serial-out appear under the network category in the editor. rosepetal-serial-config is a configuration node, created from the Serial Port field of the other two.

NodePurpose
rosepetal-serial-configConfiguration node: the port and its line settings, how the input is split into messages, the character added to the output, the NATS server and the subject root
rosepetal-serial-inEmit a message for each piece of data read from the serial port, split by the rules of the configuration node
rosepetal-serial-outWrite msg.payload to the serial port; msg.baudrate asks for a baud rate change

Reference ​

PageContents
NATS channelsThe four subjects that the nodes and the engine share, with the JSON payload of every message

Quick start ​

  1. Start a NATS server that Node-RED and the engine can reach.
  2. Build and run the engine on the host that has the serial device: see Serial engine.
  3. In Node-RED, add a rosepetal-serial-in node and create a rosepetal-serial-config node from its Serial Port field. If the NATS server is not at nats://localhost:4222, first set its URL under Advanced settings: the port list is asked through it. Choose the port from the list (the engine reports the ports of its host) and set the baud rate and the other line settings.
  4. Wire the node to a debug node and deploy. The status under the node becomes serial connected when the engine has opened the port, and each line received arrives as one message: by default the input is split on \n and delivered as a string.
  5. To write to the port, add a rosepetal-serial-out node that uses the same configuration node and send it a message with the data in msg.payload.

How it works ​

Node-RED and the engine only talk through NATS. The subjects share a root, rosepetal.serial by default:

SubjectDirectionCarries
rosepetal.serial.outNode-RED to engineThe data to write, from rosepetal-serial-out
rosepetal.serial.inEngine to Node-REDThe data read from the port, for rosepetal-serial-in
rosepetal.serial.controlBothFrom Node-RED, the port settings and the baud rate changes; from the engine, the state of the port and its heartbeat
rosepetal.serial.discoveryRequest and replyThe serial ports of the engine's host, for the port list of the editor
  • The configuration node sends its settings to the engine, which opens the port with them and reports its state. The engine repeats the state as a heartbeat, every 5 seconds by default.
  • The status under each node shows the NATS connection first and then the state of the port: serial connected while the engine has it open, serial heartbeat lost when the engine has been silent for 15 seconds.
  • Every write, frame and control message names the port it belongs to, so one engine serves all the ports that the configuration nodes ask for.
  • The messages are JSON envelopes with the data in base64, so any NATS client can follow the traffic next to Node-RED, for example with the NATS command line client:
bash
nats sub 'rosepetal.serial.>'

The payload of every message is described in NATS channels.

Serial engine ​

The engine is a Node.js service in the engine/ folder of the repository. It opens the serial devices with the serialport library and implements the NATS channels. Build its image from a clone of the repository:

bash
cd engine
docker build -t rosepetal-serial-engine .

Run it on the host that has the serial device:

bash
docker run --rm \
  --name rosepetal-serial-engine \
  --network host \
  --privileged \
  -e NATS_URL=nats://localhost:4222 \
  -e SUBJECT_ROOT=rosepetal.serial \
  -e STATUS_HEARTBEAT_MS=5000 \
  rosepetal-serial-engine

The container needs access to the serial device (--privileged is the easiest way) and a NATS server it can reach (--network host lets it use one on localhost). The name matters: rosepetal-serial-engine is the container that the configuration node looks for before it tries to start an engine by itself.

VariableDefaultPurpose
NATS_URLnats://localhost:4222NATS server
SUBJECT_ROOTrosepetal.serialRoot of the subjects. It must match the Subject root of the configuration node; several engines can share a NATS server if each has its own root
STATUS_HEARTBEAT_MS5000Interval of the status heartbeat of every port. Keep it well below 15000: the nodes show serial heartbeat lost after 15 seconds without a status
SERIAL_RECONNECT_MS15000Wait before the engine opens a port again after an error or after the port closes. It doubles at every failed attempt
SERIAL_RECONNECT_MAX_MS60000Longest wait between two attempts
SERIAL_RECONNECT_JITTER_MS500Upper limit of the random time added to every wait

The engine opens a port when it receives the settings of a configuration node for it, and opens it again when the port, the baud rate, the data bits, the parity or the stop bits change. Writes that arrive while the port is not open are queued (up to 1000; beyond that the oldest are dropped) and sent when it opens. A configure message or a write for a port that is not open makes the engine try to open it at once, without waiting for the next attempt.

By default the configuration node also tries to start the engine container from Node-RED, through the Docker command line of the Node-RED host. Docker engine describes what it runs and the environment variables that change or disable it: set ROSEPETAL_ENGINE_IMAGE=rosepetal-serial-engine in the environment of Node-RED to make it use the image built above, or ROSEPETAL_ENGINE_AUTOSTART=false so that Node-RED does not start a container by itself.