Whats wrong with my " for loop" and array selection

#1

Hi i need to send several requests to my heater, end i will sort his answer to the right output.
like:

The heater can handel max 4-5 requests in 1 Buffer, so i have to split the requests.

In my function i try to search with a "for each loop" the Hex byte, that for example "0x0d" Boiler.
Then i try to read the next byte "readUInt16BE", witch are the temprature or else.
This message shoud go to the output i whant.

Summary
var buffer = msg.payload;
msg = [];
for (var i = 0; i < buffer.length; i++) {
    if (buffer[i] == 0x0d);        //boiler 
  {
    i++;
    msg [0] = {payload : buffer.readUInt16BE(8)/10};
    continue;//next loop
  }

    if (buffer[i] == 0x0f);        //Abgas
  {
    i++;
    msg [1] = { payload: buffer.readUInt16BE(i) / 10 };
    continue;//next loop
  }

  //if (buf = ....);      //Kessel ........
  //{...}
    if (buffer[i] == 0x7d);
  {
    msg [12] = { payload: "Timestamp" }
  }
    if (buffer[i] != 0x7d);
  {
    continue;
  }
  //else;                   //anything else
  //{
  //continue;}
}
node.warn ()
return msg;

Debug logs only 1 Value, but he can find more.
My inject Buffer was with all request i would do (not the answer of the heater).

I dont think this is the best way to handel it, but once of them i tried :rofl: :rofl: :rofl: you can see a part in the background.

In this case i think it can`t be :thinking: or I cant :thinking: select the byte witch is to read out of the array with [ i ], and if i try "else"in the loop i got error.

If anyone think this is the wrong way, or even think i shoud try/do something else please tell me.

0 Likes

#2

Generate a simple test flow with an inject (or inject + function) to pass in a sample buffer containing your data and then your function node and a debug node to show the output. Then export that flow so we can understand better what you are doing.

A question, are the items in the buffer always in the same place or can there be some missing or in the wrong order? If they are always in the same place then it would be easier to extract them by position.

0 Likes

#3

hi so i try to do ...

[
    {
        "id": "6b7f0f13.02971",
        "type": "tab",
        "label": "Flow 5",
        "disabled": false,
        "info": ""
    },
    {
        "id": "58efb67f.239ed8",
        "type": "inject",
        "z": "6b7f0f13.02971",
        "name": "Werte Abfrage 1",
        "topic": "",
        "payload": "[\"0x7B\",\"0x4D\",\"0x43\",\"0x0D\",\"0xF2\",\"0x0A\",\"0x08\",\"0x00\",\"0x08\",\"0x02\",\"0x30\",\"0x08\",\"0x00\",\"0x0F\",\"0x02\",\"0x31\",\"0x08\",\"0x00\",\"0x0D\",\"0x02\",\"0x32\",\"0x08\",\"0x00\",\"0xA4\",\"0x02\",\"0x33\",\"0x7d\"]",
        "payloadType": "bin",
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "x": 220,
        "y": 200,
        "wires": [
            [
                "9d2bda80.388f38"
            ]
        ]
    },
    {
        "id": "9d2bda80.388f38",
        "type": "function",
        "z": "6b7f0f13.02971",
        "name": "",
        "func": "var buffer = msg.payload;\nmsg = [];\nfor (var i = 0; i < buffer.length; i++) {\n    if (buffer[i] == 0x0d);        //boiler \n  {\n    i++;\n    msg [0] = {payload : buffer.readUInt16BE(8)/10};\n    continue;//next loop\n  }\n\n    if (buffer[i] == 0x0f);        //Abgas\n  {\n    i++;\n    msg [1] = { payload: buffer.readUInt16BE(i) / 10 };\n    continue;//next loop\n  }\n\n  //if (buf = ....);      //Kessel ........\n  //{...}\n    if (buffer[i] == 0x7d);\n  {\n    msg [12] = { payload: \"Timestamp\" }\n  }\n    if (buffer[i] != 0x7d);\n  {\n    continue;\n  }\n  //else;                   //anything else\n  //{\n  //continue;}\n}\nnode.warn ()\nreturn msg;",
        "outputs": 12,
        "noerr": 0,
        "x": 530,
        "y": 200,
        "wires": [
            [
                "ffc25e74.0cdf"
            ],
            [
                "5b3fef8a.96d9"
            ],
            [
                "d1f50e1.5af21f"
            ],
            [],
            [],
            [],
            [],
            [],
            [],
            [],
            [],
            []
        ]
    },
    {
        "id": "ffc25e74.0cdf",
        "type": "debug",
        "z": "6b7f0f13.02971",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "x": 790,
        "y": 120,
        "wires": []
    },
    {
        "id": "5b3fef8a.96d9",
        "type": "debug",
        "z": "6b7f0f13.02971",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "x": 790,
        "y": 160,
        "wires": []
    },
    {
        "id": "d1f50e1.5af21f",
        "type": "debug",
        "z": "6b7f0f13.02971",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "x": 790,
        "y": 200,
        "wires": []
    }
]

i hope this works, this is the first one i try to post an Flow.

--> In my test i see, the Heater returns (mirrored request and answer) in various position, so i have to search for them...
--> it looks like i can only request 4 at a time, so i will crone the requests later. This means he will answer only a few of possible request at ones.

image
This is an example answer, so he mirror my request and put the answer behind.

In the test folw i generate a buffer witch 4 requests and a example answer
mirror Request"0x00","0x0F" answer "0x02","0x31"

0 Likes

#4

The main problem is one that is very difficult to see. Notice that the editor does not correctly indent the lines after your if statements? There is a reason for that. Hint, semi colon terminates a statement.

Also you have readUInt16BE(8) at one point, it should be i

The way to find problems like this is to insert node.warn statements through the code then you will see where it is going.
You might like to look at the javascript Switch statement, it will make the code a little simpler.

0 Likes

#5

thx @Colin

it was the semi colon. :poop: ; :sweat_smile:
I put an node warn at the end, but i shoud read more about warn and how to use.

Now i get more messages thx!

0 Likes

#6

how do you mean this, or how can i do this?
image

0 Likes

#7

Not a switch node, the javascript switch statement. You can use it instead of if
https://www.w3schools.com/js/js_switch.asp

0 Likes

#8

ok thx

at first i think there is an hidden box in Info or... wich i didn't found :grinning:

https://www.w3schools.com is always an open tab in my browser.

0 Likes