Seek the expression of 16crc check code

Please help:

I use the 16crc check code correctly, but the expression cannot be read by my device. My device needs to express the 4-digit check code separately. Is there any way to achieve it? Thank you
image
I need 03 06 00 0E 00 01 B9 E8 2D C1

Summary

[{"id":"2f11cc20.2f483c","type":"tab","label":"流程 1","disabled":false,"info":""},{"id":"c52abdb8.08d3b8","type":"debug","z":"2f11cc20.2f483c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":590,"y":220,"wires":},{"id":"962cc2bd.4d05d","type":"function","z":"2f11cc20.2f483c","name":"CRC16-modbus","func":"var CRCMaster = {\n StringToCheck: "",\n CleanedString: "",\n CRCTableDNP: ,\n init: function() {\n this.CRCDNPInit();\n },\n CleanString: function(inputType) {\n if (inputType == "ASCII") {\n this.CleanedString = this.StringToCheck;\n } else {\n if (this.StringToCheck.match(/^[0-9A-F \t]+$/gi) !== null) {\n this.CleanedString = this._hexStringToString(this.StringToCheck.toUpperCase().replace(/[\t ]/g, ''));\n } else {\n window.alert("String doesn't seem to be a valid Hex input.");\n return false;\n }\n }\n return true;\n },\n CRCDNPInit: function() {\n var i, j, crc, c;\n for (i = 0; i < 256; i++) {\n crc = 0;\n c = i;\n for (j = 0; j < 8; j++) {\n if ((crc ^ c) & 0x0001) crc = (crc >> 1) ^ 0xA6BC;\n else crc = crc >> 1;\n c = c >> 1;\n }\n this.CRCTableDNP[i] = crc;\n }\n },\n CRC16Modbus: function() {\n var crc = 0xFFFF;\n var str = this.CleanedString;\n for (var pos = 0; pos < str.length; pos++) {\n crc ^= str.charCodeAt(pos);\n for (var i = 8; i !== 0; i--) {\n if ((crc & 0x0001) !== 0) {\n crc >>= 1;\n crc ^= 0xA001;\n } else\n crc >>= 1;\n }\n }\n return crc;\n },\n _stringToBytes: function(str) {\n var ch, st, re = ;\n for (var i = 0; i < str.length; i++) {\n ch = str.charCodeAt(i); // get char\n st = ; // set up "stack"\n do {\n st.push(ch & 0xFF); // push byte to stack\n ch = ch >> 8; // shift value down by 1 byte\n }\n while (ch);\n // add stack contents to result\n // done because chars have "wrong" endianness\n re = re.concat(st.reverse());\n }\n // return an array of bytes\n return re;\n },\n _hexStringToString: function(inputstr) {\n var hex = inputstr.toString(); //force conversion\n var str = '';\n for (var i = 0; i < hex.length; i += 2)\n str += String.fromCharCode(parseInt(hex.substr(i, 2), 16));\n return str;\n },\n Calculate: function(str, inputType) {\n this.StringToCheck = str;\n if (this.CleanString(inputType)) {\n crcinputcrc16modbus=this.CRC16Modbus().toString(16).toUpperCase();\n crcinputcrc16modbus=crcinputcrc16modbus.substr(2) + crcinputcrc16modbus.substr(0, 2); //swap bytes\n }\n }\n};\n\nCRCMaster.init();\n\nvar inputType = "HEX";\nvar crcinputcrc16modbus;\nvar crcinput = msg.payload;\nCRCMaster.Calculate(crcinput, inputType);\n\nmsg.payload = crcinput + crcinputcrc16modbus;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":,"x":380,"y":220,"wires":[["c52abdb8.08d3b8"]]},{"id":"fc0e755b.c6636","type":"inject","z":"2f11cc20.2f483c","name":"03 06 00 0E 00 01 B9 E8 ","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"03 06 00 0E 00 01 B9 E8 ","payloadType":"str","x":130,"y":220,"wires":[["962cc2bd.4d05d"]]}]

Does the number always come in the same format? The last four digits are always the only problem?

Yes.It always displays correctly。

You could use a function node with some JavaScript code to insert the space where needed into the string.

If I understand correct you only need an extra space between 2D and C1.
If you are sure your hex string is always the same length you can use this in a function node.

msg.payload =msg.payload.substring(0, 26) + " " + msg.payload.substring(26, 28)
return msg;

Are you also sure that the device you have needs a hex string and not an array of bytes?
If the above is not working let us know what type of device your are want to use.

1 Like

Thank you. I've just tested your method. It doesn't work. I think my idea should be wrong. My device is TCP to 485 16crc + hex verification 16 bit communication is required

Thank you. It works for my project department. My idea is wrong

My question is, why are you calculating modbus CRC?

Why not simply use the fully working modbus nodes.

Just started learning, but I haven't found the tutorial related to using Mobus to connect 485 :fearful:

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