Explainer
RS485 is not Modbus: reading a serial sensor from Node-RED
Short answer
RS485 defines the electrical interface, not the message format. If the sensor manual documents Modbus RTU, use a Modbus client node with the unit ID from the sensor's address switches; if it documents a vendor-specific query, use a serial request node and send the manual's exact bytes as a buffer, with baud, parity and stop bits matching at both ends. Only one requester polls the bus.

On this page
An RS485 port tells you how bits travel, not what they mean. The standard covers the electrical characteristics of drivers and receivers and stops there: it defines no addresses, no function codes and no checksums. So when a distance sensor arrives with an RS485 port and a manual full of sensor-specific query frames, a Modbus node is the wrong tool. You read it with a serial request node that sends the manual's exact bytes as a buffer and collects the reply. A Modbus client node is right only when the manual actually documents Modbus RTU. Below is how to tell the two cases apart, how to build the request in each one, and which serial settings must match at both ends.
RS485 carries bits; the protocol gives them meaning #
RS-485 sits at layer 1 of the OSI model. The standard specifies the electrical characteristics of drivers and receivers and does not specify a protocol; everything above the wires — addressing, commands, error checking — comes from a higher-layer protocol that happens to run over those wires. Modbus RTU is one such protocol. A manufacturer's own command set is another, and plenty of dedicated RS485 sensors, ultrasonic distance sensors among them, ship with exactly that.
The practical consequence: the letters RS485 on a datasheet are evidence of a physical interface and nothing else. Terminating the pair correctly and matching the A/B labels to the sensor's signal definitions buys you a bit path. What the bits mean is decided by one document, the sensor's communication manual.
So open the manual's communication section before you open Node-RED. If it documents Modbus RTU registers and function codes, use a Modbus client node. If it documents a query-and-response format of its own, use a serial request node. If it documents neither, get the communication document for your exact hardware and firmware revision from the vendor before wiring anything: guessed frames are not a commissioning method.
If the manual documents Modbus RTU #
A Modbus RTU request is a fixed frame: unit ID, function code, data, CRC. The unit ID is the sensor's own address, set on its DIP switches or configuration tool — not an address of your gateway. The usable range is 1 to 247; address 0 is broadcast and never identifies a single sensor. Every slave on one serial bus needs a unique unit ID.
The bus also carries exactly one requester. Node-RED, as the client (traditionally the master), sends a request and waits; the addressed sensor answers. Two masters cannot share the pair, and several sensors are served by giving each its own unit ID, which is also the subject of how one serial master polls many Modbus RTU slaves.
Worked example — read one holding register from unit 1, at register 0:
- unit ID 1 encodes as the byte 0x01
- function code 3 (read holding registers) encodes as 0x03
- register 0 encodes as two bytes, 0x0000
- quantity 1 encodes as two bytes, 0x0001
- CRC-16/Modbus over those six bytes (reflected polynomial 0xA001, initial value 0xFFFF) gives 0x0A84, appended low byte first: 0x84 0x0A
Full frame on the wire: 01 03 00 00 00 01 84 0A. You do not compute this by hand — a Modbus client node from the node-red-contrib-modbus palette builds it from the four fields you enter — but it is what you compare against a serial sniffer when the node's settings and the sensor disagree. One trap: manuals often print document-style references such as 40001 while nodes expect the raw protocol address, so check which convention your node uses before converting.
If the manual documents its own query format #
This is the common case for dedicated sensors: the manual shows a query format and a response format, the address is chosen on DIP switches, and the word Modbus never appears. The tool is the serial request node from the node-red-node-serialport palette: it takes msg.payload exactly as a serial out node would, writes it to the port in strict FIFO order, and returns whatever the device answers on its output.
Build the request from the manual, field by field. Illustrative example — the manual defines a four-byte frame: address, command, data byte, then a checksum equal to the sum of the previous bytes modulo 256. With the DIP switches set to address 0x01 and the distance command 0x02:
checksum = (0x01 + 0x02 + 0x00) mod 256 = 0x03
In a function node:
// Illustrative frame for a manual that defines:
// address byte, command byte, data byte, checksum = sum of previous bytes mod 256
let frame = Buffer.from([0x01, 0x02, 0x00, 0x03]);
msg.payload = frame;
return msg;
Payload type is the detail that breaks most first attempts. A binary protocol needs a Buffer: Buffer.from with an array of byte values puts the four bytes on the wire. An inject node set to string sending the text 0x01 transmits four ASCII characters, not one address byte, and a number payload is serialized as its decimal text. Only a documented text protocol takes a string, sent with its exact delimiters and no added newline.
Prove it before polling: wire the serial request output to a debug node, trigger once, and check two things — frame.length equals the manual's request length, and the reply's byte count equals the manual's response length. One safety note: send only documented frames. On a sensor with a configuration command set, a guessed byte sequence can match a command and silently change the address or operating mode, and the sensor then disappears from the bus you just configured.
Serial settings that must match at both ends #
The serial driver moves bytes without understanding them, so a protocol-perfect request still fails when the character formats differ. These are the settings to copy from the sensor's communication table into the node's serial connection configuration:
| Setting | Take from | Failure when wrong |
|---|---|---|
| Serial port | the OS path of the gateway's RS485 interface | node cannot open the port |
| Baud rate | sensor communication table | garbage bytes or silence |
| Data bits | sensor communication table | wrong characters |
| Parity | sensor communication table | framing errors |
| Stop bits | sensor communication table | framing errors |
| Response timeout | measured reply time | reply cut off or endless wait |
The OS path is not the connector label: list the gateway's serial devices from its operating system and confirm the account running Node-RED can open that path, with no other process holding the port. If Node-RED runs in a container on the gateway, the device must also be passed into that container; exposing a serial port to Node-RED in a Docker container covers that step. The Modbus palette groups the same baud, parity and stop-bit values in its own connection configuration, even though Node-RED is the requester, so check them there too.
Decoding the reply #
The serial request node returns the reply as a Buffer, and it does not know where a reply ends unless told. Set the response split to what the manual defines: a fixed byte count, a delimiter character, or a timeout that closes the collection window. A timeout ending the window does not prove the collected bytes form one valid frame, so count them.
Then strip the framing. Continuing the illustrative protocol — the response is address, command, two data bytes, checksum — a reply of 01 02 01 2C 30 decodes in a function node:
// Illustrative decode for the same five-byte response format
let replyBytes = msg.payload; // Buffer from the serial request node
let checksum = 0;
for (let i = 0; i < replyBytes.length - 1; i++) {
checksum = (checksum + replyBytes[i]) & 0xFF;
}
if (checksum !== replyBytes[replyBytes.length - 1]) {
node.warn("checksum mismatch: " + checksum);
return null;
}
let distance = replyBytes.readUInt16BE(2); // the two data bytes, big-endian
msg.payload = distance;
return msg;
The arithmetic: checksum 0x01 + 0x02 + 0x01 + 0x2C = 0x30, which matches the last byte; distance 0x012C = 1 × 256 + 44 = 300, in whatever unit the manual states. The test: trigger one request with that reply on the wire and the debug node after the function node shows 300.
The decision for your gateway #
- The manual documents Modbus RTU: use a Modbus client node, enter the sensor's switch-set address as the unit ID, keep Node-RED the only requester on the bus, and let the node build frames and check the CRC.
- The manual documents a vendor query format: use the serial request node, put the manual's bytes into
msg.payloadas a Buffer, match the character settings at both ends, and decode with your own function node. - The manual documents neither: stop and get the vendor's communication document for your exact model and revision. Do not guess bytes at a configured sensor.
Once the distance is a number in msg.payload, forwarding it is ordinary Node-RED work; if the value travels to a broker over an unreliable link, buffering MQTT data in Node-RED during an internet outage covers the store-and-forward pattern.
Questions asked
A PLC already polls this RS485 bus. Can Node-RED read the same sensor?
Not as a second master. Two requesters on one serial pair interleave their frames, so both receive corrupted replies and the sensors see malformed requests. Give the sensor its own port and make Node-RED the only client on it, or leave the PLC polling and read the value from the PLC over its own communications link.
Is there a default baud rate for RS485?
No. The standard defines the electrical interface, not character timing, so there is nothing to default to. Take baud rate, data bits, parity and stop bits from the sensor's communication table, including any switch table that selects them, and enter identical values in the serial node. A common industrial value working on one sensor proves nothing about the next.
The manual documents both Modbus and a vendor command set. Which one do I use?
Whichever the sensor's configuration actually enables: devices that offer both usually select the protocol with a switch position or a setup command, and only one format answers on the port. Read the switch table, confirm the active protocol, then configure Node-RED for exactly that one. Prefer Modbus RTU when it is available, because the client node builds frames and checks the CRC for you.
The reply arrives as several short messages instead of one frame. Why?
The split setting, not the sensor, is cutting it: a delimiter that also occurs inside the data, or a timeout shorter than the reply time, ends each message early. Prefer the fixed byte count the manual documents for the response, and lengthen the timeout beyond the sensor's documented response time. Then count the bytes of one collected message against the manual before decoding.


