Function shut generate Xor Checksum over 4 variables

hello dear people,
I have to create a checksum over 4 variables via XOR, but I cannot find any usable information here that will help me further
the checksum itself is only one byte long

Does anyone have valuable help for me and to be able to implement my problem, please
Thanks in advance
olby2

from the data sheet

Length Description
1 STX (Start of text)
2 Recipient address (serial number) 2 (high byte) - 3 (low byte)
2 Sender address (serial number) 4 (high byte) - 5 (low byte)
2 Parameter numbers (with control bits) (Bit 11 = 0) 6 (high byte) - 7 (low byte)
4 Parameter value 8 (HH byte) - 11 (LL byte)
1 Checksum (XOR of bytes 2-11) 12
1 ETX (End of text) 13

my Function code

//var STX=02;
var recipient=msg.payload.Recipient;
var sender=msg.payload.Sender;
var Par_Number=msg.payload.Par_Number;
var write=msg.payload.write;   // Bit 15 1=Write parameter
var read=msg.payload.read;    //  Bit 14  1=Read parameter
var error=msg.payload.error; // Bit 12 1= error occured
var File=msg.payload.File;   //  Bit 11 0=No acton
var VDEW=msg.payload.VDEW;   //  Bit 10 allways 0

var Par_value=msg.payload.Par_value;
var checksum=0
// var ETX=03;

Par_Number |=  write << 15; 
Par_Number |=  read << 14; 
Par_Number |=  error << 12; 
Par_Number |=  File << 11; 
Par_Number |=  VDEW << 10; 

// Checksum from recipient ; sender ; Par_Number ; Par_value 


var Checksum = 01;
// Checksum end  
msg.payload.Par_Number = Par_Number;
msg.payload.Checksum = Checksum;

return msg;

This post should put you on the right track...

Hello Steve,
Preformatted textI've tried something with your information and examples, but I'm not getting it right yet. either I don't mind that the value of the checksum may only be an 8-bit value or I only calculate some of my variables with XOR

The inject node uses a originally transmitted packet and encodes it.
With the Inject Node 148 I try to create the packet myself, but the checksum differ between the original and my "copy"

thanks for your help in advanced

