(2017-05-31, 12:09 AM)abarrow Wrote: Thanks!
OK, getting somewhere
To strip the "$" off the front of the message and the "*" and chcksum from the end try a function like:-
Code:
var nmeaFull=msg.payload;
var nmeaStripped=nmeaFull.substring(1, nmeaFull.indexOf("*")); //cut of the leading "$" and the bad checksum
msg.payload = nmeaStripped;
return msg;
Then to calculate a new checksum try a function like :-
Seems to work OK checking against http://www.hhhh.org/wiml/proj/nmeaxor.html
Code:
var nmea = msg.payload;
var checksum = 0;
for(var i = 0; i < nmea.length; i++) {
checksum = checksum ^ nmea.charCodeAt(i);
}
checksum = checksum.toString(16); //convert to hex
nmea = '$' + nmea + '*' + checksum; //make the full nmea sentence again
message = 'Full message =' + nmea + '\r\n' + 'Checksum = ' + checksum;
msg.payload = message;
return msg;
And the node-red code :-
Code:
[
{
"id": "a91ed12.b0a903",
"type": "inject",
"z": "d26052b1.ec666",
"name": "",
"topic": "",
"payload": "$WIMDA,30,I,1.02,B,10.1,C,,C,,,,C,158.69,T,,M,3.89 ,N,2,M*37",
"payloadType": "str",
"repeat": "",
"crontab": "",
"once": false,
"x": 150,
"y": 720,
"wires": [
[
"51fc3a3c.0d9534"
]
]
},
{
"id": "51fc3a3c.0d9534",
"type": "function",
"z": "d26052b1.ec666",
"name": "Strip front and back",
"func": "var nmeaFull=msg.payload;\n\nvar nmeaStripped=nmeaFull.substring(1, nmeaFull.indexOf(\"*\")); //cut of the leading \"$\" and the bad checksum\nmsg.payload = nmeaStripped;\nreturn msg;",
"outputs": 1,
"noerr": 0,
"x": 340,
"y": 720,
"wires": [
[
"6a1166f7.40d818",
"f4310649.47dad8"
]
]
},
{
"id": "6a1166f7.40d818",
"type": "debug",
"z": "d26052b1.ec666",
"name": "",
"active": true,
"console": "false",
"complete": "false",
"x": 570,
"y": 600,
"wires": []
},
{
"id": "f4310649.47dad8",
"type": "function",
"z": "d26052b1.ec666",
"name": "calc checksum",
"func": "var nmea = msg.payload;\nvar checksum = 0; \n\nfor(var i = 0; i < nmea.length; i++) { \n checksum = checksum ^ nmea.charCodeAt(i); \n}\nchecksum = checksum.toString(16); //convert to hex\nnmea = '$' + nmea + '*' + checksum; //make the full nmea sentence again\n\nmessage = 'Full message =' + nmea + '\\r\\n' + 'Checksum = ' + checksum;\nmsg.payload = message;\n\nreturn msg;",
"outputs": 1,
"noerr": 0,
"x": 560,
"y": 720,
"wires": [
[
"6a1166f7.40d818"
]
]
}
]
That was fun over morning coffee : Sooooooo,,, how on earth do we get that into kplex!!!
PS - I'm not a programmer but tend use CNP (cut and paste from google ) so - if any of you clever people want to point out beter ways of any tips or tricks please, please take a moment to spread the wisdom, these forums are a great place to learn, thanks
YAY!!! At last, so simple - Doh! All that was missing was a line feed. This works from node red showing up in the nmea diagnostic. Note, I had to manually edit the kplex file to create a UDP input with no address:
Code:
[udp]
name=nodered
direction=in
optional=yes
port=10108
And the node-red function needs a carriage return/ line feed added like :-
msg.payload = msg.payload+"\r\n"
But it works at last!