Modbus TCP float to value

How can I change the float value that I receive from the Modbus TCP to the correct value?
I tried it with this nodes but that didn't work

node-red-contrib-buffer-parser is designed for this task.

@Steve-Mcl how does that work?

By Magic of course.


  1. Read the built in help and the readme
  2. Try the built in examples (CTRL+I --> examples --> node red contrib buffer parser)
    1. or just give it a go
  3. Pop back and ask specific questions if you get stuck

Also, search the forum (lots of examples of it working)

@Steve-Mcl

I want to converse the float to a decimal value, I tried it but it didn't succeed.
I used the buffer parser but got a fault : RangeError: Attempt to access memory outside buffer bounds

[{"id":"43c9c2fa98823fb3","type":"debug","z":"b3be6624e0cc5c82","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":810,"y":180,"wires":[]},{"id":"c387a64530b70c2b","type":"modbus-read","z":"b3be6624e0cc5c82","name":"","topic":"","showStatusActivities":false,"logIOActivities":false,"showErrors":false,"unitid":"61","dataType":"HoldingRegister","adr":"14","quantity":"1","rate":"10","rateUnit":"s","delayOnStart":false,"startDelayTime":"","server":"a1516e4ecc25830d","useIOFile":false,"ioFile":"","useIOForPayload":false,"emptyMsgOnFail":false,"x":210,"y":340,"wires":[["d1cb29d8f1443d50"],[]]},{"id":"d1cb29d8f1443d50","type":"buffer-parser","z":"b3be6624e0cc5c82","name":"Float 32 to Dec","data":"responseBuffer.buffer","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"floatbe","name":"Puissance (Wh)","offset":0,"length":1,"offsetbit":0,"scale":"1","mask":""}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"value","resultTypeType":"output","multipleResult":false,"fanOutMultipleResult":false,"setTopic":true,"outputs":1,"x":540,"y":240,"wires":[["43c9c2fa98823fb3"]]},{"id":"a1516e4ecc25830d","type":"modbus-client","name":"Smappee 1","clienttype":"tcp","bufferCommands":true,"stateLogEnabled":false,"queueLogEnabled":false,"tcpHost":"smappee5130007388","tcpPort":"502","tcpType":"DEFAULT","serialPort":"/dev/ttyUSB","serialType":"RTU-BUFFERD","serialBaudrate":"9600","serialDatabits":"8","serialStopbits":"1","serialParity":"none","serialConnectionDelay":"100","serialAsciiResponseStartDelimiter":"0x3A","unit_id":"61","commandDelay":"1","clientTimeout":"1000","reconnectOnTimeout":true,"reconnectTimeout":"2000","parallelUnitIdsAllowed":true}]

Because a float is 32 bit (4 bytes or 2 words) & you are only reading 1 16bit register (2 bytes)

On my Modbus register is it type 'Float' and I want it to a decimal.

image

maybe an example could help (from Kostal Inverter):

[
    {
        "id": "aa2ccdef.a81888",
        "type": "modbus-getter",
        "z": "9fc0f809.f00518",
        "name": "100 - 179",
        "showStatusActivities": false,
        "showErrors": false,
        "logIOActivities": false,
        "unitid": "71",
        "dataType": "HoldingRegister",
        "adr": "100",
        "quantity": "80",
        "server": "a1b432331279e425",
        "useIOFile": false,
        "ioFile": "",
        "useIOForPayload": false,
        "emptyMsgOnFail": false,
        "keepMsgProperties": false,
        "x": 100,
        "y": 100,
        "wires": [
            [
                "fa8ee63d.afaf18"
            ],
            [
                "1838b6bb.401ce1"
            ]
        ]
    },
    {
        "id": "fa8ee63d.afaf18",
        "type": "buffer-parser",
        "z": "9fc0f809.f00518",
        "name": "Wechselrichter",
        "data": "payload",
        "dataType": "msg",
        "specification": "spec",
        "specificationType": "ui",
        "items": [
            {
                "type": "floatle",
                "name": "P_DC",
                "offset": 0,
                "length": 1,
                "offsetbit": 0,
                "scale": "1",
                "mask": ""
            },
            {
                "type": "floatle",
                "name": "Hausv_Batt",
                "offset": 12,
                "length": 1,
                "offsetbit": 0,
                "scale": "1",
                "mask": ""
            },
            {
                "type": "floatle",
                "name": "Hausv_Netz",
                "offset": 16,
                "length": 1,
                "offsetbit": 0,
                "scale": "1",
                "mask": ""
            },
            {
                "type": "floatle",
                "name": "Hausv_PV",
                "offset": 32,
                "length": 1,
                "offsetbit": 0,
                "scale": "1",
                "mask": ""
            },
            {
                "type": "floatle",
                "name": "Power_Limit_EVU",
                "offset": 44,
                "length": 1,
                "offsetbit": 0,
                "scale": "1",
                "mask": ""
            },
            {
                "type": "floatle",
                "name": "cos_phi",
                "offset": 100,
                "length": 1,
                "offsetbit": 0,
                "scale": "1",
                "mask": ""
            },
            {
                "type": "floatle",
                "name": "Frequenz",
                "offset": 104,
                "length": 1,
                "offsetbit": 0,
                "scale": "1",
                "mask": ""
            },
            {
                "type": "floatle",
                "name": "I_L1",
                "offset": 108,
                "length": 1,
                "offsetbit": 0,
                "scale": "1",
                "mask": ""
            },
            {
                "type": "floatle",
                "name": "P_L1",
                "offset": 112,
                "length": 1,
                "offsetbit": 0,
                "scale": "1",
                "mask": ""
            },
            {
                "type": "floatle",
                "name": "U_L1",
                "offset": 116,
                "length": 1,
                "offsetbit": 0,
                "scale": "1",
                "mask": ""
            },
            {
                "type": "floatle",
                "name": "I_L2",
                "offset": 120,
                "length": 1,
                "offsetbit": 0,
                "scale": "1",
                "mask": ""
            },
            {
                "type": "floatle",
                "name": "P_L2",
                "offset": 124,
                "length": 1,
                "offsetbit": 0,
                "scale": "1",
                "mask": ""
            },
            {
                "type": "floatle",
                "name": "U_L2",
                "offset": 128,
                "length": 1,
                "offsetbit": 0,
                "scale": "1",
                "mask": ""
            },
            {
                "type": "floatle",
                "name": "I_L3",
                "offset": 132,
                "length": 1,
                "offsetbit": 0,
                "scale": "1",
                "mask": ""
            },
            {
                "type": "floatle",
                "name": "P_L3",
                "offset": 136,
                "length": 1,
                "offsetbit": 0,
                "scale": "1",
                "mask": ""
            },
            {
                "type": "floatle",
                "name": "U_L3",
                "offset": 140,
                "length": 1,
                "offsetbit": 0,
                "scale": "1",
                "mask": ""
            },
            {
                "type": "floatle",
                "name": "P_AC",
                "offset": 144,
                "length": 1,
                "offsetbit": 0,
                "scale": "1",
                "mask": ""
            },
            {
                "type": "floatle",
                "name": "Q",
                "offset": 148,
                "length": 1,
                "offsetbit": 0,
                "scale": "-1",
                "mask": ""
            },
            {
                "type": "floatle",
                "name": "S",
                "offset": 156,
                "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": 300,
        "y": 100,
        "wires": [
            [
                "14fab764.2ae4f9",
                "2dfc6d72.b19b1a",
                "a0504ce8.b4ba68",
                "abe58c0d.ff7c18",
                "463d785b.3157",
                "456a2eab.c0cd88",
                "b4262f8f.117268",
                "319b5af8281203bb"
            ]
        ]
    },
    {
        "id": "2dfc6d72.b19b1a",
        "type": "function",
        "z": "9fc0f809.f00518",
        "name": "Blindleistung WR",
        "func": "Q = msg.payload.Q;\nif (Q > 9999) {\n    Q = 0;\n}\nQ = parseFloat(Q.toFixed(0));\nmsg.payload = Q;\nmsg.topic = \"Q\";\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 550,
        "y": 180,
        "wires": [
            [
                "190bc38e.03ec84"
            ]
        ]
    },
    {
        "id": "a0504ce8.b4ba68",
        "type": "function",
        "z": "9fc0f809.f00518",
        "name": "Hausverbrauch",
        "func": "Batt = msg.payload.Hausv_Batt;\nBatt = parseFloat(Batt.toFixed(0));\nNetz = msg.payload.Hausv_Netz\nNetz = parseFloat(Netz.toFixed(0));\nPV = msg.payload.Hausv_PV;\nPV = parseFloat(PV.toFixed(0));\nmsg.payload = {Batt,Netz,PV};\nmsg.topic = \"Hausverbrauch\";\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 540,
        "y": 260,
        "wires": [
            [
                "2cad6ab1.46a54e",
                "e7c7f4c3.019088",
                "e1ccaa9f.68df1"
            ]
        ]
    },
    {
        "id": "319b5af8281203bb",
        "type": "function",
        "z": "9fc0f809.f00518",
        "name": "PAC,PDC,PF",
        "func": "PAC=msg.payload.P_AC;\nPAC=parseFloat(PAC.toFixed(0));\nPDC=msg.payload.P_DC;\nPDC=parseFloat(PDC.toFixed(0));\ncosphi=msg.payload.cos_phi;\ncosphi=parseFloat(cosphi.toFixed(2));\nmsg.payload = {PAC,PDC,cosphi};\nmsg.topic = \"WR\";\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 530,
        "y": 40,
        "wires": [
            [
                "8b06bd35.b666c",
                "f43170f9.378dc8",
                "68e29c.70664d64"
            ]
        ]
    },
    {
        "id": "3cf1eb5233154d32",
        "type": "cronplus",
        "z": "9fc0f809.f00518",
        "name": "alle 2s",
        "outputField": "payload",
        "timeZone": "",
        "persistDynamic": false,
        "commandResponseMsgOutput": "output1",
        "outputs": 1,
        "options": [
            {
                "name": "an",
                "topic": "an",
                "payloadType": "bool",
                "payload": "true",
                "expressionType": "cron",
                "expression": "*/2 * * * * *",
                "location": "",
                "offset": "0",
                "solarType": "all",
                "solarEvents": "sunrise,sunset"
            }
        ],
        "x": 90,
        "y": 40,
        "wires": [
            [
                "aa2ccdef.a81888"
            ]
        ]
    },
    {
        "id": "a1b432331279e425",
        "type": "modbus-client",
        "name": "Kostal Plenticore",
        "clienttype": "tcp",
        "bufferCommands": true,
        "stateLogEnabled": false,
        "queueLogEnabled": false,
        "tcpHost": "192.168.74.48",
        "tcpPort": "1502",
        "tcpType": "DEFAULT",
        "serialPort": "/dev/ttyUSB",
        "serialType": "RTU-BUFFERD",
        "serialBaudrate": "9600",
        "serialDatabits": "8",
        "serialStopbits": "1",
        "serialParity": "none",
        "serialConnectionDelay": "100",
        "serialAsciiResponseStartDelimiter": "0x3A",
        "unit_id": 71,
        "commandDelay": 5,
        "clientTimeout": 1000,
        "reconnectOnTimeout": true,
        "reconnectTimeout": 2000,
        "parallelUnitIdsAllowed": false
    }
]

That doesn't really make sense.

Did you try setting length of modbus read to 2 as I suggested. Can you show debug values going into buffer parser. Also share a screenshot of buffer parser settings.

@Steve-Mcl


image

Can you also show the value going in to buffer parser.

Also, I thought you want float values? You have set the buffer parser to make 2 int16 values from whatever you feed into it.

I get a float from de modbus TCP reader but I want a decimal value as output

image

I have some more information:

Type is Float, maybe this will help to solve the problem

Those values are not float. They are 16 bit integers.

Modbus works with integers.

Buffer parser can take the integers (or the buffer) that are produced by modbus & convert them to floating point or whatever value type you require.

If after that, you wish to convert a floating point value to decimal, simply use a function node with a round or floor function.

Tell me what value you think this data :point_up: should end up as? What value are you expecting that sample in your screenshot to become?

The values in data (52319 and 17261) are a high and low word.
This values need to be changed to a 32bit I think so

Yes I understand the concept - but what output value are you expecting these 2 WORDS (4 bytes) to become?

I want 1 value, It must be one value (237V )

Do you have a link to a manual for the modbus device?

I see from your earlier demo you are reading ModBus register 14. What does the manual say is in Register 14? What data type and length?

As it stands, the big endian values of that data parses as ...

image