[
    {
        "id": "3f7d785b05f96f24",
        "type": "inject",
        "z": "e016160.58e2068",
        "name": "",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "0200010000409400000000d503",
        "payloadType": "str",
        "x": 250,
        "y": 280,
        "wires": [
            [
                "3a27a89280829ee6"
            ]
        ]
    },
    {
        "id": "3a27a89280829ee6",
        "type": "buffer-parser",
        "z": "e016160.58e2068",
        "name": "",
        "data": "payload",
        "dataType": "msg",
        "specification": "spec",
        "specificationType": "ui",
        "items": [
            {
                "type": "byte",
                "name": "STX",
                "offset": 0,
                "length": 1,
                "offsetbit": 0,
                "scale": "1",
                "mask": ""
            },
            {
                "type": "int16be",
                "name": "Recipient",
                "offset": 1,
                "length": 1,
                "offsetbit": 0,
                "scale": "1",
                "mask": ""
            },
            {
                "type": "int16be",
                "name": "Sender",
                "offset": 3,
                "length": 1,
                "offsetbit": 0,
                "scale": "1",
                "mask": ""
            },
            {
                "type": "int16be",
                "name": "Parameter Num",
                "offset": 5,
                "length": 1,
                "offsetbit": 0,
                "scale": "1",
                "mask": "0b000000111111111"
            },
            {
                "type": "int16be",
                "name": "write",
                "offset": 5,
                "length": 1,
                "offsetbit": 0,
                "scale": ">>15",
                "mask": "0b1111100000000000"
            },
            {
                "type": "int16be",
                "name": "read",
                "offset": 5,
                "length": 1,
                "offsetbit": 0,
                "scale": ">>14",
                "mask": "0b0111100000000000"
            },
            {
                "type": "int32be",
                "name": "Value",
                "offset": 7,
                "length": 1,
                "offsetbit": 0,
                "scale": "1",
                "mask": ""
            },
            {
                "type": "byte",
                "name": "Checksum",
                "offset": 11,
                "length": 1,
                "offsetbit": 0,
                "scale": "1",
                "mask": ""
            },
            {
                "type": "byte",
                "name": "ETX",
                "offset": 12,
                "length": 1,
                "offsetbit": 0,
                "scale": "1",
                "mask": ""
            }
        ],
        "swap1": "",
        "swap2": "",
        "swap3": "",
        "swap1Type": "swap",
        "swap2Type": "swap",
        "swap3Type": "swap",
        "msgProperty": "payload",
        "msgPropertyType": "str",
        "resultType": "keyvalue",
        "resultTypeType": "output",
        "multipleResult": false,
        "fanOutMultipleResult": false,
        "setTopic": true,
        "outputs": 1,
        "x": 680,
        "y": 320,
        "wires": [
            [
                "7dd8b6d07a7786ef",
                "f77fd0f128e83955"
            ]
        ]
    },
    {
        "id": "7dd8b6d07a7786ef",
        "type": "debug",
        "z": "e016160.58e2068",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": true,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 970,
        "y": 320,
        "wires": []
    },
    {
        "id": "f77fd0f128e83955",
        "type": "function",
        "z": "e016160.58e2068",
        "name": "",
        "func": "var STX=02;\nvar recipient=msg.payload.Recipient;\nvar sender=msg.payload.Sender;\nvar Par_number=msg.payload.Par_Number;\nvar write=msg.payload.write;   // Bit 15 1=Write parameter\nvar read=msg.payload.read;    //  Bit 14  1=Read parameter\nvar error=msg.payload.error; // Bit 12 1= error occured\nvar File=msg.payload.File;   //  Bit 11 0=No acton\nvar VDEW=msg.payload.VDEW;   //  Bit 10 allways 0\n\nvar Par_value=msg.payload.Par_value;\nvar checksum=0\nvar ETX=03;\n\nPar_number = Par_number & (write << 15); \nPar_number= Par_number & (read << 14); \n// var Par_number = Par_number & (error << 12);  ich woll ja nur lesen und nicht schreiben\nPar_number = Par_number & (File << 11); \nPar_number = Par_number & (VDEW << 10); \nPar_number = Par_number & Par_number;\n\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 880,
        "y": 440,
        "wires": [
            []
        ]
    },
    {
        "id": "53064f1448fe3feb",
        "type": "inject",
        "z": "e016160.58e2068",
        "name": "read 148",
        "props": [
            {
                "p": "payload.Recipient",
                "v": "1",
                "vt": "str"
            },
            {
                "p": "payload.Sender",
                "v": "0",
                "vt": "str"
            },
            {
                "p": "payload.Par_Number",
                "v": "148",
                "vt": "str"
            },
            {
                "p": "payload.write",
                "v": "0",
                "vt": "str"
            },
            {
                "p": "payload.read",
                "v": "1",
                "vt": "str"
            },
            {
                "p": "payload.error",
                "v": "0",
                "vt": "str"
            },
            {
                "p": "payload.File",
                "v": "0",
                "vt": "str"
            },
            {
                "p": "payload.VDEW",
                "v": "0",
                "vt": "str"
            },
            {
                "p": "payload.Par_value",
                "v": "0",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "x": 250,
        "y": 460,
        "wires": [
            [
                "8002e5f9b57d0ba9"
            ]
        ]
    },
    {
        "id": "8002e5f9b57d0ba9",
        "type": "function",
        "z": "e016160.58e2068",
        "name": "",
        "func": "//var STX=02;\nvar recipient=msg.payload.Recipient;\nvar sender=msg.payload.Sender;\nvar Par_Number=msg.payload.Par_Number;\nvar write=msg.payload.write;   // Bit 15 1=Write parameter\nvar read=msg.payload.read;    //  Bit 14  1=Read parameter\nvar error=msg.payload.error; // Bit 12 1= error occured\nvar File=msg.payload.File;   //  Bit 11 0=No acton\nvar VDEW=msg.payload.VDEW;   //  Bit 10 allways 0\n\nvar Par_value=msg.payload.Par_value;\nvar checksum=0\n// var ETX=03;\n\nPar_Number |=  write << 15; \nPar_Number |=  read << 14; \nPar_Number |=  error << 12; \nPar_Number |=  File << 11; \nPar_Number |=  VDEW << 10; \n\n// Checksum from recipient ; sender ; Par_Number ; Par_value \n\nvar array=[];\n\n\nvar buf = Buffer.alloc(4);\narray[0] = buf.writeInt16BE(msg.payload.Recipient);\narray[1] = buf.writeInt16BE(msg.payload.Sender);\narray[2] = buf.writeUInt16BE(Par_Number);\narray[3] = buf.writeInt32BE(Par_value);\n\nlet Checksum = 0;//the checksum\nfor (let i = array.length - 1 ; i >= 1; i--) {\n    \n    Checksum ^= Number(array[i]); //XOR\n \n}\n\n\n\n\n// var Checksum = 01;\n// Checksum end  \nmsg.payload.Par_Number = Par_Number;\nmsg.payload.Checksum = Checksum;\nmsg.payload.Par_value = Par_value;\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 400,
        "y": 460,
        "wires": [
            [
                "7640809732653cea"
            ]
        ]
    },
    {
        "id": "7640809732653cea",
        "type": "buffer-maker",
        "z": "e016160.58e2068",
        "name": "codierung Solutronic",
        "specification": "spec",
        "specificationType": "ui",
        "items": [
            {
                "name": "STX",
                "type": "byte",
                "length": 1,
                "dataType": "str",
                "data": "02"
            },
            {
                "name": "Recipient",
                "type": "int16be",
                "length": 1,
                "dataType": "msg",
                "data": "payload.Recipient"
            },
            {
                "name": "Sender",
                "type": "int16be",
                "length": 1,
                "dataType": "msg",
                "data": "payload.Sender"
            },
            {
                "name": "Par_Number",
                "type": "uint16be",
                "length": 1,
                "dataType": "msg",
                "data": "payload.Par_Number"
            },
            {
                "name": "Par_value",
                "type": "int32be",
                "length": 1,
                "dataType": "msg",
                "data": "payload.Par_value"
            },
            {
                "name": "Checksum",
                "type": "byte",
                "length": 1,
                "dataType": "msg",
                "data": "payload.Checksum"
            },
            {
                "name": "STX",
                "type": "byte",
                "length": 1,
                "dataType": "str",
                "data": "03"
            }
        ],
        "swap1": "",
        "swap2": "",
        "swap3": "",
        "swap1Type": "swap",
        "swap2Type": "swap",
        "swap3Type": "swap",
        "msgProperty": "payload",
        "msgPropertyType": "str",
        "x": 510,
        "y": 600,
        "wires": [
            [
                "3a27a89280829ee6",
                "839e3748f740e685"
            ]
        ]
    },
    {
        "id": "839e3748f740e685",
        "type": "debug",
        "z": "e016160.58e2068",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": true,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 870,
        "y": 560,
        "wires": []
    }
]

Your parameter number in the lower inject was incorrect (it should contain 0x4094 - a combination of the read/write flag (hi) and the parameter number (lo).)

[{"id":"3f7d785b05f96f24","type":"inject","z":"6e1e28ac4d35a2dd","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0200010000409400000000d503","payloadType":"str","x":390,"y":1380,"wires":[["3a27a89280829ee6"]]},{"id":"3a27a89280829ee6","type":"buffer-parser","z":"6e1e28ac4d35a2dd","name":"","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"byte","name":"STX","offset":0,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"int16be","name":"Recipient","offset":1,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"int16be","name":"Sender","offset":3,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"int16be","name":"Parameter Num","offset":5,"length":1,"offsetbit":0,"scale":"1","mask":"0b000000111111111"},{"type":"int16be","name":"write","offset":5,"length":1,"offsetbit":0,"scale":">>15","mask":"0b1111100000000000"},{"type":"int16be","name":"read","offset":5,"length":1,"offsetbit":0,"scale":">>14","mask":"0b0111100000000000"},{"type":"int32be","name":"Value","offset":7,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"byte","name":"Checksum","offset":11,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"byte","name":"ETX","offset":12,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"buffer","name":"buffer","offset":0,"length":-1,"offsetbit":0,"scale":"1","mask":""}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"keyvalue","resultTypeType":"output","multipleResult":false,"fanOutMultipleResult":false,"setTopic":true,"outputs":1,"x":810,"y":1380,"wires":[["7dd8b6d07a7786ef","e07f37c61e55b683","1a7d90b56e5e4b20"]]},{"id":"7dd8b6d07a7786ef","type":"debug","z":"6e1e28ac4d35a2dd","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"true","targetType":"full","statusVal":"payload","statusType":"msg","x":970,"y":1400,"wires":[]},{"id":"53064f1448fe3feb","type":"inject","z":"6e1e28ac4d35a2dd","name":"read 148","props":[{"p":"payload.Recipient","v":"1","vt":"num"},{"p":"payload.Sender","v":"0","vt":"num"},{"p":"payload.Par_Number","v":"0x4094","vt":"num"},{"p":"payload.write","v":"0","vt":"num"},{"p":"payload.read","v":"1","vt":"num"},{"p":"payload.error","v":"0","vt":"num"},{"p":"payload.File","v":"0","vt":"num"},{"p":"payload.VDEW","v":"0","vt":"num"},{"p":"payload.Par_value","v":"0","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":390,"y":1520,"wires":[["7640809732653cea"]]},{"id":"7640809732653cea","type":"buffer-maker","z":"6e1e28ac4d35a2dd","name":"codierung Solutronic","specification":"spec","specificationType":"ui","items":[{"name":"Recipient","type":"uint16be","length":1,"dataType":"msg","data":"payload.Recipient"},{"name":"Sender","type":"uint16be","length":1,"dataType":"msg","data":"payload.Sender"},{"name":"Par_Number","type":"uint16be","length":1,"dataType":"msg","data":"payload.Par_Number"},{"name":"Par_value","type":"int32be","length":1,"dataType":"msg","data":"payload.Par_value"}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","x":580,"y":1520,"wires":[["06d5d209bb439d06"]]},{"id":"839e3748f740e685","type":"debug","z":"6e1e28ac4d35a2dd","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"originalPayload","targetType":"msg","statusVal":"originalPayload","statusType":"auto","x":860,"y":1640,"wires":[]},{"id":"06d5d209bb439d06","type":"function","z":"6e1e28ac4d35a2dd","name":"BCC Calc","func":"msg.last = msg.payload[msg.payload.length-1]; //capture last element\nvar data = msg.payload.slice(0,-1); //lose last element\n\nlet cs = 0;//the checksum\nfor (let i = data.length - 1; i >= 0; i--) {\n    cs ^= data[i]; //XOR\n}\n\nmsg.checksum = cs;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":800,"y":1520,"wires":[["58892743a27d971e"]]},{"id":"58892743a27d971e","type":"function","z":"6e1e28ac4d35a2dd","name":"add STX Data BCC ETX","func":"var stx = Buffer.from([2]);\nvar data = msg.payload;\nvar bcc = Buffer.from([msg.checksum]);\nvar etx = Buffer.from([3]);\nvar arr = [stx, data, bcc, etx];\n\nvar buf = Buffer.concat(arr);\n\nmsg.payload = buf;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":570,"y":1580,"wires":[["839e3748f740e685","f13e29c3d0de288e","3a27a89280829ee6","da54396c3bdab372"]]},{"id":"e07f37c61e55b683","type":"debug","z":"6e1e28ac4d35a2dd","name":"buffer","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"true","targetType":"full","statusVal":"buffer","statusType":"msg","x":970,"y":1340,"wires":[]},{"id":"f13e29c3d0de288e","type":"debug","z":"6e1e28ac4d35a2dd","name":"buffer","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"true","targetType":"full","statusVal":"payload","statusType":"msg","x":810,"y":1580,"wires":[]},{"id":"1a7d90b56e5e4b20","type":"debug","z":"6e1e28ac4d35a2dd","name":"Checksum","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload.Checksum","statusType":"msg","x":990,"y":1460,"wires":[]},{"id":"da54396c3bdab372","type":"debug","z":"6e1e28ac4d35a2dd","name":"Checksum","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload[11]","statusType":"msg","x":830,"y":1700,"wires":[]}]

I don't know if I am in the same situation or if I am search for the wrong terms.

BUT.. I have stumbled across and answer that was SO simple it hurt my brain.

https://www.techiedelight.com/find-xor-two-numbers-without-using-xor-operator/

I am / was trying to find a way to create an XOR checksum for the Iiyama large screens, following instructions in there Serial command PDF

Remote Control over IP

Based on ProLite LHxx42UHS series controls

Command code PDF download https://cdn.iiyama.com/f/516a3e577383945ece3becbc10696cf1_lhxx42uhs-rs232-lan-commands-0609.pdf

TCP traffic directed to Port 5000

Byte 1 Header Header = 0xA6
Byte 2 Monitor ID, Range : 1 ~ 255, Signal mode: Display Address range from 1 to 255, Broadcast mode: Display Address is 0 which indicates no ACK or Report is expected.
Byte 3 Category Category = 0x00 (fixed)
Byte 4 Code0 (Page) Page = 0x00 (fixed)
Byte 5 Code1 (Function)
Byte 6 Length Length has to be calculated in the fallowing way: Length = N + 3
Byte 7 Data Control Data Control = 0x01 (fixed)
Byte 8 ~ Byte 44 Data[0] ~ Data[N] This field can be also empty. If not empty then the range of Data Size, N = 0 to 36.

 Last Byte Checksum, Range = 0 to 255 (0xFF).
 Algorithm: The EXCLUSIVE-OR (XOR) of all bytes in the message except the checksum itself.
 Checksum = [Header] XOR [Monitor ID] XOR … DATA[0] … XOR DATA[N]

Now I'm not a programmer by any stretch of the imagination, so I haven't worked out how to create a dynamic calculation for a changing packet length, but as I only have a few command to create, I'm happy to have a number of Function nodes to handle differnent length commands.

What I discovered having read the forum posted listed at the top of this post, is that the Strange symbol above button 6 means XOR a number ^

The function node here is my very first attempt WHICH SEEMS to be working.
It contains this important lines

// Checksum of XOR of everything preceeding
// Create XOR checksum)
var checksum = byte1 ^ byte2 ^ byte3 ^ byte4 ^ byte5 ^ byte6 ^ byte7 ^ byte8 ^ byte9;  
msg.checksum = checksum.toString(16);

I really hope this help you and anyone else that stumbles across this when they search for XOR checksum*

[
    {
        "id": "60cc85143b45d8e2",
        "type": "inject",
        "z": "4d010104af0042c7",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 250,
        "y": 740,
        "wires": [
            [
                "411466f8afe3fdfb"
            ]
        ]
    },
    {
        "id": "411466f8afe3fdfb",
        "type": "function",
        "z": "4d010104af0042c7",
        "name": "Power ON (9 Byte)",
        "func": "\n\n var byte1      = 0xa6;         // Header = Always 0xA6\n var byte2      = 0x01;         // Monitor ID or 00 for broadcast\n var byte3      = 0x00;         //  Categorgy\n var byte4      = 0x00;         // code 0\n var byte5      = 0x00;         // code 1\n var byte6      = 0x04;         // length of remanining bytes\n var byte7      = 0x01;         // Data control\n var byte8      = 0x18;         // 1st command, 0x19 = Power state?, 0x18 = Set power\n var byte9      = 0x01;         // Set to 0x01 = OFF, 0x02= ON\n //     var byte10     = 0xbc;         // Checksum of XOR of everything preceeding\n\n \n        // Create XOR checksum)   \nvar checksum = byte1 ^ byte2 ^ byte3 ^ byte4 ^ byte5 ^ byte6 ^ byte7 ^ byte8 ^ byte9; \n\nmsg.checksum = checksum.toString(16);    \n\nmsg.payload= Buffer.from([byte1,byte2,byte3,byte4,byte5,byte6,byte7,byte8,byte9,checksum]);   \n \n \nreturn msg;\n\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 590,
        "y": 720,
        "wires": [
            [
                "477b7645ee2ec72e"
            ]
        ]
    },
    {
        "id": "477b7645ee2ec72e",
        "type": "debug",
        "z": "4d010104af0042c7",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": true,
        "complete": "true",
        "targetType": "full",
        "statusVal": "payload",
        "statusType": "msg",
        "x": 910,
        "y": 720,
        "wires": []
    }
]

Hallo Steve,
Mandy thanks for your work, That helps me a lot
I Hand some more obstacles but now the inverter Talks to me and i unterstand the answer.
Mfg
Olby2

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.