Node-RED – Voorbeeld – INT array omzetten naar 32-bit FLOAT
Op deze pagina vind je een voorbeeld hoe je 2 16-bit INT getallen omzet naar een 32-bit FLOAT getal met noode-red, als voorbeeld is hier een modbus KWH SDM120 meter genomen.
Wat heb je nodig?
- Node-RED Dashboard
- Node-RED Modbus plugin
Stap 1
Maak een modbus node aan en je ontvangt een array als payload terug:
Stap 2
Plaats onderstaand functie achter de modbus node, met de volgende inhoud:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
var rawData = new ArrayBuffer(4); var intView = new Uint16Array(rawData); var fltView = new Float32Array(rawData); intView[0] = msg.payload[1]; //low intView[1] = msg.payload[0]; //high msg.payload = parseFloat(fltView[0].toFixed(1)); msg.topic = "voltage"; node.status({fill:"blue",shape:"ring",text:msg.topic + ":" + msg.payload}); return msg; |
Stap 3
Nu wordt er een 32-bit FLOAT getal weergegeven:
Project in Node-RED clipboard:
1 |
[{"id":"bb524c09.d8df8","type":"function","z":"9c025a4c.21ac28","name":"Voltage","func":"var rawData = new ArrayBuffer(4);\nvar intView = new Uint16Array(rawData);\nvar fltView = new Float32Array(rawData);\n\nintView[0] = msg.payload[1]; //low\nintView[1] = msg.payload[0]; //high\n\nmsg.payload = parseFloat(fltView[0].toFixed(1));\nmsg.topic = \"voltage\";\n\nnode.status({fill:\"blue\",shape:\"ring\",text:msg.topic + \":\" + msg.payload}); \n\nreturn msg;","outputs":1,"noerr":0,"x":620,"y":220,"wires":[["adf04c88.8f191","2cb1c5d5.b7412a"]]},{"id":"adf04c88.8f191","type":"ui_text","z":"9c025a4c.21ac28","group":"c60eb01a.a0b07","order":0,"width":0,"height":0,"name":"","label":"Voltage","format":"{{msg.payload}} V","layout":"row-spread","x":820,"y":220,"wires":[]},{"id":"2cb1c5d5.b7412a","type":"debug","z":"9c025a4c.21ac28","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":830,"y":140,"wires":[]},{"id":"78d09556.e4e30c","type":"modbus-read","z":"9c025a4c.21ac28","name":"","topic":"","showStatusActivities":false,"showErrors":false,"unitid":"1","dataType":"InputRegister","adr":"0","quantity":"2","rate":"3","rateUnit":"s","delayOnStart":false,"startDelayTime":"","server":"ea312303.9cdfb","useIOFile":false,"ioFile":"","useIOForPayload":false,"x":410,"y":220,"wires":[["bb524c09.d8df8"],[]]},{"id":"c60eb01a.a0b07","type":"ui_group","z":"","name":"SDM120 Energy Meter","tab":"5b6dd877.7b5178","order":2,"disp":true,"width":"6"},{"id":"ea312303.9cdfb","type":"modbus-client","z":"","name":"9600","clienttype":"simpleser","bufferCommands":true,"stateLogEnabled":false,"tcpHost":"127.0.0.1","tcpPort":"502","tcpType":"DEFAULT","serialPort":"/dev/ttyUSB1","serialType":"RTU-BUFFERD","serialBaudrate":"9600","serialDatabits":"8","serialStopbits":"1","serialParity":"none","serialConnectionDelay":"100","unit_id":1,"commandDelay":1,"clientTimeout":1000,"reconnectTimeout":2000},{"id":"5b6dd877.7b5178","type":"ui_tab","z":"","name":"Modbus","icon":"memory","order":15}] |