IMD SolutionComputing LibraryShop main store

How-to

Polling DLT645 energy meters from Node-RED over TCP to RS485

Edge computing and IIoT Published 8 min read By IMD Solution engineering team

Short answer

Poll DLT645 meters from Node-RED with one serialized request-and-response queue per RS485 segment, never with concurrent requests. At the DL/T 645-2007 default of 2400 bit/s, one transaction holds the bus for about 184 ms, so 10 meters fit a 5-second cycle with a 1-second timeout and two retries. Publish the decoded value with a freshness flag, and write to MariaDB only after every frame check passes.

On this page
  1. How does a DLT645 frame reach exactly one meter?
  2. How long does one poll of 10 meters hold the bus?
  3. How do you build the request and check its checksum?
  4. How do you build the polling flow in Node-RED?
  5. When does a reading reach the PLC and the database?
  6. Why does the TCP connection work while no meter answers?
  7. How do you commission the queue without trusting the dashboard?
  8. Questions asked

Run one serialized request-and-response queue per RS485 segment. Each of the 10 meters gets its turn, the next request leaves only after the previous response is validated or has timed out, and nothing reaches the PLC, MariaDB or the dashboard until the frame checks pass. One transaction holds the bus for about 184 ms at the meters' default serial rate, so the whole queue fits a 5-second cycle with room for retries. The sections below give the DL/T 645-2007 frame layout, the checksum arithmetic with a worked request and response, the Node-RED flow that drives a TCP-to-RS485 gateway, and the test that proves a silent meter surfaces as stale rather than as a zero.

How does a DLT645 frame reach exactly one meter? #

DL/T 645-2007 frames the bus with a start byte 0x68, a six-byte BCD address sent low byte first, a second 0x68, a control code, a length byte, the data field, a one-byte checksum and the trailer 0x16.

Position Bytes Content
before the frame 0 to 4 0xFE wake-up preamble; strip it before decoding
0 1 start byte 0x68
1 to 6 6 meter address, 12 BCD digits, low byte first
7 1 second start byte 0x68
8 1 control code
9 1 data length L
10 to 9+L L data identifier first, then the payload
10+L 1 checksum
11+L 1 trailer 0x16

Meter 1 carries the address 000000000001 and therefore answers to the bytes 01 00 00 00 00 00 on the wire. The master reads with control code 0x11; a normal answer arrives as 0x91 and a refusal as 0xD1 with one error-information byte instead of data. Inside the data field a four-byte data identifier, also low byte first, names the register; 00000000 is the current total active energy, carried as six integer digits and two decimals in kWh. RS485 itself supplies only the differential pair and the multi-drop rules, and the protocol riding on it defines every byte, which is the same reason RS485 is not Modbus when a serial sensor is read from Node-RED.

How long does one poll of 10 meters hold the bus? #

At the standard's default 2400 bit/s with 8E1 framing, a request-plus-response transaction occupies the wire for about 184 ms, so 10 meters need roughly 1.8 s of bus time.

  • One character carries 11 bits: 1 start + 8 data + 1 even parity + 1 stop.
  • 2400 bit/s ÷ 11 bits per character = 218 characters per second, about 4.58 ms per character.
  • Request: 20 bytes including the four-byte preamble, so 20 × 4.58 ms ≈ 92 ms.
  • Response with a four-byte value: also 20 bytes, so another 92 ms; identifiers that return longer data fields take proportionally longer.
  • Wire time per meter ≈ 92 + 92 = 184 ms, before meter processing and bus turnaround.

Commit to a 5-second cycle at that rate, a 1-second timeout per attempt and two retries. The answer needs about 92 ms on the wire, so the 1-second timeout covers the preamble, the meter's think time and the gateway's packetization tenfold over. A dead meter then costs its three silent attempts, 3 s at worst: nine good meters need about 1.7 s of wire time, the worst cycle stays under 4.7 s, and no value in the PLC waits longer than one cycle for a refresh.

How do you build the request and check its checksum? #

For meter 000000000001 reading data identifier 00000000, the full request is FE FE FE FE 68 01 00 00 00 00 00 68 11 04 00 00 00 00 E6 16, where E6 is the checksum over every byte from the first 0x68 to the last data byte.

