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

**URL:** https://discourse.nodered.org/t/problems-with-codesys-modbus-node-red-data-types-and-conversions/38303
**Category:** General
**Created:** [30 December 2020 21:29 UTC](https://discourse.nodered.org/t/problems-with-codesys-modbus-node-red-data-types-and-conversions/38303 "2020-12-30T21:29:25Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Speedriff](https://avatars.discourse-cdn.com/v4/letter/s/c2a13f/32.png) [@Speedriff](https://discourse.nodered.org/u/Speedriff)
#### Post date: [30 December 2020 21:29 UTC](https://discourse.nodered.org/t/problems-with-codesys-modbus-node-red-data-types-and-conversions/38303/1 "2020-12-30T21:29:25Z")

</div>

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!  
Chris

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [30 December 2020 22:15 UTC](https://discourse.nodered.org/t/problems-with-codesys-modbus-node-red-data-types-and-conversions/38303/2 "2020-12-30T22:15:51Z")

</div>

Hi @Speedriff you might find the [node-red-contrib-buffer-parser](https://flows.nodered.org/node/node-red-contrib-buffer-parser) useful (I wrote it for all the various requirements of PLC data conversions).

As for

> [@Speedriff](#):
>
> by0: 46  
> by1: 0  
> by2: 2  
> by3: 147

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/1/e1dd85fceec1aa4ee64d9dc84bbf68b10552301f.png)

```auto
[{"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":[]}]

```

---

<div class="post-metadata">

### Author: ![Speedriff](https://avatars.discourse-cdn.com/v4/letter/s/c2a13f/32.png) [@Speedriff](https://discourse.nodered.org/u/Speedriff)
#### Post date: [30 December 2020 23:52 UTC](https://discourse.nodered.org/t/problems-with-codesys-modbus-node-red-data-types-and-conversions/38303/3 "2020-12-30T23:52:41Z")

</div>

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 😉  
Also really nice documentation!

Chris

---

<div class="post-metadata">

### Author: ![Speedriff](https://avatars.discourse-cdn.com/v4/letter/s/c2a13f/32.png) [@Speedriff](https://discourse.nodered.org/u/Speedriff)
#### Post date: [2 January 2021 06:20 UTC](https://discourse.nodered.org/t/problems-with-codesys-modbus-node-red-data-types-and-conversions/38303/4 "2021-01-02T06:20:32Z")

</div>

This works perfectly, so thank you very much!

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [16 January 2021 06:20 UTC](https://discourse.nodered.org/t/problems-with-codesys-modbus-node-red-data-types-and-conversions/38303/5 "2021-01-16T06:20:52Z")

</div>

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