user-0 提问: 求教，串口通信的求和校验怎么做？

回复:
user: user-1
content: 求校验和简单多了。

user: user-1
content: 仅供参考： #include <stdio.h> int main() {     unsigned char d[5]={0x12,0x34,0x56,0x78,0x00};     unsigned char sum,cksum;     int i;      sum=0;     for (i=0;i<4;i++) {         sum+=d[i];     }     sum=~sum+1;     d[4]=sum;      cksum=0;     for (i=0;i<5;i++) {         cksum+=d[i];     }     if (cksum==0) {         printf("cksum ok.\n");     } else {         printf("cksum err!\n");     }      d[2]=0x55;      cksum=0;     for (i=0;i<5;i++) {         cksum+=d[i];     }     if (cksum==0) {         printf("cksum ok.\n");     } else {         printf("cksum err!\n");     }      return 0; } //cksum ok. //cksum err! //

user: user-2
content: 求合校验, 就是把每个字节拿来相加就OK了, 溢出也不用管, 反正只看结果. 如果校验结果有1字节表示, 那么就用 unsigned char来保存累加和,  多个字节也类似.  比如计算 char [10]的校验和,  那么就把0-9累加起来, 最终得到一个数值, 不用关心溢出.

user: user-0
content: 引用 3 楼 zhao4zhong1 的回复:求校验和简单多了。  感觉应该是，但是没有找到相关的资料，可否给个例子指点一二。谢谢

user: user-0
content: 引用 1 楼 zhao4zhong1 的回复:仅供参考：/***** crc16.c *****/ #include <stdio.h>  #define CRC16_DNP   0x3D65u     // DNP, IEC 870, M-BUS, wM-BUS, ... #define CRC16_CCITT 0x1021u     // X.25, V.41, HDLC FCS, Bluetooth, ...  //Other polynoms not tested #define CRC16_IBM   0x8005u     // ModBus, USB, Bisync, CRC-16, CRC-16-ANSI, ... #define CRC16_T10_DIF   0x8BB7u     // SCSI DIF #define CRC16_DECT  0x0589u     // Cordeless Telephones #define CRC16_ARINC 0xA02Bu     // ACARS Aplications  #define POLYNOM     CRC16_DNP   // Define the used polynom from one of the aboves  // Calculates the new crc16 with the newByte. Variable crcValue is the actual or initial value (0). unsigned short crc16(unsigned short crcValue, unsigned char newByte) {     int i;      for (i = 0; i < 8; i++) {         if (((crcValue & 0x8000u) >> 8) ^ (newByte & 0x80u)){             crcValue = (crcValue << 1)  ^ POLYNOM;         } else {             crcValue = (crcValue << 1);         }         newByte <<= 1;     }     return crcValue; }  unsigned short exampleOfUseCRC16(unsigned char *Data, int len){      unsigned short crc;     int aux = 0;      crc = 0x0000u; //Initialization of crc to 0x0000 for DNP     //crc = 0xFFFFu; //Initialization of crc to 0xFFFF for CCITT      while (aux < len){         crc = crc16(crc,Data[aux]);         aux++;     }      return (~crc); //The crc value for DNP it is obtained by NOT operation      //return crc; //The crc value for CCITT }  int main() {     unsigned char d[10]={0,1,2,3,4,5,6,7,8,9};      printf("0x%04hX\n",exampleOfUseCRC16(d,10));//0x1078     return 0; }  您好， 您好，这个是CRC校验吧，和求和校验一样吗？谢谢~~

user: user-1
content: 仅供参考：/***** crc16.c *****/ #include <stdio.h>  #define CRC16_DNP   0x3D65u     // DNP, IEC 870, M-BUS, wM-BUS, ... #define CRC16_CCITT 0x1021u     // X.25, V.41, HDLC FCS, Bluetooth, ...  //Other polynoms not tested #define CRC16_IBM   0x8005u     // ModBus, USB, Bisync, CRC-16, CRC-16-ANSI, ... #define CRC16_T10_DIF   0x8BB7u     // SCSI DIF #define CRC16_DECT  0x0589u     // Cordeless Telephones #define CRC16_ARINC 0xA02Bu     // ACARS Aplications  #define POLYNOM     CRC16_DNP   // Define the used polynom from one of the aboves  // Calculates the new crc16 with the newByte. Variable crcValue is the actual or initial value (0). unsigned short crc16(unsigned short crcValue, unsigned char newByte) {     int i;      for (i = 0; i < 8; i++) {         if (((crcValue & 0x8000u) >> 8) ^ (newByte & 0x80u)){             crcValue = (crcValue << 1)  ^ POLYNOM;         } else {             crcValue = (crcValue << 1);         }         newByte <<= 1;     }     return crcValue; }  unsigned short exampleOfUseCRC16(unsigned char *Data, int len){      unsigned short crc;     int aux = 0;      crc = 0x0000u; //Initialization of crc to 0x0000 for DNP     //crc = 0xFFFFu; //Initialization of crc to 0xFFFF for CCITT      while (aux < len){         crc = crc16(crc,Data[aux]);         aux++;     }      return (~crc); //The crc value for DNP it is obtained by NOT operation      //return crc; //The crc value for CCITT }  int main() {     unsigned char d[10]={0,1,2,3,4,5,6,7,8,9};      printf("0x%04hX\n",exampleOfUseCRC16(d,10));//0x1078     return 0; }