博客
关于我
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中yield的用法详解——最简单,最清晰的解释
查看>>
python中‘set.intersection()‘的算法是什么?
查看>>
Python中一个图形中的多个绘图
查看>>
python中一个好的持久同步队列
查看>>
Python中不准确的对数
查看>>
python中什么是实例方法_在Python中创建实例方法对象的不同方法
查看>>
Python中使用cutecharts实现简单的手绘风格的图表
查看>>
python中使用dxfgrabber,解析dxf文件信息生成obj文件
查看>>
Python中使用ncmbot玩转网易云音乐
查看>>
Python中使用PIL快速实现二值图
查看>>
Python中使用PIL快速实现灰度图
查看>>
Python中使用pip install ncmbot时各种错误解决办法
查看>>
Python中使用requests和parsel爬取喜马拉雅电台音频
查看>>
Python中使用turtle绘制多个同心圆
查看>>
Python中使用turtle绘制棋盘详解
查看>>
Python中使用wordcloud生成漂亮的中文词云
查看>>
Python中使用wxpy定时机器人给微信好友发送每日一文
查看>>
Python中使用zip()并行迭代
查看>>
Python中使用推导式创建序列
查看>>
Python中信号处理程序和atexit处理程序的调用
查看>>