Hi Guy's and GalsI would really appreciate assistance with an issue I am having... I basically need to take the MODBUS data I am receiving via TCP, extract characters located at certain positions in msg.payload, these extracted characters will be in Hexadecimal format. I then need to convert these hexadecimal values to decimal values (apply MODBUS scaling factor where required ex. 0.1) and then display on a dashboard gauge.Below is the data I receive in the debugger via the MODBUS Slave device via a TCP listener:
3: 0xff
- First Register = fffc = 655.32
4: 0xfc
5: 0x7f
- Second Register = 7ffd = 327.65
6: 0xfd
7: 0x7f
- Third Register = 7fff = 327.67
8: 0xff
9: 0xff
- Fourth Register = fffc = 655.32
10: 0xfc
11: 0x0
- Fifth Register = 0000 = 0
12: 0x0
13: 0x1
- Sixth Register = 0115 = 27.7
14: 0x15
15: 0x0
- Seventh Register = 0000 = 0
16: 0x0
As a next step, I need to take the MODBUS responses from the TCP listener and pass the payload (in the second object, from [0 ... 9] AND [10 ...18]) into a function to extract the Hex values and apply the conversion to decimal, then pass on this decimal value as a function output to a dashboard ui node.
Hi SteveSorry for the late response.Please see below the function I have written in JavaScript but Node-Red throws the error: "TypeError: strHexCodes.charAt is not a function"var strHexCodes = msg.payload;var reg0 = strHexCodes.charAt(0).concat(strHexCodes.charAt(1));var reg1 = strHexCodes.charAt(2).concat(strHexCodes.charAt(3));var reg2 = strHexCodes.charAt(4).concat(strHexCodes.charAt(5));var reg3 = strHexCodes.charAt(6).concat(strHexCodes.charAt(7)).concat(strHexCodes.charAt(8)).concat(strHexCodes.charAt(9));var reg4 = strHexCodes.charAt(10).concat(strHexCodes.charAt(11)).concat(strHexCodes.charAt(12)).concat(strHexCodes.charAt(13));var reg5 = strHexCodes.charAt(14).concat(strHexCodes.charAt(15)).concat(strHexCodes.charAt(16)).concat(strHexCodes.charAt(17));var reg6 = strHexCodes.charAt(18).concat(strHexCodes.charAt(19)).concat(strHexCodes.charAt(20)).concat(strHexCodes.charAt(21));var reg7 = strHexCodes.charAt(22).concat(strHexCodes.charAt(23)).concat(strHexCodes.charAt(24)).concat(strHexCodes.charAt(25));var reg8 = strHexCodes.charAt(26).concat(strHexCodes.charAt(27)).concat(strHexCodes.charAt(28)).concat(strHexCodes.charAt(29));var reg9 = strHexCodes.charAt(30).concat(strHexCodes.charAt(31)).concat(strHexCodes.charAt(32)).concat(strHexCodes.charAt(33));var reg10 = strHexCodes.charAt(34).concat(strHexCodes.charAt(35)).concat(strHexCodes.charAt(36)).concat(strHexCodes.charAt(37));return [(parseInt(reg3, 16) / 100).toFixed(2), (parseInt(reg4, 16) / 100).toFixed(2), (parseInt(reg5, 16) / 100).toFixed(2), (parseInt(reg6, 16) / 100).toFixed(2), (parseInt(reg7, 16) / 100).toFixed(2), (parseInt(reg8, 16) / 10).toFixed(1), (parseInt(reg9, 16) / 100).toFixed(2), (parseInt(reg10, 16) / 100).toFixed(2)];Flow:[{"id":"37536f03.bb2ce","type":"mongodb out","z":"76c6836d.e441cc","mongodb":"7d092f74.c00cb","name":"","collection":"cp_values","payonly":false,"upsert":false,"multi":false,"operation":"store","x":605.2857055664062,"y":520.4285278320312,"wires":[]},{"id":"27b7dbaa.59c5a4","type":"ui_gauge","z":"76c6836d.e441cc","name":"Oil Pressure","group":"d4357969.3701f","order":1,"width":"","height":"","gtype":"gage","title":"Oil Pressure","label":"Bar","format":"{{value}}","min":0,"max":"10","colors":["#00b500","#e6e600","#ca3838"],"seg1":"3","seg2":"6","x":1433,"y":103,"wires":[]},{"id":"28fb3864.3854b8","type":"function","z":"76c6836d.e441cc","name":"Generator Oil Pressure","func":"words = msg.payload.replace(/\\n/g, \",\");\nvar res = words.split(\",\");\n\nvar value = parseFloat(res[18]);\n//value = value*8.163;\n\nmsg.payload = (Math.round((value) * 100) / 100);\nreturn msg;","outputs":1,"noerr":0,"x":863,"y":103,"wires":[["27b7dbaa.59c5a4"]]},{"id":"5f8c3b52.f10784","type":"template","z":"76c6836d.e441cc","name":"TCP Payload","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"This is the payload: {{payload}} !","output":"str","x":434.42860412597656,"y":198.7142791748047,"wires":[["28fb3864.3854b8","213e691c.2589d6","eff08d54.1c65d","c734c648.c1fd48","14d9de99.3bf781","5a12c0f3.efefb","d8051fb9.f7c3"]]},{"id":"b4409848.c1a558","type":"ui_gauge","z":"76c6836d.e441cc","name":"Fuel Level","group":"d4357969.3701f","order":2,"width":"","height":"","gtype":"gage","title":"Fuel Level","label":"Litres/Diesel","format":"{{value}}","min":"0","max":"450","colors":["#ff2600","#e6e600","#00f900"],"seg1":"150","seg2":"300","x":1423,"y":163,"wires":[]},{"id":"90aacc50.44bb","type":"ui_gauge","z":"76c6836d.e441cc","name":"Engine Temperature","group":"caf75ba2.a708a8","order":2,"width":"","height":"","gtype":"gage","title":"Engine Temperature","label":"Deg. Celcius","format":"{{value}}","min":0,"max":"110","colors":["#00b500","#e6e600","#ca3838"],"seg1":"30","seg2":"90","x":1461.5714721679688,"y":222.99999809265137,"wires":[]},{"id":"46ae16b7.808928","type":"ui_gauge","z":"76c6836d.e441cc","name":"Battery Voltage","group":"caf75ba2.a708a8","order":1,"width":"","height":"","gtype":"gage","title":"Battery Voltage","label":"Volts","format":"{{value}}","min":0,"max":"15","colors":["#ff2600","#e6e600","#00f900"],"seg1":"5","seg2":"10","x":1443,"y":283,"wires":[]},{"id":"213e691c.2589d6","type":"function","z":"76c6836d.e441cc","name":"Generator Fuel Level","func":"words = msg.payload.replace(/\\n/g, \",\");\nvar res = words.split(\",\");\n\nvar value = parseFloat(res[19]);\nvalue = value+420;\n\nmsg.payload = (Math.round((value) * 100) / 100);\nreturn msg;","outputs":1,"noerr":0,"x":863,"y":163,"wires":[["b4409848.c1a558"]]},{"id":"eff08d54.1c65d","type":"function","z":"76c6836d.e441cc","name":"Generator Engine Temperature","func":"words = msg.payload.replace(/\\n/g, \",\");\nvar res = words.split(\",\");\n\nvar value = parseFloat(res[17]);\nvalue = value*9.3;\n\nmsg.payload = (Math.round((value) * 100) / 100);\nreturn msg;","outputs":1,"noerr":0,"x":893,"y":223,"wires":[["90aacc50.44bb"]]},{"id":"c734c648.c1fd48","type":"function","z":"76c6836d.e441cc","name":"Generator Battery Voltage","func":"words = msg.payload.replace(/\\n/g, \",\");\nvar res = words.split(\",\");\n\nvar value = parseFloat(res[21]);\n//value = value*8.163;\n\nmsg.payload = (Math.round((value) * 100) / 100);\nreturn msg;","outputs":1,"noerr":0,"x":873,"y":283,"wires":[["46ae16b7.808928"]]},{"id":"4847affb.88777","type":"tcp in","z":"76c6836d.e441cc","name":"","server":"server","host":"localhost","port":"51234","datamode":"stream","datatype":"buffer","newline":"","topic":"","base64":false,"x":122.32142639160156,"y":197.07142448425293,"wires":[["ffa657cc.b96098","5f8c3b52.f10784","95f12997.0c6948"]]},{"id":"7b43d1a9.13351","type":"ui_gauge","z":"76c6836d.e441cc","name":"Oil Temperature","group":"d4357969.3701f","order":0,"width":0,"height":0,"gtype":"gage","title":"Oil Temperature","label":"Deg. Celcius","format":"{{value}}","min":0,"max":"200","colors":["#00b500","#e6e600","#ca3838"],"seg1":"75","seg2":"150","x":1443.0713500976562,"y":335.5714454650879,"wires":[]},{"id":"66b76332.0a85fc","type":"ui_gauge","z":"76c6836d.e441cc","name":"Charge Alternator Voltage","group":"caf75ba2.a708a8","order":0,"width":0,"height":0,"gtype":"gage","title":"Charge Alternator Voltage","label":"Volts","format":"{{value}}","min":0,"max":"40","colors":["#00b500","#e6e600","#ca3838"],"seg1":"10","seg2":"30","x":1472.1427841186523,"y":389.99998474121094,"wires":[]},{"id":"8f99c951.3e7d08","type":"ui_gauge","z":"76c6836d.e441cc","name":"Engine Speed","group":"d4357969.3701f","order":0,"width":0,"height":0,"gtype":"gage","title":"Engine Speed","label":"units","format":"{{value}}","min":0,"max":"6000","colors":["#00b500","#e6e600","#ca3838"],"seg1":"2000","seg2":"4000","x":1446.4284057617188,"y":451.4286165237427,"wires":[]},{"id":"14d9de99.3bf781","type":"function","z":"76c6836d.e441cc","name":"Generator Oil Temperature","func":"\nreturn msg;","outputs":1,"noerr":0,"x":887.8571395874023,"y":335.71428394317627,"wires":[["7b43d1a9.13351"]]},{"id":"5a12c0f3.efefb","type":"function","z":"76c6836d.e441cc","name":"Generator Alternator Voltage","func":"\nreturn msg;","outputs":1,"noerr":0,"x":887.8571357727051,"y":390.0000190734863,"wires":[["66b76332.0a85fc"]]},{"id":"d8051fb9.f7c3","type":"function","z":"76c6836d.e441cc","name":"Generator Engine Speed","func":"\nreturn msg;","outputs":1,"noerr":0,"x":877.8571548461914,"y":451.4285945892334,"wires":[["8f99c951.3e7d08"]]},{"id":"ffa657cc.b96098","type":"debug","z":"76c6836d.e441cc","name":"","active":true,"console":"false","complete":"true","x":414.49998474121094,"y":304.14284324645996,"wires":[]},{"id":"91efcea.6a40c3","type":"inject","z":"76c6836d.e441cc","name":"","topic":"","payload":"0,0,0,0,0,0,0,0,0,0,0,0,0,0","payloadType":"str","repeat":"","crontab":"","once":false,"x":156.78571701049805,"y":62.714287757873535,"wires":[["5f8c3b52.f10784"]]},{"id":"95f12997.0c6948","type":"function","z":"76c6836d.e441cc","name":"Generate Decimal Values","func":"var strHexCodes = msg.payload;\nvar reg0 = strHexCodes.charAt(0).concat(strHexCodes.charAt(1)); \nvar reg1 = strHexCodes.charAt(2).concat(strHexCodes.charAt(3)); \nvar reg2 = strHexCodes.charAt(4).concat(strHexCodes.charAt(5)); \nvar reg3 = strHexCodes.charAt(6).concat(strHexCodes.charAt(7)).concat(strHexCodes.charAt(8)).concat(strHexCodes.charAt(9));\nvar reg4 = strHexCodes.charAt(10).concat(strHexCodes.charAt(11)).concat(strHexCodes.charAt(12)).concat(strHexCodes.charAt(13));\nvar reg5 = strHexCodes.charAt(14).concat(strHexCodes.charAt(15)).concat(strHexCodes.charAt(16)).concat(strHexCodes.charAt(17));\nvar reg6 = strHexCodes.charAt(18).concat(strHexCodes.charAt(19)).concat(strHexCodes.charAt(20)).concat(strHexCodes.charAt(21));\nvar reg7 = strHexCodes.charAt(22).concat(strHexCodes.charAt(23)).concat(strHexCodes.charAt(24)).concat(strHexCodes.charAt(25));\nvar reg8 = strHexCodes.charAt(26).concat(strHexCodes.charAt(27)).concat(strHexCodes.charAt(28)).concat(strHexCodes.charAt(29));\nvar reg9 = strHexCodes.charAt(30).concat(strHexCodes.charAt(31)).concat(strHexCodes.charAt(32)).concat(strHexCodes.charAt(33));\nvar reg10 = strHexCodes.charAt(34).concat(strHexCodes.charAt(35)).concat(strHexCodes.charAt(36)).concat(strHexCodes.charAt(37));\n\nreturn [(parseInt(reg3, 16) / 100).toFixed(2), (parseInt(reg4, 16) / 100).toFixed(2), (parseInt(reg5, 16) / 100).toFixed(2), (parseInt(reg6, 16) / 100).toFixed(2), (parseInt(reg7, 16) / 100).toFixed(2), (parseInt(reg8, 16) / 10).toFixed(1), (parseInt(reg9, 16) / 100).toFixed(2), (parseInt(reg10, 16) / 100).toFixed(2)];","outputs":"8","noerr":0,"x":327,"y":419,"wires":[["402ff972.c6d6e8"],[],[],[],[],[],[],[]]},{"id":"402ff972.c6d6e8","type":"debug","z":"76c6836d.e441cc","name":"Oil Pressure","active":true,"console":"false","complete":"true","x":552,"y":387,"wires":[]},{"id":"7d092f74.c00cb","type":"mongodb","z":"76c6836d.e441cc","hostname":"94.130.224.94","port":"27017","db":"local","name":""},{"id":"d4357969.3701f","type":"ui_group","z":"","name":"Critical Operating Parameters","tab":"46d3d70f.a39358","order":2,"disp":true,"width":"5"},{"id":"caf75ba2.a708a8","type":"ui_group","z":"","name":"Critical Operating Parameters 2","tab":"46d3d70f.a39358","order":4,"disp":true,"width":"6"},{"id":"46d3d70f.a39358","type":"ui_tab","z":"","name":"PDS Genset Monitoring Dashboard","icon":"dashboard","order":2}]
--
http://nodered.org
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
To view this discussion on the web, visit https://groups.google.com/d/msgid/node-red/cca3ca5c-f257-4db0-837b-274d07cb661b%40googlegroups.com.