Help trimming garbage from end of buffer

I've got a solar inverter that I'd like to log to influxdb / grafana. I found a program on github by an author with a similar model to mine. The comms protocols are the same, as are many of the queried parameters, but mine is a newer model with I think more parameters being returned. The program queries the inverter and dumps the serial buffer to stdout, and I'm successfully pulling the string output into node-red.

My first problem was that an 0x0 was being returned for one of the parameters. I thought I had solved that one with a function I found searching here (see function "Replace 0x0), but now I'm getting an "Unexpected end of JSON input" and my msg,payload stops at the spot where the 0x0 was.

Here's the payload straight out of stdout:

And this is where it stops when functions are applied:

I've also got a lot of extra values being sent by the inverter at the end of the payload. I'm pretty sure these are parameter values that the github author doesn't know about yet. How can I trim the end of the payload string where it's recognizable JSON and I can parse the string and make it suitable for influxdb?

Here's my flow:
flows.json (2.9 KB)

[
    {
        "id": "2ac97eb7.c68992",
        "type": "tab",
        "label": "Flow 10",
        "disabled": false,
        "info": ""
    },
    {
        "id": "6efa58aa.fa188",
        "type": "exec",
        "z": "2ac97eb7.c68992",
        "command": "bash /home/pi/inverter.sh",
        "addpay": true,
        "append": "-u",
        "useSpawn": "false",
        "timer": "15",
        "oldrc": false,
        "name": "",
        "x": 230,
        "y": 200,
        "wires": [
            [
                "16c80eb2.5bed29",
                "7826834a.309314"
            ],
            [],
            []
        ]
    },
    {
        "id": "1a126ecb.1d5ac9",
        "type": "debug",
        "z": "2ac97eb7.c68992",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "x": 910,
        "y": 240,
        "wires": []
    },
    {
        "id": "ba80ce4b.495fe",
        "type": "inject",
        "z": "2ac97eb7.c68992",
        "name": "",
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": "0.1",
        "x": 120,
        "y": 120,
        "wires": [
            [
                "6efa58aa.fa188"
            ]
        ]
    },
    {
        "id": "5b024b1a.f178ac",
        "type": "json",
        "z": "2ac97eb7.c68992",
        "name": "",
        "property": "payload",
        "action": "str",
        "pretty": true,
        "x": 770,
        "y": 240,
        "wires": [
            [
                "1a126ecb.1d5ac9"
            ]
        ]
    },
    {
        "id": "b894d2a.bc4833",
        "type": "function",
        "z": "2ac97eb7.c68992",
        "name": "convert",
        "func": "msg.payload = msg.payload.toString('utf8');\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 620,
        "y": 240,
        "wires": [
            [
                "5b024b1a.f178ac"
            ]
        ]
    },
    {
        "id": "16c80eb2.5bed29",
        "type": "debug",
        "z": "2ac97eb7.c68992",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "x": 910,
        "y": 200,
        "wires": []
    },
    {
        "id": "7826834a.309314",
        "type": "function",
        "z": "2ac97eb7.c68992",
        "name": "Replace 0x0",
        "func": "var zeroIndex = msg.payload.indexOf(0)\nvar newBuffer = Buffer.allocUnsafe(zeroIndex);\nmsg.payload.copy(newBuffer,0,0,zeroIndex)\nmsg.payload = newBuffer.toString();\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 450,
        "y": 240,
        "wires": [
            [
                "b894d2a.bc4833"
            ]
        ]
    }
]

After you convert buffer to string, you could string replace the AC_charge_on: with AC_charge_on:0 then it'll be valid JSON.

I'm fairness, you should raise an issue & post the screenshot on his repo.

Better still, look through the code - I bet it's quite simple to replicate in a pure node-red flow.

Thank you very much for your suggestions. I've posted the issue to the author's github repo. I even found what I "think" might be the issue with his code, but no word back yet. It hasn't been very active in a while.

I've also tried what you suggested and converted the raw buffer to a string and then replaced AC_charge_on: with AC_charge_on:0 with a "change node":


This resulted in the desired "payload: string" values as shown in the debug results:

However, feeding that payload to a JSON node to "convert between JSON string & object" (which I need to feed to influxdb) still results in "Unexpected token in JSON at position 508".
image

From the debug node connected directly to the stdout of the executable, the offending "0x0" character still exists in the flow, although it's one position earlier than reported by the error: (This data is not available in the debug error msg connected to the JSON node)

With the payload having been converted and listed in debug as a "String" instead of a "buffer", how is it still finding that offending "0x0"? How can I get rid of it so that I end up with a properly formatted JSON object?

It's crazy how much I've learned in just a few days of studying javascript, JSON, and node-red, but the further I go, the more it's clear that I have a long way to go. Any additional pointers would be appreciated.

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