# Format data to INT\_16 for Modbus TCP

**URL:** <https://discourse.nodered.org/t/format-data-to-int-16-for-modbus-tcp/67238>\
**Category:** General\
**Created:** [1 September 2022 22:29 UTC](https://discourse.nodered.org/t/format-data-to-int-16-for-modbus-tcp/67238 "2022-09-01T22:29:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![telinal](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/telinal/32/57974_2.png) [@telinal](https://discourse.nodered.org/u/telinal)\
**Post date:** [1 September 2022 22:29 UTC](https://discourse.nodered.org/t/format-data-to-int-16-for-modbus-tcp/67238/1 "2022-09-01T22:29:33Z")

</div>

We are using the 1-wire node to read temperature from a 1-wire sensor. The node output msg.payload is as follows:  
_{"payload":15.25,"topic":"","\_msgid":"dac51164c5a5ec03"}_

We need to write this temperature data (in this example 15.25) to a Modbus TCP client with the "Modbus write" node, and the error we currently get is:  
_Error: Data length error, expected 8 got 4_

So I guess some sort of data parsing/conversion needs to take place (with a function node or parse node?) to format the payload to a clean 16 bit signed integer so that the Modbus TCP client register can be written with function code 6.

What is the simplest most effective way to achieve this?

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/6/367c0e444a011388e3b016730b9e9aa74c6f8295.png)

Thank you!

Flow and used nodes follow:

```auto
[
    {
        "id": "873e6f46fb3cd974",
        "type": "1-Wire",
        "z": "f41a1647b9ab4cab",
        "identifier": "28-00000cfa46e8",
        "name": "Sensor",
        "format": "1",
        "x": 380,
        "y": 840,
        "wires": [
            [
                "b71bc2e80e367d13",
                "6d814f4da6947f0b"
            ]
        ]
    },
    {
        "id": "b71bc2e80e367d13",
        "type": "debug",
        "z": "f41a1647b9ab4cab",
        "name": "debug 1",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 660,
        "y": 840,
        "wires": []
    },
    {
        "id": "5849884c7f8b304d",
        "type": "inject",
        "z": "f41a1647b9ab4cab",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "5",
        "crontab": "",
        "once": true,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 190,
        "y": 840,
        "wires": [
            [
                "873e6f46fb3cd974"
            ]
        ]
    },
    {
        "id": "6d814f4da6947f0b",
        "type": "modbus-write",
        "z": "f41a1647b9ab4cab",
        "name": "MTCP",
        "showStatusActivities": true,
        "showErrors": true,
        "unitid": "1",
        "dataType": "HoldingRegister",
        "adr": "40002",
        "quantity": "1",
        "server": "df54d9acd8346d83",
        "emptyMsgOnFail": true,
        "keepMsgProperties": true,
        "x": 650,
        "y": 920,
        "wires": [
            [],
            []
        ]
    },
    {
        "id": "df54d9acd8346d83",
        "type": "modbus-client",
        "name": "MDBUS CLIENT",
        "clienttype": "tcp",
        "bufferCommands": true,
        "stateLogEnabled": false,
        "queueLogEnabled": false,
        "failureLogEnabled": true,
        "tcpHost": "192.168.1.167",
        "tcpPort": "502",
        "tcpType": "DEFAULT",
        "serialPort": "/dev/ttyUSB",
        "serialType": "RTU-BUFFERD",
        "serialBaudrate": "9600",
        "serialDatabits": "8",
        "serialStopbits": "1",
        "serialParity": "none",
        "serialConnectionDelay": "100",
        "serialAsciiResponseStartDelimiter": "0x3A",
        "unit_id": "1",
        "commandDelay": "1",
        "clientTimeout": "1000",
        "reconnectOnTimeout": true,
        "reconnectTimeout": "2000",
        "parallelUnitIdsAllowed": true
    }
]

```

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [1 September 2022 22:52 UTC](https://discourse.nodered.org/t/format-data-to-int-16-for-modbus-tcp/67238/2 "2022-09-01T22:52:25Z")

</div>

> [@telinal](#):
>
> We need to write this temperature data (in this example 15.25) to a Modbus TCP

your value is a float .. sometimes floats are 32 bit values (2 registers) or it could be an integer divided by a /10 or /100 (multiplied in this case to get back to an 16bit int). Do you know how your Modbus device expects the value ? Do you have any documentation ?

---

<div class="post-metadata">

**Author:** ![telinal](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/telinal/32/57974_2.png) [@telinal](https://discourse.nodered.org/u/telinal)\
**Post date:** [1 September 2022 23:35 UTC](https://discourse.nodered.org/t/format-data-to-int-16-for-modbus-tcp/67238/3 "2022-09-01T23:35:23Z")

</div>

Hi there, yes the modbus device expects a INT\_16 value, that is a signed 16 bit interger value.

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [2 September 2022 00:18 UTC](https://discourse.nodered.org/t/format-data-to-int-16-for-modbus-tcp/67238/4 "2022-09-02T00:18:14Z")

</div>

ok .. what if you do send an integer instead of a float  
If you wire an Inject node to the Modbus Write node and send a msg.payload **15** (change to type Number)

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

does this get written to the device ?

* * *

.. you can also try changing your register and writing to address 2

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

* * *

**[EDIT]**

As i mentioned above floats are usually 32bit and use 4 bytes  
meaning that you may need to write to **2 x registers** to the device. FC16  
If that is the case the conversion for that could be done either with the [buffer nodes](https://flows.nodered.org/node/node-red-contrib-buffer-parser)  
or in a Function node, make an array of the two registers UInt16bit values that the modbus node expects

```auto
let buf = Buffer.alloc(4)
buf.writeFloatBE(msg.payload, 0)
msg.payload = [buf.readUInt16BE(0), buf.readUInt16BE(2)]

return msg;

```

From the node's help tab :

> For FC 16, **msg.payload** must be an array`[]` of comma separated values between 0:65535 each.

i could be wrong .. im tagging @Steve-Mcl who is a ninja with these things .. crunches bytes for breakfast

---

<div class="post-metadata">

**Author:** ![MountainKing91](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mountainking91/32/59516_2.png) [@MountainKing91](https://discourse.nodered.org/u/MountainKing91)\
**Post date:** [2 September 2022 08:40 UTC](https://discourse.nodered.org/t/format-data-to-int-16-for-modbus-tcp/67238/5 "2022-09-02T08:40:55Z")

</div>

Hi telinal,

like UnborN suggested you are trying to write a float value (32 bit) in a single register (16 bit). I replicated what you are doing with a local modbus server and basically I get to write "15" (the floating point part is ignored). I am reading 4 registers just to show that there is nothing else being written.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/1/5128370f2e863f75ed8db9296c14d374cfd73775.png)  
If you really want to write the exact value, you need to write it to multiple registers with FC16 (check for your device endianess requirements).

---

<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:** [1 November 2022 08:41 UTC](https://discourse.nodered.org/t/format-data-to-int-16-for-modbus-tcp/67238/6 "2022-11-01T08:41:20Z")

</div>

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