The checksum is the plain arithmetic sum of those bytes taken modulo 256; nothing from the 0xFE preamble and nothing past the data field enters it. For this request: 104 (first 0x68) + 1 (address) + 104 (second 0x68) + 17 (0x11) + 4 (length) + 0 (identifier) = 230, and 230 is 0xE6 as one byte. The same sum in a function node, with the test that proves it:

// frame: Buffer holding the bytes from the first 0x68 to the last data byte
// sum: number, starts at 0; i: number, loop index starting at 0
const frame = Buffer.from('6801000000000068110400000000', 'hex');
let sum = 0;
for (let i = 0; i < frame.length; i++) { sum = (sum + frame[i]) & 0xFF; }
node.warn('checksum 0x' + sum.toString(16).toUpperCase());
// test: the warning prints checksum 0xE6, equal to the hand sum

A meter holding 000123.45 kWh answers with 68 01 00 00 00 00 00 68 91 08 00 00 00 00 45 23 01 00 D3 16. Its checksum: 104 + 1 + 104 + 145 (0x91) + 8 + 0 + 0 + 0 + 0 + 69 (0x45) + 35 (0x23) + 1 + 0 = 467, and 467 - 256 = 211 = 0xD3. The length byte 8 covers four identifier bytes plus four value bytes; each byte carries two BCD digits and the value arrives low byte first, so 45 23 01 00 reads as 00 01 23 45, giving 000123.45 with the identifier's two implied decimals.

Accept a response only when all five checks pass:

  • the stripped frame begins with 0x68 and byte 7 is the second 0x68;
  • the length byte matches the bytes actually present and the frame ends in 0x16;
  • the recomputed checksum equals the received checksum byte;
  • the address echoes the meter that was just polled;
  • the control code is 0x91 and the echoed identifier matches the request.

Any miss is a failed poll, never a zero reading.

How do you build the polling flow in Node-RED? #

Build the flow as one queue that sends a meter's request only after the previous transaction has returned a validated frame or exhausted its retries, with a decoder between the TCP bytes and every output.

  1. Write the meter list once: each entry carries a meter's 12-digit address, its data identifiers and its edition of the standard, taken from the meter's documentation or nameplate.
  2. Point the TCP side at the serial gateway using its configured address and port, for example 192.168.1.50 and port 8899, and set the gateway's serial side to the meters' settings: 2400 bit/s, 8E1 unless the meter documentation says otherwise.
  3. Send one request as a single write and start a 1-second timer; nothing else goes onto the bus until the response is complete or the timer fires.
  4. Buffer the incoming bytes and scan for frames: a TCP stream has no message boundaries, so one response can arrive split across two reads or glued to the next frame's preamble. A frame ends at the first 0x16 that follows a matching checksum.
  5. Run the five checks and decode the value; on a pass, forward the number with a true freshness flag.
  6. On a timeout or a failed check, retry the same meter twice, then mark it stale, log the outcome and advance to the next address.
  7. Close the cycle: a full pass over the 10 meters at the default rate takes about 5 s including turnaround gaps, and the queue then restarts from the first address.
  8. Log address, outcome and timestamp for every transaction, so a failure maps to one device rather than to the segment.

When does a reading reach the PLC and the database? #

Only a frame that passed all five checks is published: the PLC receives the decoded value with its freshness flag, and MariaDB receives one row holding the meter address, data identifier, value, unit and acquisition time.

Expose the per-meter values to the PLC as plain numbers with a companion flag, for example as holding registers served from a Modbus TCP server in the same flow, one register for the scaled value and one for the flag. When a meter goes stale, its last good value stays in that image with the flag false; nothing overwrites it with zero. The dashboard binds to the same validated property and shows the acquisition time beside it, so a frozen value looks frozen to the operator. The insert branch follows the pattern of logging Modbus TCP data to a database from a plant PC, with the DLT645 decoder in front: one INSERT per validated reading, and the flow checks the statement result instead of assuming the row landed.

Why does the TCP connection work while no meter answers? #

An open TCP connection proves only the hop to the gateway, so when the link is up and no valid frame comes back, check the gateway's serial settings, the RS485 pair and its termination, the meter address, and only then the decoder.

