I want to retrieve data from my sensor. Baud rate is fix at 9600.
My flow works so far, but the communication stops after 1 second because my sensor runs into a WatchDog and restarts.
Therefore, I would like to design the flow and especially the node function in such a way that the 112 bytes can be received in less than 1 second. Attempts with a for loop were unsuccessful, as I can only use a node.send from a function but do not have a node.receive function to receive a new msg object, for example.
Does anyone have any suggestions on how I can optimise the code to make it run faster - or maybe a completely new approach?
Here my flow:
[
{
"id": "dd740ba2282df4a6",
"type": "tab",
"label": "MessfĂĽhler auslesen",
"disabled": false,
"info": ""
},
{
"id": "dde8b67b44289268",
"type": "inject",
"z": "dd740ba2282df4a6",
"name": "sende 0xCB",
"props": [
{
"p": "payload"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "[\"0xCB\"]",
"payloadType": "bin",
"x": 210,
"y": 140,
"wires": [
[
"d26ab8ac82582542"
]
]
},
{
"id": "0b785fa2f97ab2ee",
"type": "function",
"z": "dd740ba2282df4a6",
"name": "",
"func": "var ACK = Buffer.from([0x13]); // When sensor is ready to send data it sends ACK in response to 0xCB\nvar REQ = Buffer.from([0x11]); // Request to sensor = REQ to send another byte\nvar datensatz = flow.get('datensatz') || new Array(111);\nvar counter = flow.get('counter'); \n\nif (Buffer.compare(msg.payload, ACK) === 0 && counter == !0) { // when the ACK arrives, send a REQ to output 1\n node.warn(\"ACK received - send REQ\");\n\n msg.counter = counter;\n msg.payload = REQ;\n\n return [msg, null];\n}\n\nelse {\n\n if (counter < 111) { // If not all data are there yet\n\n datensatz[counter] = msg.payload; // Save received data in data set at counter position\n flow.set('datensatz', datensatz);\n\n counter++; \n flow.set('counter', counter);\n\n msg.counter = counter;\n msg.payload = REQ;\n\n return [msg, null]; // send REQ to output 1\n\n }\n\n else { // counter >= 112\n\n node.warn(\"send Datensatz to output 2\");\n\n flow.set('counter', 0); // reset counter\n msg.counter = counter;\n msg.payload = flow.get('datensatz'); // copy datensatz to msg\n\n return [null, msg]; // output 2 gets compete datensatz\n\n }\n\n}",
"outputs": 2,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 420,
"y": 280,
"wires": [
[
"d26ab8ac82582542",
"34c82f95247b334e"
],
[
"8c0d6351e4d21748",
"275df45533ca8e0a"
]
]
},
{
"id": "8c0d6351e4d21748",
"type": "debug",
"z": "dd740ba2282df4a6",
"name": "datensatz",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": true,
"complete": "payload",
"targetType": "msg",
"statusVal": "payload",
"statusType": "msg",
"x": 660,
"y": 280,
"wires": []
},
{
"id": "d26ab8ac82582542",
"type": "serial request",
"z": "dd740ba2282df4a6",
"name": "",
"serial": "dccc90be94647fa3",
"x": 430,
"y": 140,
"wires": [
[
"6ad5dfb0e9991354",
"0b785fa2f97ab2ee"
]
]
},
{
"id": "6ad5dfb0e9991354",
"type": "debug",
"z": "dd740ba2282df4a6",
"name": "debug 181",
"active": false,
"tosidebar": true,
"console": false,
"tostatus": true,
"complete": "payload",
"targetType": "msg",
"statusVal": "payload",
"statusType": "msg",
"x": 670,
"y": 120,
"wires": []
},
{
"id": "34c82f95247b334e",
"type": "debug",
"z": "dd740ba2282df4a6",
"name": "",
"active": false,
"tosidebar": true,
"console": false,
"tostatus": true,
"complete": "counter",
"targetType": "msg",
"statusVal": "counter",
"statusType": "msg",
"x": 670,
"y": 200,
"wires": []
},
{
"id": "275df45533ca8e0a",
"type": "debug",
"z": "dd740ba2282df4a6",
"name": "",
"active": false,
"tosidebar": true,
"console": false,
"tostatus": true,
"complete": "counter",
"targetType": "msg",
"statusVal": "counter",
"statusType": "msg",
"x": 670,
"y": 360,
"wires": []
},
{
"id": "4f1a9ec07a2ddef1",
"type": "config",
"z": "dd740ba2282df4a6",
"name": "",
"properties": [
{
"p": "datensatz",
"pt": "flow",
"to": "[ ]",
"tot": "json"
},
{
"p": "counter",
"pt": "flow",
"to": "0",
"tot": "num"
},
{
"p": "DatenErhalten",
"pt": "flow",
"to": "false",
"tot": "bool"
}
],
"active": true,
"x": 130,
"y": 60,
"wires": []
},
{
"id": "dccc90be94647fa3",
"type": "serial-port",
"serialport": "COM4",
"serialbaud": "9600",
"databits": "8",
"parity": "none",
"stopbits": "1",
"waitfor": "",
"dtr": "none",
"rts": "none",
"cts": "none",
"dsr": "none",
"newline": "1",
"bin": "bin",
"out": "count",
"addchar": "",
"responsetimeout": "1000"
}
]