Hi
I need to calculate a checksum i a buffer payload
the way to calculate the checksum is as follow
-
In Windows, Start / Programs / Accessories / Calculator
-
In Calculator, under View, Select Scientific
-
In Calculator, select Hex
-
Add the first 7 Hex bytes, then press = (ie: 1C + 01 + 20 + 00 + 00 + 00 + FF total = 13C)
-
Press the [Not] button (in our example, the total = FFFFFEC3)
-
Add 1 to the total (in our example, the total = FFFFFEC4)
-
The checksum is the two least significant characters (in our example, checksum = C4)
I have written some code in a function, but i am stuck on the 5) NoT and 7)
How do I convert the Not ~ to a buffer i can insert in my execisting buffer, how do i only take the last tow figures on the final result and but it on D7
D0 = [0x1C]
D1 = msg.payload.A
D2 = msg.payload.B
D3 = msg.payload.C
D4 = [0x00]
D5 = [0x00]
D6 = [0xff]
D7 = [0] //Checksum
//add = D0 + D1 + D2 + D3 + D4 + D5 + D6
add = D1 + D2 + D3
not = ~ add
plus_one = not + 1
// Q1: Convert plus_one to a buffer.
//Q2: Take out the last two characters an put it in to D7
msg.payload = new Buffer([D0,D1,D2,D3,D4,D5,D6,D7]);
return msg;
[{"id":"f131a05.069bde","type":"tab","label":"Flow 3","disabled":false,"info":""},{"id":"16421c01.36c1d4","type":"function","z":"f131a05.069bde","name":"Math add","func":"D0 = [0x1C]\nD1 = msg.payload.A\nD2 = msg.payload.B\nD3 = msg.payload.C\nD4 = [0x00]\nD5 = [0x00]\nD6 = [0xff]\nD7 = [0] //Checksum\n\n//add = D0 + D1 + D2 + D3 + D4 + D5 + D6 \nadd = D1 + D2 + D3\nnot = ~ add\nplus_one = not + 1\n\n\n\nmsg.payload = new Buffer([D0,D1,D2,D3,D4,D5,D6,D7]);\n\nreturn msg;","outputs":1,"noerr":0,"x":470,"y":200,"wires":[["348f24e7.aed91c"]]},{"id":"348f24e7.aed91c","type":"debug","z":"f131a05.069bde","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":510,"y":280,"wires":[]},{"id":"f5c82250.e845f","type":"inject","z":"f131a05.069bde","name":"","topic":"","payload":"{\"A\":1,\"B\":32,\"C\":0}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":290,"y":200,"wires":[["16421c01.36c1d4","348f24e7.aed91c"]]}]