A socket that connects and then carries nothing is the symptom behind a gateway that shows a TCP connection but sends no payload: the network side is alive while the serial side is misconfigured or unwired. Work the list in order:

  • Serial settings on the gateway against the meters: a wrong rate or parity garbles every answer, and every checksum then fails.
  • The RS485 pair, its termination and bias: DLT645 meters listen silently and answer nothing when they cannot decode the bus.
  • The address in the request, especially when one meter answers and its neighbor stays silent; that points at the neighbor's address, not at the shared transport.
  • The decoder's assumptions, last.

If the flow runs in a container, add the serial pass-through to that list: Modbus RTU over TCP failing inside a Proxmox LXC container shows how the virtual link breaks while the network itself stays up.

How do you commission the queue without trusting the dashboard? #

Prove the chain in three steps, one meter alone, the full queue, then a forced failure, and accept the system only when the forced failure appears as a stale flag with no new database row.

  1. One meter alone: send the worked request, watch the hex bytes at the gateway's serial side, decode the answer by hand and compare the number with the meter's own display.
  2. The full queue: confirm that 10 distinct addresses answered inside one cycle, each with its own database row and timestamp.
  3. A forced failure: open one meter's signal pair at an isolated point during a planned outage, never by pulling terminals in a live panel, then confirm that within the worst-case cycle its flag turns false, the PLC value freezes at the last good reading and no row is written; close the pair again and confirm recovery without a flow restart.

Watch the flow's debug output at each boundary, bytes out, bytes in, decoded value, PLC image, insert result, because a dashboard that looks alive proves only the last link of the chain.

Questions asked

Can the PLC and Node-RED both act as master on the same RS485 segment?

No. DLT645 is a single-master protocol: two masters on one segment interleave their frames, both read corrupted answers and every checksum fails. Give the segment exactly one master. Either Node-RED polls and the PLC reads the results over its own network connection, or the PLC polls and Node-RED takes values from the PLC, never both on the same pair.

What does a meter's error response look like?

The meter answers with control code 0xD1 instead of 0x91 and carries a single error-information byte in place of data, for example when the data identifier is unsupported. Count it as a failed poll and log the error byte. An identifier error repeats every cycle, so correct the request instead of retrying it.

Do meters built to the older 1997 edition use the same data identifiers?

No. The older 1997 edition of the standard carries two-byte data identifiers where the 2007 edition uses four bytes, and a four-byte request to a 1997 meter comes back as an error response or not at all. Write each meter's edition beside its address in the polling list before building any request.

Should MariaDB keep the raw frames?

Keep validated readings as the history, meaning address, identifier, value, unit, acquisition time and quality flag, and store raw hex only in a separate commissioning log that can be deleted. Raw frames inside the history table slow trend queries and leave values unreadable to anyone without the decoder.

Is it worth moving the bus to 9600 bit/s?

Yes, when every meter on the segment supports it: wire time per transaction falls from about 184 ms to about 46 ms, so a full 10-meter pass needs well under half a second of bus time. A mixed bus is the trap: meters left at the default rate answer garbage and fail their checksums, so change all meters in one commissioning session and then change the gateway.

Keep reading

  • Troubleshooting

    Modbus RTU over TCP fails in a Proxmox LXC container

    A passing ping from the LXC container clears the network, not the Modbus transaction. A desktop client read the same gateway with RTU-over-TCP framing while Node-RED failed, and the gateway's own configuration was at fault: match transport framing, TCP port and unit ID on both ends, keep one client connected during tests, and rebuild the gateway configuration if matched settings still fail.

    Edge computing and IIoT

  • Explainer

    Edge gateway Modbus TCP polling over the WAN port

    When a data gateway supports MQTT as a client only, drop the broker hop: configure the Modbus TCP poll to target the device's IP address in the same subnet as the gateway's WAN interface. The gateway opens the TCP connection itself, so polled values keep updating with the internet link down. Verify by pulling the uplink and watching one known register.

    Edge computing and IIoT

  • How-to

    MQTT to Modbus TCP with Node-RED on an edge gateway

    Yes: Node-RED with the node-red-contrib-modbus package can serve MQTT data to a SCADA client as Modbus TCP. Read the RTU device in Node-RED first, give the Modbus server node a port nothing else owns, and scale each MQTT payload to one integer per holding register before the first end-to-end test.

    Edge computing and IIoT

Why a Node-RED MQTT to OPC UA variable never appears