博客
关于我
CRC16循环冗余校验 RTU-MODBUS标准 Linux C
阅读量:205 次
发布时间:2019-02-28

本文共 3978 字,大约阅读时间需要 13 分钟。

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 
#include
#include
/*! -------------------------------------------------------------------------- *//*! 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

 

转载地址:http://qbwp.baihongyu.com/

你可能感兴趣的文章
Mysql 分页
查看>>
Mysql 分页语句 Limit原理
查看>>
MySql 创建函数 Error Code : 1418
查看>>
MySQL 创建新用户及授予权限的完整流程
查看>>
mysql 创建表,不能包含关键字values 以及 表id自增问题
查看>>
mysql 删除日志文件详解
查看>>
mysql 判断表字段是否存在,然后修改
查看>>
MySQL 到底能不能放到 Docker 里跑?
查看>>
mysql 前缀索引 命令_11 | Mysql怎么给字符串字段加索引?
查看>>
MySQL 加锁处理分析
查看>>
mysql 协议的退出命令包及解析
查看>>
mysql 参数 innodb_flush_log_at_trx_commit
查看>>
mysql 取表中分组之后最新一条数据 分组最新数据 分组取最新数据 分组数据 获取每个分类的最新数据
查看>>
MySQL 命令和内置函数
查看>>
MySQL 和 PostgreSQL,我到底选择哪个?
查看>>
mysql 四种存储引擎
查看>>
MySQL 在并发场景下的问题及解决思路
查看>>
MySQL 在控制台插入数据时,中文乱码问题的解决
查看>>
MySQL 基础架构
查看>>
MySQL 基础模块的面试题总结
查看>>