How to send this HEX command to my device using Node-red


This is the configuration of serial in Keyto

ok, but why do you have it set to send strings - set this to binary / buffer

Also...

can you attach a debug node to the output of function 4 so we can see what is being sent into the serial node?

Yes of course, the output of the function is: [ 170, 63, 0, 17 ]
And when pressing on the output, it opens and shows this:
buffer[4]
0: 0xaa
1: 0x3f
2: 0x0
3: 0x11

Which in both cases are not right and should not be like this!

which then leads to the question 'what is inside "function 4"'

See my previous post...

Can you please suggest a way to write the function? Before I start looking onto the buffer maker. Since I do not know what endianness is..

Just an observation, Steve asked 3 or 4 times what the content of function 4 is...

Oh I thought I answered what's the content of function 4. It's just this code:

var Arr1 = Buffer.from([0xaa,0x3f,0x00,0x00,0xf0,0x11]);
msg.payload = Arr1;
return msg;


I have done this now, in the buffer maker node I put the type to hex and send buffer: [0xaa,0x3f,0x00,0x00,0xf0,0x11]
This is leading to an error: Syntax error, unexpected token x in JSON at position 2.
In the buffer parser I have changed the output to: array (of objects)
And the type to: hex

Your function code 2- works , as I've just done the same experiment.

var buf = Buffer.from([0xaa, 0x3f, 0x00, 0x00, 0xf0, 0x11]);
msg.payload = buf;
return msg;

Code of my flow

[
    {
        "id": "4a65bd5989e6e48a",
        "type": "tab",
        "label": "流程 1",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "deb4b3071092f4b4",
        "type": "serial-port",
        "serialport": "/dev/ttyS4",
        "serialbaud": "9600",
        "databits": "8",
        "parity": "none",
        "stopbits": "1",
        "waitfor": "",
        "dtr": "none",
        "rts": "none",
        "cts": "none",
        "dsr": "none",
        "newline": "1",
        "bin": "false",
        "out": "time",
        "addchar": "",
        "responsetimeout": "10000"
    },
    {
        "id": "d4aee0b6830813cf",
        "type": "serial out",
        "z": "4a65bd5989e6e48a",
        "name": "",
        "serial": "deb4b3071092f4b4",
        "x": 400,
        "y": 320,
        "wires": []
    },
    {
        "id": "f0a90340ad0b821f",
        "type": "debug",
        "z": "4a65bd5989e6e48a",
        "name": "debug 1",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 440,
        "y": 220,
        "wires": []
    },
    {
        "id": "daab7c3dfd21638b",
        "type": "function",
        "z": "4a65bd5989e6e48a",
        "name": "function 1",
        "func": "var buf = Buffer.from([0xaa, 0x3f, 0x00, 0x00, 0xf0, 0x11]);\nmsg.payload = buf;\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 220,
        "y": 280,
        "wires": [
            [
                "f0a90340ad0b821f",
                "d4aee0b6830813cf"
            ]
        ]
    },
    {
        "id": "782bddc5e8783cf7",
        "type": "inject",
        "z": "4a65bd5989e6e48a",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 150,
        "y": 200,
        "wires": [
            [
                "daab7c3dfd21638b"
            ]
        ]
    }
]

Thank you for your time, I tried the exact function and I attached a debugger to the "serial in" node, it is giving this error: "TypeError: The List[1]" argument must be an instance of Buffer or Unit8Array. Received type is string (' ')"
And the node will keep in the waiting mode.
Can you please check if this error is coming to you?
And what is the code of the flow? Where to see it?

If this is what you have in function 4 you must have changed it because I can say 100% certainty you would not get [ 170, 63, 0, 17 ]

You WOULD get [170,63,0,0,240,17]

image


  1. Please confirm the payload going → to → Serial Node (what do you see in debug)
  2. Did you change the Serial Port setting to "deliver" binary / buffer?

Using buffer-parser / buffer-maker...

Here is a demo of making a buffer, calculating a CRC then appending it to the data, to generate a final buffer for transmission to a serial or network device...

Demo Flow (use CTRL-I to import)

