Hi!
I need help width, how to split/create two 16 bit from payload to be send via modbus leater.
We get payload value trough kplex and tcp in node, leater use switch node to take those rows we need.
in GPRMC function node we split it to messages, json node convert lat to number...
now after that we need to get rid of dot in number 59.3234917 to 593234917 then get it to 2 modbus registers (16bit) 4 byte array which we can feed modbus. Reason to get rid of dot is if we use like float latitude not going to be so exact coz float will round numbers.
19/10/2018, 10:59:09node: e8e2ab9e.6a8748
msg.payload : Object
object
lat: 59.3234917
lon: "18.1840927"
Feed from tcp in looks like
19/10/2018, 10:58:44node: cd9ba9b2.09dd28
msg.payload : string[66]
"$GPRMC,085844.00,A,5932.80963,N,018.60482,E,0.026,,191018,,,A*7A"
in attached flow is function to 2 UInt16 witch contain code I found both to read and create float registers
[{"id":"9b787397.f9a65","type":"tcp in","z":"df4f10d2.533e3","name":"","server":"client","host":"192.168.10.144","port":"10110","datamode":"stream","datatype":"utf8","newline":"\r\n","topic":"","base64":false,"x":157,"y":157,"wires":[["becb5746.5aea38"]]},{"id":"f8588990.06deb8","type":"debug","z":"df4f10d2.533e3","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":1157,"y":156,"wires":},{"id":"becb5746.5aea38","type":"switch","z":"df4f10d2.533e3","name":"","property":"payload","propertyType":"msg","rules":[{"t":"cont","v":"$GPRMC","vt":"str"},{"t":"cont","v":"$GPGGA","vt":"str"},{"t":"cont","v":"$SDDBT","vt":"str"},{"t":"cont","v":"$SDMTW","vt":"str"}],"checkall":"true","repair":false,"outputs":4,"x":395,"y":155,"wires":[["9e22f267.aca55","f8588990.06deb8"],["f8588990.06deb8"],["f8588990.06deb8"],["f8588990.06deb8"]]},{"id":"71b6fba0.c710b4","type":"function","z":"df4f10d2.533e3","name":"to 2 Uint16 ","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 = "to 2 Uint16";\n\nnode.status({fill:"blue",shape:"ring",text:msg.topic + ":" + msg.payload}); \n\nreturn msg;\n//The above code expects a 4 byte array \n//which is 2 Modbus registers\n//from node.js - Convert 32bit unsigned "Real" data type (splitted into two 16 bit signed words) to javascript - Stack Overflow uint16ToFloat32(low, high) {\n var buffer = new ArrayBuffer(4);\n var intView = new Uint16Array(buffer);\n var floatView = new Float32Array(buffer);\n\n intView[0] = low;\n intView[1] = high;\n return floatView[0];\n}\n\nfunction float32ToUint16(value) {\n var buffer = new ArrayBuffer(4);\n var intView = new Uint16Array(buffer);\n var floatView = new Float32Array(buffer);\n\n floatView[0] = value;\n return [intView[0], intView[1]];\n}\n\nconsole.log("Converted ints to", uint16ToFloat32(18584, 18081));\nconsole.log("Converted float to", float32ToUint16(20644.297));\n*/","outputs":1,"noerr":0,"x":936,"y":261,"wires":[["fc550a63.065c68"]]},{"id":"9e22f267.aca55","type":"function","z":"df4f10d2.533e3","name":"GPRMC","func":"//Example of data received:\n//"$GPRMC,071723.00,A,5914.81006,N,01816.64371,E,0.006,,191018,,,A*7D"\n\n\nmessage0 = {};\nmessage1 = {};\nmessage2 = {};\nmessage3 = {};\nmessage4 = {};\nmessage5 = {};\nmessage6 = {};\nmessage7 = {};\nmessage8 = {};\nmessage9 = {};\nmessage10 = {};\nmessage11 = {};\nvar symblat = "";\nvar symblong = "";\n\n//Split the 12 NMEA data into 12 messages\nmsg.string = msg.payload.split(",");\n\n//Find if the data gps fix is valid \nif (msg.string[2] == "A") {\n \n//Find if latitude is south and add minus\nif (msg.string[4] == "S") {\nsymblat = "-";\n} else {\n\n}\n\n//Find if longitude is west and add minus\nif (msg.string[6] == "W") {\nsymblong = "-";\n} else {\n\n}\n\nvar nvals = msg.payload.match(/,(\d+)(\d\d\.\d+),(N|S)/);\nvar evals = msg.payload.match(/,(\d+)(\d\d\.\d+),(E|W)/);\nmsg.payload = {\nlat: symblat + (+nvals[1] + nvals[2]/60).toFixed(7),\nlon: symblong + (+evals[1] + evals[2]/60).toFixed(7)\n\n}}\n\nreturn [msg];","outputs":1,"noerr":0,"x":644,"y":92,"wires":[["f8588990.06deb8","eaa9f38e.e839c"]]},{"id":"eaa9f38e.e839c","type":"json","z":"df4f10d2.533e3","name":"","property":"payload.lat","action":"","pretty":true,"x":923.5,"y":85,"wires":[["fc550a63.065c68","71b6fba0.c710b4"]]},{"id":"fc550a63.065c68","type":"debug","z":"df4f10d2.533e3","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":1273,"y":84,"wires":}]
/IQAPPS