{"id":200,"date":"2018-10-28T03:49:27","date_gmt":"2018-10-28T03:49:27","guid":{"rendered":"http:\/\/web.recologypower.com:9080\/?p=200"},"modified":"2025-07-22T08:39:34","modified_gmt":"2025-07-22T08:39:34","slug":"%e6%80%bb%e7%ba%bf%e9%80%9a%e8%ae%af%e5%8d%8f%e8%ae%ae%e4%b9%8bcrc16%e6%a0%a1%e9%aa%8c%e7%ae%97%e6%b3%95c%e8%af%ad%e8%a8%80","status":"publish","type":"post","link":"http:\/\/www.recologypower.com:9080\/?p=200","title":{"rendered":"\u603b\u7ebf\u901a\u8baf\u534f\u8bae\u4e4bCRC16\u6821\u9a8c\u7b97\u6cd5(C\u8bed\u8a00)"},"content":{"rendered":"<p>\u968f\u7740\u5de5\u4e1a\u5316\u751f\u4ea7\u7684\u52a0\u5feb\uff0c\u5404\u79cd\u673a\u5668\u534f\u4f5c\u4f5c\u4e1a\u5df2\u7ecf\u6210\u4e3a\u5927\u52bf\u6240\u8d8b\uff0c\u7136\u800c\u901a\u8baf\u603b\u7ebf\u6210\u4e3a\u5efa\u7acb\u5408\u4f5c\u4e4b\u95f4\u7684\u6865\u6881\uff0c\u4e3a\u4fdd\u8bc1\u6570\u636e\u7684\u5b89\u5168\u53ef\u9760\u5230\u8fbe\u5bf9\u65b9\u7684\u4e3b\u63a7\u8bbe\u5907\uff0c\u4e8e\u662f\u5f00\u53d1\u4eba\u5458\u5bf9\u6570\u636e\u8fdb\u884c\u6821\u51c6\uff0c\u591a\u6b21\u91cd\u53d1\u673a\u5236\u3002<\/p>\n<p>CRC\u662f\u5176\u4e2d\u4e00\u79cd\uff0cCRC16\u7528\u7684\u6700\u591a\uff0c\u5e76\u4e14\u5206\u4e3a\u591a\u79cd\uff1aCRC-16(Modbus)\u3001CRC-16(Sick)\u3001CRC-16(XModem)\u3001CRC-16(0xffff)\u3001CRC-16(1D0F)\u3001CRC-16(Kermit)\u3002<\/p>\n<p>C\u8bed\u8a00CRC-16(Modbus)\uff1a<\/p>\n<pre class=\"brush:c\">unsigned short ModBusCRC (unsigned char *ptr,unsigned char size)\n{\n\tunsigned short a,b,tmp,CRC16,V;\n\tCRC16=0xffff; \/\/CRC \u5bc4\u5b58\u5668\u521d\u59cb\u503c\n\tfor (a=0;a&lt;size;a++) \/\/N \u4e2a\u5b57\u8282\n\t{\n\t\tCRC16=*(ptr+a)^CRC16;\n\t\tfor (b=0;b&lt;8;b++) \/\/8 \u4f4d\u6570\u636e\n\t\t{\n\t\t\ttmp=CRC16 &amp; 0x0001;\n\t\t\tCRC16 =CRC16 &gt;&gt;1; \/\/ \u53f3\u79fb\u4e00\u4f4d\n\t\t\tif (tmp)\n\t\t\tCRC16=CRC16 ^ 0xa001; \/\/ \u5f02\u6216\u591a\u9879\u5f0f\n\t\t}\n\t}\n\tV = ((CRC16 &amp; 0x00FF) &lt;&lt; 8) | ((CRC16 &amp; 0xFF00) &gt;&gt; 8) ;\/\/ \u9ad8\u4f4e\u5b57\u8282\u8f6c\u6362\n\treturn V;\n}<\/pre>\n<p>C\u8bed\u8a00CRC-16(XModem)\uff1a<\/p>\n<pre class=\"brush:c\">unsigned int period_crc16(unsigned char *dat, unsigned int count)\n{\n\tconst unsigned int ccitt_table[] = {\n\t0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7,\n\t0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF,\n\t0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6,\n\t0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE,\n\t0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485,\n\t0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D,\n\t0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4,\n\t0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC,\n\t0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823,\n\t0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B,\n\t0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12,\n\t0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A,\n\t0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41,\n\t0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 0x9D49,\n\t0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70,\n\t0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78,\n\t0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 0xE16F,\n\t0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067,\n\t0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E,\n\t0x02B1, 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256,\n\t0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D,\n\t0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,\n\t0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 0xD73C,\n\t0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634,\n\t0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB,\n\t0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3,\n\t0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A,\n\t0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92,\n\t0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9,\n\t0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1,\n\t0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8,\n\t0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0\n\t};\n\tunsigned int crc = 0;\n\tunsigned int crctemp;\n\tunsigned int i = 0;\n\twhile (count--)\n\t{\n\t\tcrctemp = crc &gt;&gt; 8;\n\t\tcrc = (crc &lt;&lt; 8) &amp; 0xFFFF;\n\t\tcrc = (ccitt_table[crctemp ^ dat[i]] ^ crc) &amp; 0xFFFF;\n\t\ti += 1;\n\t}\n\treturn crc;\n}<\/pre>\n<p>\u6d4b\u8bd5VS2010 demo\uff1a<a href=\"http:\/\/web.recologypower.com:9080\/?attachment_id=205\" rel=\"attachment wp-att-205\">crc16_test<\/a><\/p>\n<p>MODBUS-CRC16\u7684\u539f\u7406\u53caC\u8bed\u8a00\u7684\u5b9e\u73b0\uff1a<a href=\"http:\/\/web.recologypower.com:9080\/?attachment_id=206\" rel=\"attachment wp-att-206\">MODBUS-CRC16\u7684\u539f\u7406\u53caC\u8bed\u8a00\u7684\u5b9e\u73b0<\/a><\/p>\n<p>\u6d4b\u8bd5\u5de5\u5177\uff1a<a href=\"http:\/\/web.recologypower.com:9080\/?attachment_id=207\" rel=\"attachment wp-att-207\">CRC_Calc+v0.1.exe<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"slide-text-bg2\">\n<h3>\u968f\u7740\u5de5\u4e1a\u5316\u751f\u4ea7\u7684\u52a0\u5feb\uff0c\u5404\u79cd\u673a\u5668\u534f\u4f5c\u4f5c\u4e1a\u5df2<\/h3>\n<\/div>\n<div class=\"flex-btn-div\"><a href=\"http:\/\/www.recologypower.com:9080\/?p=200\" class=\"btn1 flex-btn\">\u9605\u8bfb\u66f4\u591a<\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[38],"_links":{"self":[{"href":"http:\/\/www.recologypower.com:9080\/index.php?rest_route=\/wp\/v2\/posts\/200"}],"collection":[{"href":"http:\/\/www.recologypower.com:9080\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.recologypower.com:9080\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.recologypower.com:9080\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.recologypower.com:9080\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=200"}],"version-history":[{"count":6,"href":"http:\/\/www.recologypower.com:9080\/index.php?rest_route=\/wp\/v2\/posts\/200\/revisions"}],"predecessor-version":[{"id":1700,"href":"http:\/\/www.recologypower.com:9080\/index.php?rest_route=\/wp\/v2\/posts\/200\/revisions\/1700"}],"wp:attachment":[{"href":"http:\/\/www.recologypower.com:9080\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=200"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.recologypower.com:9080\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=200"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.recologypower.com:9080\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=200"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}