# Seek the expression of 16crc check code

**URL:** <https://discourse.nodered.org/t/seek-the-expression-of-16crc-check-code/49340>\
**Category:** General\
**Created:** [4 August 2021 17:52 UTC](https://discourse.nodered.org/t/seek-the-expression-of-16crc-check-code/49340 "2021-08-04T17:52:17Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![SunvidWong](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sunvidwong/32/46038_2.png) [@SunvidWong](https://discourse.nodered.org/u/SunvidWong)\
**Post date:** [4 August 2021 17:52 UTC](https://discourse.nodered.org/t/seek-the-expression-of-16crc-check-code/49340/1 "2021-08-04T17:52:18Z")

</div>

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/b/6bc4a4998c3b761c30d8ebe64de50469e7e36124.png)  
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"]]}]

---

<div class="post-metadata">

**Author:** ![gerry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gerry/32/21604_2.png) [@gerry](https://discourse.nodered.org/u/gerry)\
**Post date:** [4 August 2021 18:25 UTC](https://discourse.nodered.org/t/seek-the-expression-of-16crc-check-code/49340/2 "2021-08-04T18:25:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![SunvidWong](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sunvidwong/32/46038_2.png) [@SunvidWong](https://discourse.nodered.org/u/SunvidWong)\
**Post date:** [4 August 2021 18:28 UTC](https://discourse.nodered.org/t/seek-the-expression-of-16crc-check-code/49340/3 "2021-08-04T18:28:05Z")

</div>

Yes.It always displays correctly。

---

<div class="post-metadata">

**Author:** ![Gunner](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gunner/32/43582_2.png) [@Gunner](https://discourse.nodered.org/u/Gunner)\
**Post date:** [4 August 2021 18:43 UTC](https://discourse.nodered.org/t/seek-the-expression-of-16crc-check-code/49340/4 "2021-08-04T18:43:03Z")

</div>

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

> **[JavaScript validation with regular expression: Insert a string within a...](https://www.w3resource.com/javascript-exercises/javascript-string-exercise-14.php)**
>
> JavaScript exercises, practice and solution: Write a JavaScript function to insert a string within a string at a particular position (default is 1).

---

<div class="post-metadata">

**Author:** ![edje11](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/edje11/32/572_2.png) [@edje11](https://discourse.nodered.org/u/edje11)\
**Post date:** [4 August 2021 18:58 UTC](https://discourse.nodered.org/t/seek-the-expression-of-16crc-check-code/49340/5 "2021-08-04T18:58:44Z")

</div>

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.

```auto
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.

---

<div class="post-metadata">

**Author:** ![SunvidWong](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sunvidwong/32/46038_2.png) [@SunvidWong](https://discourse.nodered.org/u/SunvidWong)\
**Post date:** [4 August 2021 19:32 UTC](https://discourse.nodered.org/t/seek-the-expression-of-16crc-check-code/49340/6 "2021-08-04T19:32:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![SunvidWong](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sunvidwong/32/46038_2.png) [@SunvidWong](https://discourse.nodered.org/u/SunvidWong)\
**Post date:** [4 August 2021 19:38 UTC](https://discourse.nodered.org/t/seek-the-expression-of-16crc-check-code/49340/7 "2021-08-04T19:38:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![SunvidWong](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sunvidwong/32/46038_2.png) [@SunvidWong](https://discourse.nodered.org/u/SunvidWong)\
**Post date:** [4 August 2021 20:01 UTC](https://discourse.nodered.org/t/seek-the-expression-of-16crc-check-code/49340/8 "2021-08-04T20:01:23Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/6/762ac12235a2762a151211491d476e789f1a8df1.jpeg)

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [4 August 2021 20:24 UTC](https://discourse.nodered.org/t/seek-the-expression-of-16crc-check-code/49340/10 "2021-08-04T20:24:05Z")

</div>

My question is, why are you calculating modbus CRC?

Why not simply use the fully working modbus nodes.

---

<div class="post-metadata">

**Author:** ![SunvidWong](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sunvidwong/32/46038_2.png) [@SunvidWong](https://discourse.nodered.org/u/SunvidWong)\
**Post date:** [4 August 2021 20:55 UTC](https://discourse.nodered.org/t/seek-the-expression-of-16crc-check-code/49340/11 "2021-08-04T20:55:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [3 October 2021 20:55 UTC](https://discourse.nodered.org/t/seek-the-expression-of-16crc-check-code/49340/12 "2021-10-03T20:55:09Z")

</div>

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