1、概述

CRC16循环冗余校验常用在MODBUS协议中,用于校验报文的完整性。CRC16校验值为uint16_t 无符号整形2字节,在MODBUS协议中,低检验字节在前,高校验字节在后,比如校验结果crc16=0x1788,则MODBUS中的校验顺序为 ...0x88 0x17。

以下为自己写的linux C 代码,可以直接用。

2、C程序

/*******************************************************************************
_____ ___ ____  ___   _____    _             _   _
| ____|_ _|  _ \|_ _| |_   _|__(_)_ __   __ _| | | |_   _  __ _
|  _|  | || |_) || |    | |/ __| | '_ \ / _` | |_| | | | |/ _` |
| |___ | ||  _ < | |    | |\__ \ | | | | (_| |  _  | |_| | (_| |
|_____|___|_| \_\___|   |_||___/_|_| |_|\__, |_| |_|\__,_|\__,_|* File Name          : main.c* Description        : This file provides code for crc16 caculation in linuxc.* Author             : jackwang by jiawang16@foxmail.com* Date               : 2018-07-15
*******************************************************************************/
/*! -------------------------------------------------------------------------- */
/*! Include headers */
#include <stdio.h>
#include <string.h>
#include <stdbool.h>/*! -------------------------------------------------------------------------- */
/*! Private function declarations */
static unsigned char Char2Int(char chr,bool *isOK);/*! convert char to int type*/
static unsigned char HexStr2Int(char *str, bool *isOK);/*!convert hexstr to int*/
/*! caculate crc16 of buff-input:arr_buff and length: len */
static unsigned short GenerateCRC16(unsigned char *arr_buff, unsigned short len);/*! -------------------------------------------------------------------------- */
/*! main function defination */
int main(int argc, char* argv[])
{/*! variable define */int ret = 0;int numByte = argc; unsigned char bccVal = 0x00;char inPutbuff[10];unsigned char databuff[1000];unsigned short buffsize = 0;unsigned short crcVal;bool isOK;int Nibb;if(argc == 1){printf("[note]  no params to caculate, please input hex string,splite by space!\r\n");}else{printf("[note]  input %d byte: ",numByte-1);for(int i = 1; i < numByte; i++){printf("%s ",argv[i]);}printf("\r\n");for(int i = 1; i < numByte; i++){memcpy(inPutbuff,argv[i],2);Nibb = HexStr2Int(inPutbuff,&isOK);if(isOK){databuff[i-1] = Nibb;buffsize++;}}crcVal = GenerateCRC16(databuff,buffsize);printf("[note]  crc16 value: 0x%04X\r\n",crcVal);}return 0;
}/*! -------------------------------------------------------------------------- */
/*! Private function definations */
/*! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
static unsigned char Char2Int(char chr,bool *isOK)
{unsigned char nibb1;if(chr >= '0' && chr <= '9'){ nibb1 = chr - '0'; *isOK = true;}else if(chr >= 'a' && chr <= 'f'){ nibb1 = chr - 'a' + 10; *isOK = true;}else if(chr >= 'A' && chr <= 'F'){ nibb1 = chr -'A' + 10; *isOK = true; }else{  printf("[error]  invalid hex str input: %c \r\n",chr); *isOK = false; }return nibb1;
}/*! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
static unsigned char HexStr2Int(char *str, bool *isOK)
{unsigned char nibb1,nibb2;bool isOK1,isOK2;nibb1 = Char2Int(*str, &isOK1);nibb2 = Char2Int(*(str+1),&isOK2);if(isOK1 && isOK2){*isOK = true;return nibb1*16 + nibb2;}else{*isOK = false;return 0;}
} /*! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
static unsigned short GenerateCRC16(unsigned char *arr_buff, unsigned short len)
{unsigned short crc=0xFFFF;unsigned char i, j;for ( j=0; j < len;j++){crc=crc ^*arr_buff++;for ( i=0; i<8; i++) {if( ( crc&0x0001) >0){crc=crc>>1;crc=crc^ 0xa001;}else{ crc=crc>>1;}}}return crc;
}

3、编译

~$ gcc main.c -o getcrc16

4、使用


~$ ./getcrc16 01 03 00 04 00 04
~$ [note]  input 6 byte: 01 03 00 04 00 04
~$ [note]  crc16 value: 0xC805

CRC16循环冗余校验 RTU-MODBUS标准 Linux C相关推荐

  1. 一文详解循环冗余校验校验算法(CRC校验)及C语言代码的实现 ---- 以CRC-16/MODBUS为例讲解

    一.概述 现在的产品开发过程中,无论是数据的储存还是传输,都需要确保数据的准确性,所以就需要在数据帧后面附加一串校验码,方便接收方使用校验码校验接收到的数据是否是正确的. 常用的校验方式有奇偶校验.异 ...

  2. 单项式计算机在线使用,16进制(CRC16)(多项式异或,多种)校验(2),循环冗余校验在线计算器_三贝计算网_23bei.com...

    本工具用于快速求出通信中CRC16校验值,包括: 1)CRC-16/DECT-R(别名:R-CRC-16). 2)CRC-16/DECT-X(别名:X-CRC-16). 3)CRC-16/GENIBU ...

  3. c语言累加和校验_循环冗余校验(CRC)算法入门

    http://blog.csdn.net/liyuanbhu/article/details/7882789 前言 CRC校验(循环冗余校验)是数据通讯中最常采用的校验方式.在嵌入式软件开发中,经常要 ...

  4. 【科普向】谁都能看懂的CRC(循环冗余校验)原理

    CRC原理 简介 CRC基本原理 模二运算 二进制系数多项式 CRC算法 示例 CRC算法的数学描述 常用CRC版本 CRC算法的编程实现 简介 循环冗余校验(Cyclic Redundancy Ch ...

  5. CRC循环冗余校验码总结

    一.CRC简介 先在此说明下什么是CRC:循环冗余码校验 英文名称为Cyclical Redundancy Check,简称CRC,它是利用除法及余数的原理来作错误侦测(Error Detecting ...

  6. 写给嵌入式程序员的循环冗余校验(CRC)算法入门引导

    CRC校验(循环冗余校验)是数据通讯中最常采用的校验方式.在嵌入式软件开发中,经常要用到CRC 算法对各种数据进行校验.因此,掌握基本的CRC算法应是嵌入式程序员的基本技能.可是,我认识的嵌入式程序员 ...

  7. 循环冗余校验(CRC)算法入门引导

    写给嵌入式程序员的循环冗余校验(CRC)算法入门引导 前言 CRC校验(循环冗余校验)是数据通讯中最常采用的校验方式.在嵌入式软件开发中,经常要用到CRC 算法对各种数据进行校验.因此,掌握基本的CR ...

  8. CRC(循环冗余校验)

    在数据传输过程中,无论传输系统的设计再怎么完美,差错总会存在,这种差错可能会导致在链路上传输的一个或者多个帧被破坏(出现比特差错,0变为1,或者1变为0),从而接受方接收到错误的数据.为尽量提高接受方 ...

  9. CRC循环冗余校验检错的原理

    比起奇偶校验码(PCC)只能校验一位错误,循环冗余校验码(CRC)的检错能力更强,可以检出多位错误. 1.CRC校验原理 CRC校验原理看起来比较复杂,好难懂,因为大多数书上基本上是以二进制的多项式形 ...

最新文章

  1. silverlight4.0 写文件不能设置默认文件名
  2. ios启动页尺寸_关于移动端App启动页的策划方案
  3. 3D 视觉 相关知识-SLAM框架-常见方案对比
  4. 7000字 Redis 超详细总结笔记总 | 收藏必备!
  5. ASP.NET MVC:@helper 不能调试
  6. Fiddler抓包使用教程-扫盲篇
  7. 产品经理应该如何设计面包屑原理
  8. JavaScript DOM操作 提高篇
  9. oracle catalog命令,db2 catalog tcpip 命令咨询?
  10. 收藏 | Python数据分析必备速查表
  11. ftp ---- 虚拟用户登录(实例配置)
  12. 一个类的类类型是Class类的实例,即类的字节码
  13. 更改Cygwin的主机名,改WINDOWS的计算机名即可
  14. 【经典箴言 || 人生感悟 】//wodeganwu3034 == 8. 回溯做过的事情,如Review自己曾经写过的代码或者设计,思考为什么会这么做,有没有更好的方法==
  15. vivo浏览器缓存视频如何拷贝到电脑上
  16. 修改docker时区为北京时间
  17. html tbody增加行,使用jquery向表的tbody添加行
  18. 「标签管理」使用标签管理有道云笔记资料
  19. 【解决方案 十二】一文彻底解决文件格式判别问题
  20. 如何 使用 apache 访问 本地目录及本地文件

热门文章

  1. 单元格内容分列多行_姓名太多,放在一列打印时浪费纸张,可以分成多行多列打印...
  2. c++分治法求最大最小值实现_最优化计算与matlab实现(12)——非线性最小二乘优化问题——G-N法...
  3. java二维数组水平翻转,C 语言 利用二维数组实现对输入的数组进行翻转
  4. mysql for update 锁_MySql FOR UPDATE 锁的一点问题……
  5. mysql8.0主从配置,MySQL 8.0主从服务器(Master-Slave)配置
  6. oracle集群监听启动,在RAC中lsnrctl和srvctl操作监听区别
  7. 参数估计_随机微分方程的参数估计(一)
  8. 小程序 、h5图片铺满div,自适应容器大小
  9. Linux下test命令使用
  10. 原始套接字抓取所有以太网数据包与分析