"# RTU\u5e27 & ASCII\u5e27\u7684\u5dee\u9519\u6821\u9a8c-Modbus\u901a\u8baf\u534f\u8bae\u8981\u70b9\u7b2c5\u90e8\u5206\n\n> 2023-10-02 | 2023-10-02\n> https:\/\/www.modbus.cn\/en\/25215.html\n\n**Modbus \u534f\u8bae\u6807\u51c6**\n\n---\n\n\u524d\u9762\u5df2\u7ecf\u5206\u6790\u8fc7RTU\u5e27\u548cASCII\u5e27\u7684\u62a5\u6587\u533a\u522b\uff0c\u7ec6\u5fc3\u7684\u670b\u53cb\u5e94\u8be5\u4f1a\u53d1\u73b0\u5728\u4e24\u79cd\u4e0d\u540c\u7684\u62a5\u6587\u4f20\u8f93\u6a21\u5f0f\u4e0b\u90fd\u6709\u4e00\u4e2a\u5171\u540c\u7684\u7ec4\u6210\u90e8\u5206\u2014\u2014\u5dee\u9519\u6821\u9a8c\u3002\u8fd9\u4e2a\u5dee\u9519\u6821\u9a8c\u5728RTU\u6a21\u5f0f\u548cASCII\u6a21\u5f0f\u4e0b\u4e5f\u4e0d\u662f\u4e0d\u76f8\u540c\u7684\u3002\n\n\u770b\u4e0b\u9762\u7684\u5bf9\u6bd4\uff1aRTU\u6a21\u5f0f\u7684\u5dee\u9519\u6821\u9a8c\uff1a\n\n![\u56fe\u7247](https:\/\/image.modbus.cn\/wp-content\/uploads\/2023\/10\/20231002221733232.png)\n\nASCII\u6a21\u5f0f\u7684\u5dee\u9519\u6821\u9a8c\uff1a\n\n![\u56fe\u7247](https:\/\/image.modbus.cn\/wp-content\/uploads\/2023\/10\/20231002221733713.png)\n\n\u603b\u800c\u8a00\u4e4b\uff0cRTU\u6a21\u5f0f\u4e0b\u7528\u7684\u662fCRC\u6821\u9a8c\uff0cASCII\u6a21\u5f0f\u4e0b\u7528\u7684\u662fLRC\u6821\u9a8c\u3002\n\n\u4e0b\u9762\u4f1a\u8bf4\u660e\u8fd9\u4e24\u8005\u4e0d\u540c\u7684\u6821\u9a8c\u65b9\u5f0f\u662f\u600e\u4e48\u5b9e\u73b0\u7684\u3002\n\n**1\u3001RTU\u6a21\u5f0f\u7684CRC\u6821\u9a8c**\n\n\u5728&nbsp;RTU&nbsp;\u6a21\u5f0f\u4e2d\u57fa\u4e8e\u5faa\u73af\u5197\u4f59\u6821\u9a8c&nbsp;(&nbsp;CRC&nbsp;-&nbsp;-&nbsp;Cyclical&nbsp;RedundancyChecking)&nbsp;\u7b97\u6cd5\u7684\u9519\u8bef\u68c0\u9a8c\u57df\u3002CRC \u57df\u68c0\u9a8c\u6574\u4e2a\u62a5\u6587\u7684\u5185\u5bb9\u3002\u4e0d\u7ba1\u62a5\u6587\u6709\u65e0\u5947\u5076\u6821\u9a8c\uff0c\u5747\u6267\u884c\u6b64\u68c0\u9a8c\u3002CRC \u5305\u542b\u7531\u4e24\u4e2a 8 \u4f4d\u5b57\u8282\u7ec4\u6210\u7684\u4e00\u4e2a 16 \u4f4d\u503c\uff0c\u4f5c\u4e3a\u62a5\u6587\u7684\u6700\u540e\u7684\u57df\u9644\u52a0\u5728\u62a5\u6587\u4e4b\u540e\u3002\u8ba1\u7b97\u540e\uff0c\u9996\u5148\u9644\u52a0\u4f4e\u5b57\u8282\uff0c\u7136\u540e\u662f\u9ad8\u5b57\u8282\u3002CRC\u9ad8\u5b57\u8282\u4e3a\u62a5\u6587\u53d1\u9001\u7684\u6700\u540e\u4e00\u4e2a\u5b50\u8282\u3002\u9644\u52a0\u5728\u62a5\u6587\u540e\u9762\u7684 CRC \u7684\u503c\u7531\u53d1\u9001\u8bbe\u5907\u8ba1\u7b97\u3002\u63a5\u6536\u8bbe\u5907\u5728\u63a5\u6536\u62a5\u6587\u65f6\u91cd\u65b0\u8ba1\u7b97 CRC \u7684\u503c\uff0c\u5e76\u5c06\u8ba1\u7b97\u7ed3\u679c\u4e8e\u5b9e\u9645\u63a5\u6536\u5230\u7684 CRC \u503c\u76f8\u6bd4\u8f83\u3002\u5982\u679c\u4e24\u4e2a\u503c\u4e0d\u76f8\u7b49\uff0c\u5219\u4e3a\u9519\u8bef\u3002\u8fd9\u6837\u7684\u64cd\u4f5c\u662f\u53ef\u4ee5\u9632\u6b62\u6570\u636e\u5728\u4f20\u9001\u8fc7\u6210\u53d1\u751f\u9519\u8bef\uff0c\u5bfc\u81f4\u6700\u540e\u51fa\u73b0\u5f02\u5e38\u73b0\u8c61\u3002\n\n**\uff081\uff09CRC\u751f\u6210\u7684\u539f\u7406**\n\nCRC \u7684\u751f\u6210\u8fc7\u7a0b\u4e2d\uff0c&nbsp;\u6bcf\u4e2a 8\u2013\u4f4d\u5b57\u7b26\u4e0e\u5bc4\u5b58\u5668\u4e2d\u7684\u503c\u5f02\u6216\u3002\u7136\u540e\u7ed3\u679c\u5411\u6700\u4f4e\u6709\u6548\u4f4d&nbsp;(LSB)&nbsp;\u65b9\u5411\u79fb\u52a8(Shift) 1 \u4f4d\uff0c\u800c\u6700\u9ad8\u6709\u6548\u4f4d&nbsp;(MSB)&nbsp;\u4f4d\u7f6e\u5145\u96f6\u3002&nbsp;\u7136\u540e\u63d0\u53d6\u5e76\u68c0\u67e5 LSB\uff1a\u5982\u679c LSB \u4e3a 1\uff0c&nbsp;\u5219\u5bc4\u5b58\u5668\u4e2d\u7684\u503c\u4e0e\u4e00\u4e2a\u56fa\u5b9a\u7684\u9884\u7f6e\u503c\u5f02\u6216\uff1b\u5982\u679c LSB \u4e3a&nbsp;0\uff0c&nbsp;\u5219\u4e0d\u8fdb\u884c\u5f02\u6216\u64cd\u4f5c\u3002\n\n\u8fd9\u4e2a\u8fc7\u7a0b\u5c06\u91cd\u590d\u76f4\u5230\u6267\u884c\u5b8c 8 \u6b21\u79fb\u4f4d\u3002\u5b8c\u6210\u6700\u540e\u4e00\u6b21\uff08\u7b2c 8 \u6b21\uff09\u79fb\u4f4d\u53ca\u76f8\u5173\u64cd\u4f5c\u540e\uff0c\u4e0b\u4e00\u4e2a 8\u4f4d\u5b57\u8282\u4e0e\u5bc4\u5b58\u5668\u7684\u5f53\u524d\u503c\u5f02\u6216\uff0c\u7136\u540e\u53c8\u540c\u4e0a\u9762\u63cf\u8ff0\u8fc7\u7684\u4e00\u6837\u91cd\u590d 8 \u6b21\u3002\u5f53\u6240\u6709\u62a5\u6587\u4e2d\u5b50\u8282\u90fd\u8fd0\u7b97\u4e4b\u540e\u5f97\u5230\u7684\u5bc4\u5b58\u5668\u4e2d\u7684\u6700\u7ec8\u503c\uff0c\u5c31\u662f CRC\u3002\n\n**\uff082\uff09CRC\u7684\u751f\u6210\u8fc7\u7a0b**\n\n```\n\r\n1. \u5c06\u4e00\u4e2a 16 \u4f4d\u5bc4\u5b58\u5668\u88c5\u5165\u5341\u516d\u8fdb\u5236 FFFF (\u5168 1). \u5c06\u4e4b\u79f0\u4f5c CRC \u5bc4\u5b58\u5668.\r\n2. \u5c06\u62a5\u6587\u7684\u7b2c\u4e00\u4e2a 8 \u4f4d\u5b57\u8282\u4e0e 16 \u4f4d CRC \u5bc4\u5b58\u5668\u7684\u4f4e\u5b57\u8282\u5f02\u6216\uff0c\u7ed3\u679c\u7f6e\u4e8e CRC \u5bc4\u5b58\u5668.\r\n3. \u5c06 CRC \u5bc4\u5b58\u5668\u53f3\u79fb 1 \u4f4d (\u5411 LSB \u65b9\u5411)\uff0c MSB \u5145\u96f6. \u63d0\u53d6\u5e76\u68c0\u6d4b LSB.\r\n4. (\u5982\u679c LSB \u4e3a 0): \u91cd\u590d\u6b65\u9aa4 3 (\u53e6\u4e00\u6b21\u79fb\u4f4d).(\u5982\u679c LSB \u4e3a 1): \u5bf9 CRC \u5bc4\u5b58\u5668\u5f02\u6216\u591a\u9879\u5f0f\u503c 0xA001 (1010 0000 0000 0001).\r\n5. \u91cd\u590d\u6b65\u9aa4 3 \u548c 4\uff0c\u76f4\u5230\u5b8c\u6210 8 \u6b21\u79fb\u4f4d\u3002\u5f53\u505a\u5b8c\u6b64\u64cd\u4f5c\u540e\uff0c\u5c06\u5b8c\u6210\u5bf9 8 \u4f4d\u5b57\u8282\u7684\u5b8c\u6574\u64cd\u4f5c\u3002\r\n6. \u5bf9\u62a5\u6587\u4e2d\u7684\u4e0b\u4e00\u4e2a\u5b57\u8282\u91cd\u590d\u6b65\u9aa4 2 \u5230 5\uff0c\u7ee7\u7eed\u6b64\u64cd\u4f5c\u76f4\u81f3\u6240\u6709\u62a5\u6587\u88ab\u5904\u7406\u5b8c\u6bd5\u3002\r\n7. CRC \u5bc4\u5b58\u5668\u4e2d\u7684\u6700\u7ec8\u5185\u5bb9\u4e3a CRC \u503c.\r\n8. \u5f53\u653e\u7f6e CRC \u503c\u4e8e\u62a5\u6587\u65f6\uff0c\u9ad8\u4f4e\u5b57\u8282\u5fc5\u987b\u4ea4\u6362\u3002\n```\n\n\u6d41\u7a0b\u56fe\u5982\u4e0b\uff1a\n\n![\u56fe\u7247](https:\/\/image.modbus.cn\/wp-content\/uploads\/2023\/10\/20231002221734694.png)\n\n**\uff083\uff09CRC\u6821\u9a8c\u751f\u6210\u7684\u7b97\u6cd5\u5b9e\u73b0**CRC\u7684\u8ba1\u7b97\u4e00\u822c\u662f\u7528\u67e5\u8868\u6cd5\u5b9e\u73b0\uff0c\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u8ba1\u7b97\u901f\u5ea6\u5feb\uff0c\u7f3a\u70b9\u662f\u5360\u7528\u5185\u5b58\u8d44\u6e90\u591a\u3002CRC\u7684\u9ad8\u5b57\u8282\u8868\u5982\u4e0b\uff1a\n\n```\n\r\nstatic unsigned char auchCRCHi&#91;] = {\r\n0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81,\r\n0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,\r\n0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01,\r\n0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,\r\n0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81,\r\n0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,\r\n0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01,\r\n0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,\r\n0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81,\r\n0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,\r\n0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01,\r\n0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,\r\n0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81,\r\n0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,\r\n0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01,\r\n0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,\r\n0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81,\r\n0x40\r\n} ;\n```\n\nCRC\u7684\u4f4e\u5b57\u8282\u8868\u5982\u4e0b\uff1a\n\n```\n\r\nstatic char auchCRCLo&#91;] = {\r\n0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 0x07, 0xC7, 0x05, 0xC5, 0xC4,\r\n0x04, 0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09,\r\n0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A, 0x1E, 0xDE, 0xDF, 0x1F, 0xDD,\r\n0x1D, 0x1C, 0xDC, 0x14, 0xD4, 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3,\r\n0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3, 0xF2, 0x32, 0x36, 0xF6, 0xF7,\r\n0x37, 0xF5, 0x35, 0x34, 0xF4, 0x3C, 0xFC, 0xFD, 0x3D, 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A,\r\n0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, 0x28, 0xE8, 0xE9, 0x29, 0xEB, 0x2B, 0x2A, 0xEA, 0xEE,\r\n0x2E, 0x2F, 0xEF, 0x2D, 0xED, 0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26,\r\n0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60, 0x61, 0xA1, 0x63, 0xA3, 0xA2,\r\n0x62, 0x66, 0xA6, 0xA7, 0x67, 0xA5, 0x65, 0x64, 0xA4, 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F,\r\n0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68, 0x78, 0xB8, 0xB9, 0x79, 0xBB,\r\n0x7B, 0x7A, 0xBA, 0xBE, 0x7E, 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5,\r\n0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71, 0x70, 0xB0, 0x50, 0x90, 0x91,\r\n0x51, 0x93, 0x53, 0x52, 0x92, 0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C,\r\n0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B, 0x99, 0x59, 0x58, 0x98, 0x88,\r\n0x48, 0x49, 0x89, 0x4B, 0x8B, 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,\r\n0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83, 0x41, 0x81, 0x80,\r\n0x40\r\n};\n```\n\nCRC\u7684\u751f\u6210\u51fd\u6570\u5982\u4e0b\uff1a\n\n```\n\r\nunsigned short CRC16 ( puchMsg\uff0c usDataLen ) \/* \u51fd\u6570\u4ee5 unsigned short \u7c7b\u578b\u8fd4\u56de CRC *\/\r\n{\r\n    unsigned char *puchMsg ; \/* \u7528\u4e8e\u8ba1\u7b97 CRC \u7684\u62a5\u6587 *\/\r\n    unsigned short usDataLen ; \/* \u62a5\u6587\u4e2d\u7684\u5b57\u8282\u6570 *\/\r\n    unsigned char uchCRCHi = 0xFF ; \/* CRC \u7684\u9ad8\u5b57\u8282\u521d\u59cb\u5316 *\/\r\n    unsigned char uchCRCLo = 0xFF ; \/* CRC \u7684\u4f4e\u5b57\u8282\u521d\u59cb\u5316 *\/\r\n    unsigned uIndex ; \/* CRC \u67e5\u8be2\u8868\u7d22\u5f15 *\/\r\n\r\n    while (usDataLen--) \/* \u5b8c\u6210\u6574\u4e2a\u62a5\u6587\u7f13\u51b2\u533a *\/\r\n    {\r\n            uIndex = uchCRCLo ^ *puchMsgg++ ; \/* \u8ba1\u7b97 CRC *\/\r\n            uchCRCLo = uchCRCHi ^ auchCRCHi&#91;uIndex} ;\r\n            uchCRCHi = auchCRCLo&#91;uIndex] ;\r\n    }\r\n\r\n    return (uchCRCHi &lt;&lt; 8 | uchCRCLo) ;\r\n}\n```\n\n**\uff084\uff09CRC\u6821\u9a8c\u7801\u6dfb\u52a0\u5230\u62a5\u6587\u4e2d**\u5f53 16 \u4f4d CRC (2 \u4e2a 8 \u4f4d\u5b57\u8282)\u00a0\u5728\u62a5\u6587\u4e2d\u4f20\u9001\u65f6\uff0c\u4f4e\u4f4d\u5b57\u8282\u9996\u5148\u53d1\u9001\uff0c\u7136\u540e\u662f\u9ad8\u4f4d\u5b57\u8282\u3002\u4f8b\u5982\uff0c\u00a0\u5982\u679c CRC \u503c\u4e3a\u5341\u516d\u8fdb\u5236 1241 (0001 0010\u00a00100\u00a00001)\uff0c\u5982\u4e0b\uff1a\n\n![\u56fe\u7247](https:\/\/image.modbus.cn\/wp-content\/uploads\/2023\/10\/20231002221735953.png)\n\n2\u3001**ASCII\u6a21\u5f0f\u7684LRC\u6821\u9a8c**\n\n**\uff081\uff09LRC\u7684\u751f\u6210\u539f\u7406**\u7eb5\u5411\u5197\u4f59\u6821\u9a8c(LRC)\u4e3a\u4e00\u4e2a\u5b57\u8282\uff0c\u542b\u6709 8 \u4f4d\u4e8c\u8fdb\u5236\u503c\u3002LRC \u7531\u53d1\u9001\u8bbe\u5907\u8ba1\u7b97\uff0c\u5e76\u9644\u52a0 LRC \u5230\u62a5\u6587\u3002\u63a5\u6536\u8bbe\u5907\u5728\u63a5\u6536\u6587\u65f6\u8ba1\u7b97 LRC, \u5e76\u5c06\u8ba1\u7b97\u7684\u7ed3\u679c\u4e0e\u5728 LRC \u63a5\u6536\u5230\u7684\u5b9e\u9645\u503c\u76f8\u6bd4\u8f83\uff0c\u5982\u679c\u4e24\u4e2a\u503c\u4e0d\u76f8\u7b49\uff0c\u5219\u7ed3\u679c\u4e3a\u9519\u3002LRC \u7684\u8ba1\u7b97, \u5bf9\u62a5\u6587\u4e2d\u7684\u6240\u6709\u7684\u8fde\u7eed 8 \u4f4d\u5b57\u8282\u76f8\u52a0\uff0c\u5ffd\u7565\u4efb\u4f55\u8fdb\u4f4d\uff0c\u7136\u540e\u6c42\u51fa\u5176\u4e8c\u8fdb\u5236\u8865\u7801\u3002LRC \u4e3a\u4e00\u4e2a 8 \u4f4d\u57df\uff0c\u90a3\u4e48\u6bcf\u4e2a\u4f1a\u5bfc\u81f4\u503c\u5927\u4e8e 255 \u65b0\u7684\u76f8\u52a0\u53ea\u662f\u7b80\u5355\u7684\u5c06\u57df\u7684\u503c\u5728\u96f6\u201d\u56de\u7ed5\u201d\u3002\u56e0\u4e3a\u6ca1\u6709\u7b2c 9 \u4f4d\uff0c\u8fdb\u4f4d\u88ab\u81ea\u52a8\u653e\u5f03\u3002\n\n**\uff082\uff09LRC\u7684\u751f\u6210\u8fc7\u7a0b**\n\n```\n\r\n1.\u4e0d\u5305\u62ec\u8d77\u59cb\u201d\u5192\u53f7\u201d\u548c\u7ed3\u675f CRLF \u7684\u62a5\u6587\u4e2d\u7684\u6240\u6709\u5b57\u8282\u76f8\u52a0\u5230\u4e00\u4e2a 8 \u4f4d\u57df\uff0c\u6545\u6b64\u8fdb\u4f4d\u88ab\u4e22\u5f03\u3002\r\n2.\u4ece FF (\u5168 1)\u5341\u516d\u8fdb\u5236\u4e2d\u51cf\u53bb\u57df\u7684\u6700\u7ec8\u503c\uff0c\u4ea7\u751f 1 \u7684\u8865\u7801(\u4e8c\u8fdb\u5236\u53cd\u7801)\u3002\r\n3.\u52a0 1 \u4ea7\u751f\u4e8c\u8fdb\u5236\u8865\u7801.\n```\n\n**\uff083\uff09LRC\u6821\u9a8c\u751f\u6210\u7684\u7b97\u6cd5**\n\n```\n\r\nstatic unsigned char LRC(auchMsg\uff0c usDataLen) \/* \u51fd\u6570\u8fd4\u56de unsigned char \u7c7b\u578b\u7684 LRC \u7ed3\u679c*\/\r\n{\r\n    unsigned char *auchMsg ; \/* \u8981\u8ba1\u7b97 LRC \u7684\u62a5\u6587*\/\r\n    unsigned short usDataLen ; \/* \u62a5\u6587\u7684\u5b57\u8282\u6570 *\/\r\n    unsigned char uchLRC = 0 ; \/* LRC \u521d\u59cb\u5316 *\/\r\n\r\n    while (usDataLen--) \/* \u5b8c\u6210\u6574\u4e2a\u62a5\u6587\u7f13\u51b2\u533a *\/\r\n        uchLRC += *auchMsg++ ; \/* \u7f13\u51b2\u533a\u5b57\u8282\u76f8\u52a0\uff0c\u65e0\u8fdb\u4f4d *\/\r\n\r\n    return ((unsigned char)(-((char)uchLRC))) ; \/* \u8fd4\u56de\u4e8c\u8fdb\u5236\u8865\u7801 *\/\r\n}\n```\n\n**\uff084\uff09\u5c06LRC\u6821\u9a8c\u7801\u6dfb\u52a0\u5230\u62a5\u6587\u4e2d**\u5f53 8 \u4f4d LRC (2 \u4e2a ASCII \u5b57\u7b26)&nbsp;\u5728\u62a5\u6587\u4e2d\u4f20\u9001\u65f6\uff0c\u9ad8\u4f4d\u5b57\u7b26\u9996\u5148\u53d1\u9001\uff0c\u7136\u540e\u662f\u4f4e\u4f4d\u5b57\u7b26\u3002\u4f8b\u5982\uff0c\u5982\u679c LRC \u503c\u4e3a\u5341\u516d\u8fdb\u5236 61 (0110&nbsp;0001)\uff0c\u5982\u4e0b\uff1a\n\n![\u56fe\u7247](https:\/\/image.modbus.cn\/wp-content\/uploads\/2023\/10\/20231002221735455.png)\n\n---\n*modbus.cn*\n"