[{"id":"1b1e6cd3345cb250","type":"inject","z":"f888b812e6b0fc73","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"aa2b0000","payloadType":"str","x":880,"y":740,"wires":[["c3e3c4e9c75f1ebb"]]},{"id":"e0d2df440975a5ea","type":"debug","z":"f888b812e6b0fc73","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"crc","targetType":"msg","statusVal":"","statusType":"auto","x":1320,"y":800,"wires":[]},{"id":"9f66087a29681310","type":"function","z":"f888b812e6b0fc73","name":"Calculate CRC16","func":"function crc16(data) {\n    const buf = Buffer.isBuffer(data) ? data : Buffer.from(data, 'hex')\n    let crc = 0xFFFF;\n    for (let i = 0; i < buf.length; i++) {\n        crc = crc ^ buf[i];\n        for (let j = 0; j < 8; j++) {\n            const temp = crc & 0x01;\n            crc >>= 0x01;\n            if (temp == 0x01) {\n                crc ^= 0xA001;\n            }\n        }\n    }\n    return crc;\n}\n\nmsg.crc = crc16(msg.data)\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1290,"y":740,"wires":[["e0d2df440975a5ea","742cb4936f6b8153"]]},{"id":"c3e3c4e9c75f1ebb","type":"buffer-maker","z":"f888b812e6b0fc73","name":"","specification":"spec","specificationType":"ui","items":[{"name":"data","type":"hex","length":-1,"dataType":"msg","data":"payload"}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"data","msgPropertyType":"str","x":1090,"y":740,"wires":[["8c51ca3a6bfaf4f5","9f66087a29681310"]]},{"id":"8c51ca3a6bfaf4f5","type":"debug","z":"f888b812e6b0fc73","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"data","targetType":"msg","statusVal":"","statusType":"auto","x":1100,"y":800,"wires":[]},{"id":"742cb4936f6b8153","type":"buffer-maker","z":"f888b812e6b0fc73","name":"join data + crc","specification":"spec","specificationType":"ui","items":[{"name":"data","type":"buffer","length":-1,"dataType":"msg","data":"data"},{"name":"crc","type":"uint16be","length":1,"dataType":"msg","data":"crc"}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","x":1520,"y":740,"wires":[["ed4b127af8f41b76"]]},{"id":"ed4b127af8f41b76","type":"debug","z":"f888b812e6b0fc73","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1550,"y":800,"wires":[]},{"id":"a1e71dedcb1e9749","type":"inject","z":"f888b812e6b0fc73","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"aa3f0000","payloadType":"str","x":880,"y":700,"wires":[["c3e3c4e9c75f1ebb"]]},{"id":"93d14ab247ad849f","type":"inject","z":"f888b812e6b0fc73","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"aa1a0002ffff","payloadType":"str","x":890,"y":780,"wires":[["c3e3c4e9c75f1ebb"]]}]
1 Like

Thanks for replying, yes the content of the function and the reply before was for this:

Then I rewrote it in this form:

  • I have changed the Serial Port Setting to delivering binary buffer.
  • The error:

is coming from the Serial Node and not from the debugger attached to it. The serial node will stay waiting until I redeploy something and then also no response from the debugger attached to the serial node.

I have written the command in string in the inject node and connected it to the buffer maker with type hex and length 12. I am getting the same error.
Regarding the "calculate CRC 16" function, I do not know what's inside it and it is not there in the examples.

there is more going on that you have shown

  • is there more than one node named "keyto"?

  • when you hover over that error which node is highlighted?

  • your "deploy" button is lit-up - is what I am seeing actually deployed?


PS, why are you sending photographs? Can you not connect to the node-red server/device using its IP? e.g. http://IP-OF-NODE-RED:1880. Then you can use the "Copy Value" button on the debug nodes & take screenshots on your PC/Laptop instead of taking photos of a monitor?!?

  • I have disabled all the serial nodes named ''keyto'' except the one in the image.
  • When hovering over the error the serial node is highlighted.
  • The image quality was not good and I'm sorry for that; no the deploy button is not lit-up.

can you export this flow (CTRL-E) and paste it into a reply

```
paste copied flow between three backticks like this
```

The flow is:

[
    {
        "id": "e7e0acf8ed94f75e",
        "type": "tab",
        "label": "Keyto",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "067e0acd2d271669",
        "type": "inject",
        "z": "e7e0acf8ed94f75e",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "aa3f0000f011",
        "payloadType": "str",
        "x": 190,
        "y": 1000,
        "wires": [
            [
                "0e3b76dd644213b3"
            ]
        ]
    },
    {
        "id": "0e3b76dd644213b3",
        "type": "buffer-maker",
        "z": "e7e0acf8ed94f75e",
        "name": "",
        "specification": "spec",
        "specificationType": "ui",
        "items": [
            {
                "name": "item1",
                "type": "hex",
                "length": 12,
                "dataType": "msg",
                "data": "payload"
            }
        ],
        "swap1": "",
        "swap2": "swap16",
        "swap3": "",
        "swap1Type": "swap",
        "swap2Type": "swap",
        "swap3Type": "swap",
        "msgProperty": "payload",
        "msgPropertyType": "str",
        "x": 410,
        "y": 1000,
        "wires": [
            [
                "1defe32f1f4fd013",
                "197bffe1203be388"
            ]
        ]
    },
    {
        "id": "1defe32f1f4fd013",
        "type": "debug",
        "z": "e7e0acf8ed94f75e",
        "name": "debug 13",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 400,
        "y": 1120,
        "wires": []
    },
    {
        "id": "197bffe1203be388",
        "type": "serial request",
        "z": "e7e0acf8ed94f75e",
        "name": "keyto",
        "serial": "22f712114a9952dd",
        "x": 670,
        "y": 1000,
        "wires": [
            [
                "b30f1f7458ccaa75"
            ]
        ]
    },
    {
        "id": "b30f1f7458ccaa75",
        "type": "debug",
        "z": "e7e0acf8ed94f75e",
        "name": "debug 14",
        "active": true,
        "tosidebar": true,
        "console": true,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 690,
        "y": 1120,
        "wires": []
    },
    {
        "id": "22f712114a9952dd",
        "type": "serial-port",
        "serialport": "COM1",
        "serialbaud": "38400",
        "databits": "8",
        "parity": "none",
        "stopbits": "1",
        "waitfor": "",
        "dtr": "none",
        "rts": "none",
        "cts": "none",
        "dsr": "none",
        "newline": "50",
        "bin": "bin",
        "out": "interbyte",
        "addchar": "  ",
        "responsetimeout": "10000"
    }
]

@Steve-Mcl

Ok - there is a small bug in serial port in that it isn't quite doing what it is supposed to... - but you have somehow managed to add two spaces in the "add to output" field

  • the node is failing to append these two spaces (which is the bug causing the error) - but you need to remove those spaces... and then it will work...
1 Like

Beat me to it :wink:

Confirmed - removing the spaces from "Add character to output" does fix this...

1 Like

v1.0.2 now on flows (but the fix will mean the buffer would then contain 20 20 at the end :slight_smile:

1 Like