Converting an array of bytes into a int16 to write in a modbus server(holding register)

Hello guys,

I'm trying to convert an array of bytes(16bytes exactly) that I'm receiving from a function to an int16 to be able to enter it in a modbus holding register but I don't understand how to convert it. I tried with the buffer parser and it didn't work (might be that don't know how to use it).

Important Info: I must absolutely do the conversion from this array

Do you know a way to do the conversion?

An example of what I want to be able to do:

The node you will need courtesy of @Steve-Mcl

Have not used this node myself, but seems to be the goto for this requirement

1 Like

I might be really dumb but i tried to use it multiple times looking throught the forums and tutorials but I didn't manage to make it work

This what I have when I use It:

I don't understand the parameters

1 Like

What does the input data look like and what do you expect out of it?

PS, for modbus, you likely need UINT16be not INT16be

So here's my flow

[
    {
        "id": "1378ba83.90c825",
        "type": "tab",
        "label": "Read file",
        "disabled": false,
        "info": ""
    },
    {
        "id": "20b32e98.5e9fb2",
        "type": "file in",
        "z": "1378ba83.90c825",
        "name": "RequĂȘte1",
        "filename": "C:\\Users\\Theodros\\Documents\\GRTgaz\\NODE-RED_GRTgaz\\Tables modbus\\Requete1.csv",
        "format": "utf8",
        "chunk": false,
        "sendError": false,
        "encoding": "none",
        "allProps": false,
        "x": 300,
        "y": 460,
        "wires": [
            [
                "a8436e2f.135fe"
            ]
        ]
    },
    {
        "id": "a8436e2f.135fe",
        "type": "csv",
        "z": "1378ba83.90c825",
        "name": "",
        "sep": ",",
        "hdrin": false,
        "hdrout": "all",
        "multi": "mult",
        "ret": "\\r\\n",
        "temp": "label",
        "skip": "0",
        "strings": true,
        "include_empty_strings": "",
        "include_null_values": "",
        "x": 450,
        "y": 460,
        "wires": [
            [
                "9e327bbf.9c3c58",
                "93cf18f7.648228"
            ]
        ]
    },
    {
        "id": "9e327bbf.9c3c58",
        "type": "debug",
        "z": "1378ba83.90c825",
        "name": "",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 630,
        "y": 420,
        "wires": []
    },
    {
        "id": "93cf18f7.648228",
        "type": "function",
        "z": "1378ba83.90c825",
        "name": "REMPLISSAGE",
        "func": "msg.options = msg.payload.map((el, index) => {\n\nreturn { [el.label] : index }\n    \n})\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 680,
        "y": 540,
        "wires": [
            [
                "a33a2128.40e88"
            ]
        ]
    },
    {
        "id": "9232e59c366097f5",
        "type": "file in",
        "z": "1378ba83.90c825",
        "name": "RequĂȘte2",
        "filename": "C:\\Users\\Theodros\\Documents\\GRTgaz\\NODE-RED_GRTgaz\\Tables modbus\\Requete2.csv",
        "format": "utf8",
        "chunk": false,
        "sendError": false,
        "encoding": "none",
        "allProps": false,
        "x": 300,
        "y": 700,
        "wires": [
            [
                "46283feae83669d0"
            ]
        ]
    },
    {
        "id": "bfa7ea1854572131",
        "type": "debug",
        "z": "1378ba83.90c825",
        "name": "",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 630,
        "y": 660,
        "wires": []
    },
    {
        "id": "7b54af6a49744acb",
        "type": "function",
        "z": "1378ba83.90c825",
        "name": "REMPLISSAGE",
        "func": "msg.options = msg.payload.map((el, index) => {\n\nreturn { [el.label] : index }\n    \n})\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 680,
        "y": 780,
        "wires": [
            [
                "7a1076f8bb47d19f"
            ]
        ]
    },
    {
        "id": "55ca832546785f2b",
        "type": "file in",
        "z": "1378ba83.90c825",
        "name": "RequĂȘte3",
        "filename": "C:\\Users\\Theodros\\Documents\\GRTgaz\\NODE-RED_GRTgaz\\Tables modbus\\Requete3.csv",
        "format": "utf8",
        "chunk": false,
        "sendError": false,
        "encoding": "none",
        "allProps": false,
        "x": 300,
        "y": 960,
        "wires": [
            [
                "db9bd5da2b417c25"
            ]
        ]
    },
    {
        "id": "4661fbad8601b1c4",
        "type": "debug",
        "z": "1378ba83.90c825",
        "name": "",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 630,
        "y": 920,
        "wires": []
    },
    {
        "id": "fcf264a7e9715206",
        "type": "function",
        "z": "1378ba83.90c825",
        "name": "REMPLISSAGE",
        "func": "msg.options = msg.payload.map((el, index) => {\n\nreturn { [el.label] : index }\n    \n})\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 680,
        "y": 1040,
        "wires": [
            [
                "99de5655e7178068"
            ]
        ]
    },
    {
        "id": "a33a2128.40e88",
        "type": "ui_dropdown",
        "z": "1378ba83.90c825",
        "name": "",
        "label": "",
        "tooltip": "",
        "place": "Select option",
        "group": "671cb6d0f0a7d1c7",
        "order": 5,
        "width": 0,
        "height": 0,
        "passthru": true,
        "multiple": false,
        "options": [],
        "payload": "",
        "topic": "S1",
        "topicType": "str",
        "className": "",
        "x": 860,
        "y": 540,
        "wires": [
            [
                "b7b1f645eb9b83c0"
            ]
        ]
    },
    {
        "id": "4dd9ce82a81007d0",
        "type": "ui_button",
        "z": "1378ba83.90c825",
        "name": "",
        "group": "ac5f4946e8c230e7",
        "order": 1,
        "width": 0,
        "height": 0,
        "passthru": true,
        "label": "button",
        "tooltip": "",
        "color": "",
        "bgcolor": "",
        "className": "",
        "icon": "",
        "payload": "",
        "payloadType": "date",
        "topic": "topic",
        "topicType": "msg",
        "x": 70,
        "y": 700,
        "wires": [
            [
                "20b32e98.5e9fb2",
                "9232e59c366097f5",
                "55ca832546785f2b"
            ]
        ]
    },
    {
        "id": "458310f8128b08be",
        "type": "ui_switch",
        "z": "1378ba83.90c825",
        "name": "",
        "label": "switch",
        "tooltip": "",
        "group": "671cb6d0f0a7d1c7",
        "order": 2,
        "width": "1",
        "height": "1",
        "passthru": true,
        "decouple": "false",
        "topic": "B1",
        "topicType": "str",
        "style": "",
        "onvalue": "1",
        "onvalueType": "num",
        "onicon": "",
        "oncolor": "",
        "offvalue": "0",
        "offvalueType": "num",
        "officon": "",
        "offcolor": "",
        "animate": false,
        "className": "",
        "x": 850,
        "y": 480,
        "wires": [
            [
                "b7b1f645eb9b83c0"
            ]
        ]
    },
    {
        "id": "46283feae83669d0",
        "type": "csv",
        "z": "1378ba83.90c825",
        "name": "",
        "sep": ",",
        "hdrin": false,
        "hdrout": "all",
        "multi": "mult",
        "ret": "\\r\\n",
        "temp": "label",
        "skip": "0",
        "strings": true,
        "include_empty_strings": "",
        "include_null_values": "",
        "x": 450,
        "y": 700,
        "wires": [
            [
                "bfa7ea1854572131",
                "7b54af6a49744acb"
            ]
        ]
    },
    {
        "id": "db9bd5da2b417c25",
        "type": "csv",
        "z": "1378ba83.90c825",
        "name": "",
        "sep": ",",
        "hdrin": false,
        "hdrout": "all",
        "multi": "mult",
        "ret": "\\r\\n",
        "temp": "label",
        "skip": "0",
        "strings": true,
        "include_empty_strings": "",
        "include_null_values": "",
        "x": 450,
        "y": 960,
        "wires": [
            [
                "fcf264a7e9715206",
                "4661fbad8601b1c4"
            ]
        ]
    },
    {
        "id": "b4e1c71defe23048",
        "type": "link in",
        "z": "1378ba83.90c825",
        "name": "Valeur choix1",
        "links": [
            "79e4d93ab0c66a1c"
        ],
        "x": 165,
        "y": 1380,
        "wires": [
            [
                "5a6587d2222c2220"
            ]
        ]
    },
    {
        "id": "f71362bbb83da4c9",
        "type": "link in",
        "z": "1378ba83.90c825",
        "name": "choix1",
        "links": [
            "16abf2150784fc77"
        ],
        "x": 165,
        "y": 1440,
        "wires": [
            [
                "5a6587d2222c2220"
            ]
        ]
    },
    {
        "id": "d0b9e76692aadad7",
        "type": "ui_button",
        "z": "1378ba83.90c825",
        "name": "",
        "group": "671cb6d0f0a7d1c7",
        "order": 1,
        "width": 0,
        "height": 0,
        "passthru": false,
        "label": "envoyer",
        "tooltip": "",
        "color": "",
        "bgcolor": "",
        "className": "",
        "icon": "",
        "payload": "true",
        "payloadType": "bool",
        "topic": "topic",
        "topicType": "msg",
        "x": 200,
        "y": 1220,
        "wires": [
            []
        ]
    },
    {
        "id": "5a6587d2222c2220",
        "type": "function",
        "z": "1378ba83.90c825",
        "name": "Adressage",
        "func": "table = [];\nadressage = [];\nvar num = msg.payload.S1; //Chiffre allant de 0 à 79\nvar val =  msg.payload.B1; // booléen\n\nfor(i = 0; i<79 ; i++){\ntable[i] = 0  ;\n}\n\ntable[num] = val; //On assigne une valeur à l'adresse saisie\n\nif(num < 15){\n    for(i = 0; i<16 ; i++){\n     adressage[i] = table[i]; //It returns the whole table\n    }   \n   \n}\n\nmsg.payload = adressage;\nreturn msg",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 470,
        "y": 1400,
        "wires": [
            [
                "19f79ff39da8bb1b"
            ]
        ]
    },
    {
        "id": "6ee3aaf7d2238cc2",
        "type": "change",
        "z": "1378ba83.90c825",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "selected",
                "pt": "flow",
                "to": "payload",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1200,
        "y": 500,
        "wires": [
            []
        ]
    },
    {
        "id": "e72aa291dd82fd34",
        "type": "change",
        "z": "1378ba83.90c825",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "selected",
                "tot": "flow"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 380,
        "y": 1280,
        "wires": [
            [
                "5a6587d2222c2220"
            ]
        ]
    },
    {
        "id": "b7b1f645eb9b83c0",
        "type": "join",
        "z": "1378ba83.90c825",
        "name": "",
        "mode": "custom",
        "build": "object",
        "property": "payload",
        "propertyType": "msg",
        "key": "topic",
        "joiner": "\\n",
        "joinerType": "str",
        "accumulate": true,
        "timeout": "",
        "count": "1",
        "reduceRight": false,
        "reduceExp": "",
        "reduceInit": "",
        "reduceInitType": "",
        "reduceFixup": "",
        "x": 1010,
        "y": 500,
        "wires": [
            [
                "6ee3aaf7d2238cc2"
            ]
        ]
    },
    {
        "id": "7a1076f8bb47d19f",
        "type": "ui_dropdown",
        "z": "1378ba83.90c825",
        "name": "",
        "label": "",
        "tooltip": "",
        "place": "Select option",
        "group": "846c632af12c3e74",
        "order": 5,
        "width": 0,
        "height": 0,
        "passthru": true,
        "multiple": false,
        "options": [],
        "payload": "",
        "topic": "S1",
        "topicType": "str",
        "className": "",
        "x": 860,
        "y": 780,
        "wires": [
            [
                "7007a963b3ae03c0"
            ]
        ]
    },
    {
        "id": "c9957cf08822b7a0",
        "type": "ui_switch",
        "z": "1378ba83.90c825",
        "name": "",
        "label": "switch",
        "tooltip": "",
        "group": "846c632af12c3e74",
        "order": 2,
        "width": "1",
        "height": "1",
        "passthru": true,
        "decouple": "false",
        "topic": "B1",
        "topicType": "str",
        "style": "",
        "onvalue": "1",
        "onvalueType": "num",
        "onicon": "",
        "oncolor": "",
        "offvalue": "0",
        "offvalueType": "num",
        "officon": "",
        "offcolor": "",
        "animate": false,
        "className": "",
        "x": 850,
        "y": 720,
        "wires": [
            [
                "7007a963b3ae03c0"
            ]
        ]
    },
    {
        "id": "eeac4a72b9dc5d0e",
        "type": "change",
        "z": "1378ba83.90c825",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "selected",
                "pt": "flow",
                "to": "payload",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1200,
        "y": 740,
        "wires": [
            []
        ]
    },
    {
        "id": "7007a963b3ae03c0",
        "type": "join",
        "z": "1378ba83.90c825",
        "name": "",
        "mode": "custom",
        "build": "object",
        "property": "payload",
        "propertyType": "msg",
        "key": "topic",
        "joiner": "\\n",
        "joinerType": "str",
        "accumulate": true,
        "timeout": "",
        "count": "1",
        "reduceRight": false,
        "reduceExp": "",
        "reduceInit": "",
        "reduceInitType": "",
        "reduceFixup": "",
        "x": 1010,
        "y": 740,
        "wires": [
            [
                "eeac4a72b9dc5d0e"
            ]
        ]
    },
    {
        "id": "99de5655e7178068",
        "type": "ui_dropdown",
        "z": "1378ba83.90c825",
        "name": "",
        "label": "",
        "tooltip": "",
        "place": "Select option",
        "group": "25e2db2699b87ee1",
        "order": 5,
        "width": 0,
        "height": 0,
        "passthru": true,
        "multiple": false,
        "options": [],
        "payload": "",
        "topic": "S1",
        "topicType": "str",
        "className": "",
        "x": 860,
        "y": 1040,
        "wires": [
            [
                "5676a3cd7ef5e23d"
            ]
        ]
    },
    {
        "id": "3dd6b08423eb1dd1",
        "type": "ui_switch",
        "z": "1378ba83.90c825",
        "name": "",
        "label": "switch",
        "tooltip": "",
        "group": "25e2db2699b87ee1",
        "order": 2,
        "width": "1",
        "height": "1",
        "passthru": true,
        "decouple": "false",
        "topic": "B1",
        "topicType": "str",
        "style": "",
        "onvalue": "1",
        "onvalueType": "num",
        "onicon": "",
        "oncolor": "",
        "offvalue": "0",
        "offvalueType": "num",
        "officon": "",
        "offcolor": "",
        "animate": false,
        "className": "",
        "x": 850,
        "y": 980,
        "wires": [
            [
                "5676a3cd7ef5e23d"
            ]
        ]
    },
    {
        "id": "5fc121eeade2156a",
        "type": "change",
        "z": "1378ba83.90c825",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "selected",
                "pt": "flow",
                "to": "payload",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1200,
        "y": 1000,
        "wires": [
            []
        ]
    },
    {
        "id": "5676a3cd7ef5e23d",
        "type": "join",
        "z": "1378ba83.90c825",
        "name": "",
        "mode": "custom",
        "build": "object",
        "property": "payload",
        "propertyType": "msg",
        "key": "topic",
        "joiner": "\\n",
        "joinerType": "str",
        "accumulate": true,
        "timeout": "",
        "count": "1",
        "reduceRight": false,
        "reduceExp": "",
        "reduceInit": "",
        "reduceInitType": "",
        "reduceFixup": "",
        "x": 1010,
        "y": 1000,
        "wires": [
            [
                "5fc121eeade2156a"
            ]
        ]
    },
    {
        "id": "3ffeefaa5596a04c",
        "type": "debug",
        "z": "1378ba83.90c825",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 810,
        "y": 1400,
        "wires": []
    },
    {
        "id": "8d613ded2d4ecc2e",
        "type": "ui_button",
        "z": "1378ba83.90c825",
        "name": "",
        "group": "671cb6d0f0a7d1c7",
        "order": 1,
        "width": 0,
        "height": 0,
        "passthru": false,
        "label": "Valider",
        "tooltip": "",
        "color": "",
        "bgcolor": "",
        "className": "",
        "icon": "",
        "payload": "",
        "payloadType": "date",
        "topic": "topic",
        "topicType": "msg",
        "x": 190,
        "y": 1280,
        "wires": [
            [
                "e72aa291dd82fd34"
            ]
        ]
    },
    {
        "id": "19f79ff39da8bb1b",
        "type": "buffer-parser",
        "z": "1378ba83.90c825",
        "name": "",
        "data": "payload",
        "dataType": "msg",
        "specification": "spec",
        "specificationType": "ui",
        "items": [
            {
                "type": "uint16be",
                "name": "item1",
                "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": 630,
        "y": 1400,
        "wires": [
            [
                "3ffeefaa5596a04c"
            ]
        ]
    },
    {
        "id": "671cb6d0f0a7d1c7",
        "type": "ui_group",
        "name": "RequĂȘte 1",
        "tab": "3f071cb9a3d49f7f",
        "order": 1,
        "disp": true,
        "width": "6",
        "collapse": false,
        "className": ""
    },
    {
        "id": "ac5f4946e8c230e7",
        "type": "ui_group",
        "name": "Initialisation des listes déroulantes",
        "tab": "3f071cb9a3d49f7f",
        "order": 4,
        "disp": true,
        "width": "6",
        "collapse": false,
        "className": ""
    },
    {
        "id": "846c632af12c3e74",
        "type": "ui_group",
        "name": "RequĂȘte 2",
        "tab": "3f071cb9a3d49f7f",
        "order": 1,
        "disp": true,
        "width": "6",
        "collapse": false,
        "className": ""
    },
    {
        "id": "25e2db2699b87ee1",
        "type": "ui_group",
        "name": "RequĂȘte 3",
        "tab": "3f071cb9a3d49f7f",
        "order": 3,
        "disp": true,
        "width": "6",
        "collapse": false,
        "className": ""
    },
    {
        "id": "3f071cb9a3d49f7f",
        "type": "ui_tab",
        "name": "COMPRESSEUR",
        "icon": "COMPRESSEUR",
        "disabled": false,
        "hidden": false
    }
]

I'm inputing data from the dashboard as a number (0 or 1) and putting it inside a first array going from 0 to 79. Then from this array, I extract 16 numbers that i put inside another array to then convert it into an UINT16be
(I don't know if It's clear)

Show me what the data in flow.selected looks like.

Better still, copy some actual input data (using the copy value button)
image

Then explain what that data should be.

e.g. if you show my flow.selected has [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16] what should it look like before it goes to modbus?

  • The first problem might be that I'm inputing numbers in the array (wich are already integer if i'm not wrong), but than how do I feel up my first array with boolean numbers

Here's how it looks like

{"S1":0,"B1":1}

image

How do you expect to make 16 integers from 2 values?

Please explain exactly what you expect the output to be?

I actually have 16, I'm making a drawing. It might be easier to understand

First the person will select the value It wants (B1 = 1 or 0) and wich variable it want's to change (S1 going from 0 to 79) :

image

When validated,
image

I'm putting the value of B1 in an array at the adress S1:
image

The last and missing part is the conversion.

And If you ask why a table of 79, It is because there's 79 variables that can be selected and modified.

ok, so how do you write to modbus - all 79 values at once? or upon value change + button press?

I dont understand where 16 comes into this?

Depending on wich variable the person choose (line 15-17 on the last code pic) I will return a table of 16 that will be converted to UINT16be and after a button press will be sent to write to the holding register of a modbus server.

I am slowly extracting the necessary info from you and trying to peice this together.

correct me if any of this is wrong...

  • User selects a parameter
  • User sets a TRUE or FALSE (1 or 0)
  • User "stores this" setting in flow memory
  • Once user has entered all 16 values, you want to create a 1 WORD (16 bit) value from the 16 "bits" entered - then you write this to ModBus?

No problem It's hard to understand when you're not the one coding.

He just enter one value but that's exactly It :+1:

Thank you for taking your time with my problem

ok, nearly there.

If the user enters 1 for address 0 then that would make {"S1":0,"B1":1} right?

So what value do you want to write to ModBus? Why are you looping 16 times?

What might make it easier to see the bigger picture by providing a few examples.

e.g.

  • user selects address 2 & enters 0, I would get {"S1":2,"B1":0} and would expect to write xxxx to ModBus at address yyyy?
  • user selects address 15 & enters 1, I would get {"S1":15,"B1":1} and would expect to write xxxx to ModBus at address yyyy?