Convert data PLC into floating value

Hello,

I read values from a Siemens PLC thanks to the s7 node. However, I have now to convert the data into floating values. I've tried to use buffer-parser or maker, or doing it with a change mode or a function, but the result is always 0. I've seen several topics about this subject on the forum but I didn't find the answer to my problem.

This is my flow :

[
    {
        "id": "176701d716ba4394",
        "type": "tab",
        "label": "Flux 2",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "8cc018e22ffb36c8",
        "type": "s7 in",
        "z": "176701d716ba4394",
        "endpoint": "fed32a73d5582e79",
        "mode": "all",
        "variable": "",
        "diff": true,
        "name": "",
        "x": 460,
        "y": 360,
        "wires": [
            [
                "113b04d62d587a51",
                "78ac1c9bf42bdfc4",
                "84b765e8f9789969"
            ]
        ]
    },
    {
        "id": "113b04d62d587a51",
        "type": "debug",
        "z": "176701d716ba4394",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 650,
        "y": 200,
        "wires": []
    },
    {
        "id": "93b2ff2180088830",
        "type": "function",
        "z": "176701d716ba4394",
        "name": "Dry Bulb + Wet Bulb To Float",
        "func": "// Extraction des données brutes\nvar bufDryBulb = Buffer.alloc(4);\nvar bufWetBulb = Buffer.alloc(4);\n\nbufDryBulb[0] = flow.Dry_Bulb_0_float;\nbufDryBulb[1] = flow.Dry_Bulb_1_float;\nbufDryBulb[2] = flow.Dry_Bulb_2_float;\nbufDryBulb[3] = flow.Dry_Bulb_3_float;\n\nbufWetBulb[0] = flow.Wet_Bulb_0_float;\nbufWetBulb[1] = flow.Wet_Bulb_1_float;\nbufWetBulb[2] = flow.Wet_Bulb_2_float;\nbufWetBulb[3] = flow.Wet_Bulb_3_float;\n\n// Conversion des données brutes en nombre flottant\nvar floatValueDryBulb = bufDryBulb.readFloatBE(0); // Lit le nombre flottant en Big Endian à partir du tampon\nvar floatValueWetBulb = bufWetBulb.readFloatBE(0);\n// Retourne la valeur flottante\nreturn [{ payload: floatValueDryBulb }, { payload: floatValueWetBulb }] ;\n",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 980,
        "y": 360,
        "wires": [
            [
                "be5b7cab248c34c9"
            ]
        ]
    },
    {
        "id": "78ac1c9bf42bdfc4",
        "type": "change",
        "z": "176701d716ba4394",
        "name": "Dry bulb conv + flow context",
        "rules": [
            {
                "t": "set",
                "p": "payload.Dry_Bulb_0_float",
                "pt": "msg",
                "to": "(payload.Dry_Bulb_0)+3",
                "tot": "jsonata"
            },
            {
                "t": "set",
                "p": "payload.Dry_Bulb_1_float",
                "pt": "msg",
                "to": "(payload.Dry_Bulb_1)+2",
                "tot": "jsonata"
            },
            {
                "t": "set",
                "p": "payload.Dry_Bulb_2_float",
                "pt": "msg",
                "to": "(payload.Dry_Bulb_2)+1",
                "tot": "jsonata"
            },
            {
                "t": "set",
                "p": "payload.Dry_Bulb_3_float",
                "pt": "msg",
                "to": "(payload.Dry_Bulb_3)+0",
                "tot": "jsonata"
            },
            {
                "t": "set",
                "p": "Dry_Bulb_0_float",
                "pt": "flow",
                "to": "payload.Dry_Bulb_0_float",
                "tot": "msg"
            },
            {
                "t": "set",
                "p": "Dry_Bulb_1_float",
                "pt": "flow",
                "to": "payload.Dry_Bulb_1_float",
                "tot": "msg"
            },
            {
                "t": "set",
                "p": "Dry_Bulb_2_float",
                "pt": "flow",
                "to": "payload.Dry_Bulb_2_float",
                "tot": "msg"
            },
            {
                "t": "set",
                "p": "Dry_Bulb_3_float",
                "pt": "flow",
                "to": "payload.Dry_Bulb_3_float",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 700,
        "y": 340,
        "wires": [
            [
                "81b83400d8dff551",
                "93b2ff2180088830"
            ]
        ]
    },
    {
        "id": "be5b7cab248c34c9",
        "type": "debug",
        "z": "176701d716ba4394",
        "name": "debug 17",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 1200,
        "y": 360,
        "wires": []
    },
    {
        "id": "81b83400d8dff551",
        "type": "debug",
        "z": "176701d716ba4394",
        "name": "debug 18",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 920,
        "y": 300,
        "wires": []
    },
    {
        "id": "84b765e8f9789969",
        "type": "change",
        "z": "176701d716ba4394",
        "name": "Wet bulb conv + flow context",
        "rules": [
            {
                "t": "set",
                "p": "payload.Wet_Bulb_0_float",
                "pt": "msg",
                "to": "(payload.Wet_Bulb_0)+3",
                "tot": "jsonata"
            },
            {
                "t": "set",
                "p": "payload.Wet_Bulb_1_float",
                "pt": "msg",
                "to": "(payload.Wet_Bulb_1)+2",
                "tot": "jsonata"
            },
            {
                "t": "set",
                "p": "payload.Wet_Bulb_2_float",
                "pt": "msg",
                "to": "(payload.Wet_Bulb_2)+1",
                "tot": "jsonata"
            },
            {
                "t": "set",
                "p": "payload.Wet_Bulb_3_float",
                "pt": "msg",
                "to": "(payload.Wet_Bulb_3)+0",
                "tot": "jsonata"
            },
            {
                "t": "set",
                "p": "Wet_Bulb_0_float",
                "pt": "flow",
                "to": "payload.Wet_Bulb_0_float",
                "tot": "msg"
            },
            {
                "t": "set",
                "p": "Wet_Bulb_1_float",
                "pt": "flow",
                "to": "payload.Wet_Bulb_1_float",
                "tot": "msg"
            },
            {
                "t": "set",
                "p": "Wet_Bulb_2_float",
                "pt": "flow",
                "to": "payload.Wet_Bulb_2_float",
                "tot": "msg"
            },
            {
                "t": "set",
                "p": "Wet_Bulb_3_float",
                "pt": "flow",
                "to": "payload.Wet_Bulb_3_float",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 700,
        "y": 400,
        "wires": [
            [
                "93b2ff2180088830",
                "ff4ca3f2bc562b0b"
            ]
        ]
    },
    {
        "id": "ff4ca3f2bc562b0b",
        "type": "debug",
        "z": "176701d716ba4394",
        "name": "debug 19",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 920,
        "y": 440,
        "wires": []
    },
    {
        "id": "fed32a73d5582e79",
        "type": "s7 endpoint",
        "transport": "iso-on-tcp",
        "address": "192.168.0.1",
        "port": "102",
        "rack": "0",
        "slot": "1",
        "localtsaphi": "01",
        "localtsaplo": "00",
        "remotetsaphi": "01",
        "remotetsaplo": "00",
        "connmode": "rack-slot",
        "adapter": "",
        "busaddr": "2",
        "cycletime": "10000",
        "timeout": "2000",
        "name": "Discovery",
        "vartable": [
            {
                "addr": "DB62,BYTE0",
                "name": "Dry_Bulb_0"
            },
            {
                "addr": "DB62,BYTE1",
                "name": "Dry_Bulb_1"
            },
            {
                "addr": "DB62,BYTE2",
                "name": "Dry_Bulb_2"
            },
            {
                "addr": "DB62,BYTE3",
                "name": "Dry_Bulb_3"
            },
            {
                "addr": "DB62,BYTE4",
                "name": "Wet_Bulb_0"
            },
            {
                "addr": "DB62,BYTE5",
                "name": "Wet_Bulb_1"
            },
            {
                "addr": "DB62,BYTE6",
                "name": "Wet_Bulb_2"
            },
            {
                "addr": "DB62,BYTE7",
                "name": "Wet_Bulb_3"
            }
        ]
    }
]

Here, the decode function in C for floating values (datasheet) :

static float conv_float_s7_pc(char * plc_real)
{
    char tmp[4];
    
    tmp[0] = * (plc_real + 3);
    tmp[1] = * (plc_real + 2);
    tmp[2] = * (plc_real + 1);
    tmp[3] = * (plc_real + 0);  
    
    return(* (float *) tmp);
}

Thank you for your help.

Buffer parser requires array/buffer data. Your data is an object with 4 properties.

What are you requesting from the PLC? 4 individual byte address?
Can you not request a float or even an array of 4 bytes> Even a UINT32 would be better.

Failing that: Put the 4 values into a buffer-maker (to make a buffer) then send that through a buffer parser (to make float)

example:

flow:

[{"id":"63d0c3a4473a5171","type":"buffer-parser","z":"1b95bb0cab495f48","name":"","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"floatle","name":"to_float_le","offset":0,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"floatbe","name":"to_float_be","offset":0,"length":1,"offsetbit":0,"scale":"1","mask":""}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"keyvalue","resultTypeType":"return","multipleResult":false,"fanOutMultipleResult":false,"setTopic":true,"outputs":1,"x":1020,"y":640,"wires":[["c6a067ff5ce028b1"]]},{"id":"72bb41af62fde612","type":"buffer-maker","z":"1b95bb0cab495f48","name":"Dry_Bulb_0~3","specification":"spec","specificationType":"ui","items":[{"name":"item1","type":"byte","length":1,"dataType":"msg","data":"payload.Dry_Bulb_0"},{"name":"item2","type":"byte","length":1,"dataType":"msg","data":"payload.Dry_Bulb_1"},{"name":"item3","type":"byte","length":1,"dataType":"msg","data":"payload.Dry_Bulb_2"},{"name":"item4","type":"byte","length":1,"dataType":"msg","data":"payload.Dry_Bulb_3"}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","x":840,"y":580,"wires":[["dd987b3779b08901","63d0c3a4473a5171"]]},{"id":"c6a067ff5ce028b1","type":"debug","z":"1b95bb0cab495f48","name":"debug 178","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1030,"y":700,"wires":[]},{"id":"beffe9675a24f52a","type":"inject","z":"1b95bb0cab495f48","name":"Faking your data","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"Dry_Bulb_0\":65,\"Dry_Bulb_1\":196,\"Dry_Bulb_2\":120,\"Dry_Bulb_3\":62}","payloadType":"json","x":770,"y":540,"wires":[["72bb41af62fde612"]]},{"id":"dd987b3779b08901","type":"debug","z":"1b95bb0cab495f48","name":"debug 177","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1020,"y":580,"wires":[]}]

Please open: node-red-contrib-s7 (node) - Node-RED (which was proposed to you many times) here it clearly states:

To convert your variables it will be DB62,REAL0 // DB62,REAL4

1 Like

I tried it yesterday and it worked perfectly ! I didn't well understand how the buffer worked (even with the datasheet), but now it's ok. I can read and convert all the data I want.

Thank you for your help and have a good day :slight_smile:

1 Like

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