Skip to content

Buffer swap and endianness

Klaus Landsdorf edited this page Oct 19, 2017 · 1 revision

Extract from raw Modbus Buffer response

Use the second output msg.payload.buffer object to convert the responding buffer.

extract Buffer

Example flow

Tested with the JS Buffer to extract and swap the buffer as you need your data.

[
    {
        "id": "18ea8042.ed0f2",
        "type": "modbus-read",
        "z": "325a2317.7214fc",
        "name": "Automatic Read FC3",
        "showStatusActivities": false,
        "unitid": "",
        "dataType": "HoldingRegister",
        "adr": "200",
        "quantity": "10",
        "rate": "1",
        "rateUnit": "s",
        "server": "",
        "x": 250,
        "y": 200,
        "wires": [
            [
                "4842691f.156838"
            ],
            [
                "78c57d11.312d94",
                "7c3a39bf.40b348",
                "33239b9c.7605b4",
                "4d64be9f.20935"
            ]
        ]
    },
    {
        "id": "78c57d11.312d94",
        "type": "debug",
        "z": "325a2317.7214fc",
        "name": "",
        "active": false,
        "console": "false",
        "complete": "true",
        "x": 450,
        "y": 240,
        "wires": []
    },
    {
        "id": "7c3a39bf.40b348",
        "type": "function",
        "z": "325a2317.7214fc",
        "name": "extract buffer",
        "func": "return [{ payload: msg.payload.buffer.readInt16LE(0,2)},\n{ payload: msg.payload.buffer.readInt16BE(0,2) }];",
        "outputs": "2",
        "noerr": 0,
        "x": 470,
        "y": 280,
        "wires": [
            [
                "656af465.503b3c"
            ],
            [
                "656af465.503b3c"
            ]
        ]
    },
    {
        "id": "33239b9c.7605b4",
        "type": "modbus-response",
        "z": "325a2317.7214fc",
        "name": "",
        "registerShowMax": 20,
        "x": 490,
        "y": 160,
        "wires": []
    },
    {
        "id": "4842691f.156838",
        "type": "debug",
        "z": "325a2317.7214fc",
        "name": "",
        "active": false,
        "console": "false",
        "complete": "false",
        "x": 470,
        "y": 120,
        "wires": []
    },
    {
        "id": "656af465.503b3c",
        "type": "debug",
        "z": "325a2317.7214fc",
        "name": "",
        "active": false,
        "console": "false",
        "complete": "false",
        "x": 650,
        "y": 280,
        "wires": []
    },
    {
        "id": "4d64be9f.20935",
        "type": "function",
        "z": "325a2317.7214fc",
        "name": "swap buffer",
        "func": "msg.payload.buffer.swap16()\nreturn { payload: msg.payload.buffer };",
        "outputs": "1",
        "noerr": 0,
        "x": 470,
        "y": 320,
        "wires": [
            [
                "6ed544fa.dcce3c"
            ]
        ]
    },
    {
        "id": "6ed544fa.dcce3c",
        "type": "debug",
        "z": "325a2317.7214fc",
        "name": "",
        "active": false,
        "console": "false",
        "complete": "false",
        "x": 650,
        "y": 320,
        "wires": []
    }
]