Injecting a string in a function not working

Newbie at work:

In my function node this works:

msg.payload = Buffer.from('-v.?\r')
return msg;

Passing the string from my injection node (as in the flow), it does not work:

msg.payload = Buffer.from(msg.payload)
return msg;

I have tried various variations , but did not find out why.
What am i doing wrong?

Thanks

My flow:

[
    {
        "id": "7ef9dfab0cff67ba",
        "type": "tab",
        "label": "TCP REQUEST",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "cbcde23642237613",
        "type": "tcp request",
        "z": "7ef9dfab0cff67ba",
        "server": "192.168.20.40",
        "port": "50001",
        "out": "time",
        "ret": "buffer",
        "splitc": "1000",
        "name": "",
        "x": 510,
        "y": 200,
        "wires": [
            [
                "c4e9b6d3e5e0ad19",
                "6026b84bc8dc2e79"
            ]
        ]
    },
    {
        "id": "647d2e0b14640b05",
        "type": "debug",
        "z": "7ef9dfab0cff67ba",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 890,
        "y": 240,
        "wires": []
    },
    {
        "id": "522e649cb030ad01",
        "type": "function",
        "z": "7ef9dfab0cff67ba",
        "name": "",
        "func": "msg.payload = Buffer.from(msg.payload)\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 260,
        "y": 240,
        "wires": [
            [
                "cbcde23642237613"
            ]
        ]
    },
    {
        "id": "c4e9b6d3e5e0ad19",
        "type": "function",
        "z": "7ef9dfab0cff67ba",
        "name": "",
        "func": "msg.payload = msg.payload.toString().split('.');\nmsg.payload[0];\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 720,
        "y": 240,
        "wires": [
            [
                "647d2e0b14640b05"
            ]
        ]
    },
    {
        "id": "baf28b66599c876c",
        "type": "inject",
        "z": "7ef9dfab0cff67ba",
        "name": "",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "-p.?\\r",
        "payloadType": "str",
        "x": 80,
        "y": 240,
        "wires": [
            [
                "522e649cb030ad01"
            ]
        ]
    },
    {
        "id": "6026b84bc8dc2e79",
        "type": "debug",
        "z": "7ef9dfab0cff67ba",
        "name": "",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 890,
        "y": 140,
        "wires": []
    }
]

Can you be more specific? Do you get an error or the wrong data in the buffer? Something else?

The inject node string doesn't need \r to be escaped. The inject sends exactly what is entered. However, If you actually need a carriage return then you would need to append that using a function or use JSONata in the inject.

I need the \r, i will try your suggestion!

Thank you very much for the tip!
appreciate it.

The inject node string input does not allow the use of \n \r \t etc it takes strings literally
try using JSON or JSONata or a function node as in these examples

[{"id":"baf28b66599c876c","type":"inject","z":"7ef9dfab0cff67ba","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"-p.?\\r","payloadType":"str","x":80,"y":240,"wires":[["522e649cb030ad01"]]},{"id":"522e649cb030ad01","type":"function","z":"7ef9dfab0cff67ba","name":"","func":"msg.payload = Buffer.from(msg.payload)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":260,"y":240,"wires":[["6026b84bc8dc2e79"]]},{"id":"6026b84bc8dc2e79","type":"debug","z":"7ef9dfab0cff67ba","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":510,"y":180,"wires":[]},{"id":"4e31b19e.e0cd38","type":"function","z":"7ef9dfab0cff67ba","name":"","func":"msg.payload = \"-p.?\\r\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":290,"y":300,"wires":[["522e649cb030ad01"]]},{"id":"cf4039b4.5c2268","type":"inject","z":"7ef9dfab0cff67ba","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"\"-p.?\\r\"","payloadType":"json","x":90,"y":360,"wires":[["522e649cb030ad01"]]},{"id":"6ac4a5f9.547c34","type":"inject","z":"7ef9dfab0cff67ba","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"\"-p.?\\r\"","payloadType":"jsonata","x":90,"y":420,"wires":[["522e649cb030ad01"]]},{"id":"9416954b.60f8b","type":"inject","z":"7ef9dfab0cff67ba","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":110,"y":300,"wires":[["4e31b19e.e0cd38"]]}]

Thank you very much for your hints to a solution.
It was not obvious that the special characters are stripped way.

Now i have a working proof of concept and can work on flows that will be implemented in production.

@E1cid sorry that i did not choose your solution, @Steve-Mcl was first at the finish line.

Cheers, Peter

Not an issue. I only added my response as Steve had omitted the use of JSON. Using JSON has less overhead than JSONata or a function node.

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