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

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

1、概述

CRC16循环冗余校验是一种常用的数据完整性校验方法,尤其在MODBUS协议中应用广泛。CRC16校验值为16位无符号整数,通常以两个字节表示,低检验字节在前,高检验字节在后。例如,若校验值为0x1788,则MODBUS报文中校验顺序为...0x88 0x17。

2、C程序实现

以下是实现CRC16校验的Linux C代码示例:

```c/****************************************************************************** * 文件名称: main.c * 描述: 提供CRC16校验的Linux实现 * 作者: jackwang * 日期: 2018-07-15 ******************************************************************************/#include
#include
#include

static unsigned char Char2Int(char chr, bool *isOK);static unsigned char HexStr2Int(char *str, bool *isOK);static unsigned short GenerateCRC16(unsigned char *arr_buff, unsigned short len);

int main(int argc, char *argv) {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] 无输入参数,请输入要校验的字节数据,按空格分隔。\r\n");} else {    printf("[note] 输入 %d 个字节的数据:", 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校验值为:0x%04X\r\n", crcVal);}return 0;

}

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] 无效的Hex字符输入:%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 *arrBuff, unsigned short len) {unsigned short crc = 0xFFFF;unsigned char i, j;

for (j = 0; j < len; ++j) {    crc = crc ^ *arrBuff++;    for (i = 0; i < 8; ++i) {        if ((crc & 0x0001) > 0) {            crc = crc >> 1;            crc = crc ^ 0xA001;        } else {            crc = crc >> 1;        }    }}return crc;

}

3、编译

可以使用以下命令编译程序:

```bashgcc main.c -o getcrc16```

4、使用示例

运行程序时,按照以下格式输入Hex字节数据:

```bash./getcrc16 01 03 00 04 00 04```

输出结果为:

```bash[note] 输入 6 字节的数据:01 03 00 04 00 04[note] CRC16校验值为:0xC805```

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

你可能感兴趣的文章
python图像条状状噪声,使用PYTHON PIL从验证码图像中删除背景嘈杂的线条
查看>>
Python图像处理:从内存加载jpeg
查看>>
python固定后缀(名物化词汇)词频统计:抽取+统计+可视化
查看>>
python商品评论数据采集与分析可视化系统 Flask框架 requests爬虫 NLP情感分析 毕业设计 源码
查看>>
python商品数据分析可视化系统(带爬虫)京东销售数据分析 计算机毕业设计 源码下载
查看>>
python商品库存管理系统 django框架 商品网站 MySQL数据库 源码下载 计算机毕业设计
查看>>
Python哪个版本最稳定好用2023.10.19
查看>>
Python和黑客技术的渊源
查看>>
Python和RF编写接口自动化
查看>>
Python和RF编写web自动化
查看>>
python和js哪个难_【Python】记一次学习,Python 与 js 在实现闭包时的差异
查看>>
python和java哪个更值得学——来自知乎高赞回答
查看>>
python和c混合编程 github_在pycharm中使用git版本管理以及同步github的方法
查看>>
python命名空间更改_Python模块xml.etree.ElementTree自动修改xml命名空间键
查看>>
Python命名中尾随下划线有什么好处?
查看>>
Python启动脚本
查看>>
Python听写汇总结果
查看>>
python吊打Excel?屁!那是你不会用!
查看>>
Python合并两张图片 | 先叠透明度再合并 (附Demo)
查看>>
python各进制的表述与转换
查看>>