CRC16modbus计算(C实现)

#include <stdio.h>
#include <string.h>
unsigned short utils_crc16_modbus( unsigned char *buff,int len)
{
    unsigned short tmp = 0xffff;
    unsigned short ret1 = 0;
  
    for(int n = 0; n < len; n++){
        tmp = buff[n] ^ tmp;
        for(int i = 0;i < 8;i++){ 
            if(tmp & 0x01){
                tmp = tmp >> 1;
                tmp = tmp ^ 0xa001;
            }   
            else{
                tmp = tmp >> 1;
            }   
        }   
    }   
    return tmp;
}

  

原文地址:https://www.cnblogs.com/yuanqiangfei/p/15789306.html