Problems with Codesys => Modbus => Node-RED - Data types and conversions

Hello everyone,

I'm trying to create a nice Node-RED Dashboard visualization for my Codesys programm. Codesys runs on a rasperry pi, also node-RED.

So, I have a MODBUS TCP connection from Node-RED to codesys and I want to show some data on the dashboard.

My problem is, how to deliver a TOD datatype (time of day - it's a 4 BYTE value) to the dashboard (into a time picker). The TOD data type looks in Codesys like "#TOD12:00:00". If i convert it to a DWORD (also 4 bytes), I get "43200000". I think this is milliseconds from 00:00:00.

To send/read it over modbus, I have to split it to 2 WORD data types (Codesys).
In Node-RED i get the 2 modbus registers and convert them again in a 4 byte value. But I don't get the right value. It is merged in a wrong sequence.

I use a function, which I found here in this forum. This merges the 4-Byte Array (raw-data from the modbus node) to a 4-byte UINT value. But, as I mentioned, in a wrong sequence, so I get a wrong value.

If I split TOD#12:00:00 (> DWORD= 43200000) in Codesys in 4 Bytes I get:
by0: 0
by1: 46
by2: 147
by3: 2.

If i look at the raw data byte array, received from node-red, I get:
by0: 46
by1: 0
by2: 2
by3: 147

The function to merge is:

// Create new Buffer based on array bytes
const buf = Buffer.from(msg.payload.buffer);

// Represent these bytes as 32-bit unsigned int
const value = buf.readUInt32LE();

// save the value
msg.payload = value;

return msg;

The value I get from the function: 2466381870.

Would you please help me to find the correct way to transmit/merge the right values?
In a further step, I also want to send data from the node-red time picker to codesys (with modbus), so I also need the way back...

I'm new to Node-RED, so I'm also javascript beginner.

Thanks very much und please excuse my bad english!

Hi @Speedriff you might find the node-red-contrib-buffer-parser useful (I wrote it for all the various requirements of PLC data conversions).

As for


[{"id":"bdece2af.dc55d","type":"inject","z":"b9a43aed.f09838","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1200,"y":80,"wires":[["45efc5de.d26a7c"]]},{"id":"45efc5de.d26a7c","type":"function","z":"b9a43aed.f09838","name":"46, 0, 2, 147","func":"//msg.payload = Buffer.from([0, 46, 147, 2]);\nmsg.payload = Buffer.from([46, 0, 2, 147]);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1350,"y":80,"wires":[["e354ddc9.d360d"]]},{"id":"e354ddc9.d360d","type":"buffer-parser","z":"b9a43aed.f09838","name":"","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"uint32le","name":"time","offset":0,"length":1,"offsetbit":0,"scale":"1","mask":""}],"swap1":"swap16","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"keyvalue","resultTypeType":"output","multipleResult":false,"fanOutMultipleResult":false,"setTopic":true,"outputs":1,"x":1180,"y":140,"wires":[["10ddc56a.96aafb","2c931a14.fe40e6"]]},{"id":"10ddc56a.96aafb","type":"debug","z":"b9a43aed.f09838","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1350,"y":140,"wires":[]},{"id":"2c931a14.fe40e6","type":"function","z":"b9a43aed.f09838","name":"to time","func":"msg.payload = new Date(msg.payload.time)\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1170,"y":200,"wires":[["3f3f00f1.efe93"]]},{"id":"3f3f00f1.efe93","type":"debug","z":"b9a43aed.f09838","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1350,"y":200,"wires":[]}]

Thanks very much, this seems to be the solution, although it's hard to understand all functions of these two nodes. I will need some time to understand and for trying :wink:
Also really nice documentation!


This works perfectly, so thank you very much!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.