java实现CRC16 MODBUS校验算法

article/2025/8/23 3:10:47

最近进行led彩屏通讯开发,接触到CRC16算法计算校验码,先附上两篇网上可用的代码

 /*** 计算CRC16校验码** @param bytes* @return*/public static String getCRC(byte[] bytes) {int CRC = 0x0000ffff;int POLYNOMIAL = 0x0000a001;int i, j;for (i = 0; i < bytes.length; i++) {CRC ^= ((int) bytes[i] & 0x000000ff);for (j = 0; j < 8; j++) {if ((CRC & 0x00000001) != 0) {CRC >>= 1;CRC ^= POLYNOMIAL;} else {CRC >>= 1;}}}return Integer.toHexString(CRC);}
/*** 查表法计算CRC16校验** @param data 需要计算的字节数组*/public static String getCRC3(byte[] data) {byte[] crc16_h = {(byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40,(byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41,(byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41,(byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40,(byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41,(byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40,(byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40,(byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41,(byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41,(byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40,(byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40,(byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41,(byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40,(byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41,(byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41,(byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40};byte[] crc16_l = {(byte) 0x00, (byte) 0xC0, (byte) 0xC1, (byte) 0x01, (byte) 0xC3, (byte) 0x03, (byte) 0x02, (byte) 0xC2, (byte) 0xC6, (byte) 0x06, (byte) 0x07, (byte) 0xC7, (byte) 0x05, (byte) 0xC5, (byte) 0xC4, (byte) 0x04,(byte) 0xCC, (byte) 0x0C, (byte) 0x0D, (byte) 0xCD, (byte) 0x0F, (byte) 0xCF, (byte) 0xCE, (byte) 0x0E, (byte) 0x0A, (byte) 0xCA, (byte) 0xCB, (byte) 0x0B, (byte) 0xC9, (byte) 0x09, (byte) 0x08, (byte) 0xC8,(byte) 0xD8, (byte) 0x18, (byte) 0x19, (byte) 0xD9, (byte) 0x1B, (byte) 0xDB, (byte) 0xDA, (byte) 0x1A, (byte) 0x1E, (byte) 0xDE, (byte) 0xDF, (byte) 0x1F, (byte) 0xDD, (byte) 0x1D, (byte) 0x1C, (byte) 0xDC,(byte) 0x14, (byte) 0xD4, (byte) 0xD5, (byte) 0x15, (byte) 0xD7, (byte) 0x17, (byte) 0x16, (byte) 0xD6, (byte) 0xD2, (byte) 0x12, (byte) 0x13, (byte) 0xD3, (byte) 0x11, (byte) 0xD1, (byte) 0xD0, (byte) 0x10,(byte) 0xF0, (byte) 0x30, (byte) 0x31, (byte) 0xF1, (byte) 0x33, (byte) 0xF3, (byte) 0xF2, (byte) 0x32, (byte) 0x36, (byte) 0xF6, (byte) 0xF7, (byte) 0x37, (byte) 0xF5, (byte) 0x35, (byte) 0x34, (byte) 0xF4,(byte) 0x3C, (byte) 0xFC, (byte) 0xFD, (byte) 0x3D, (byte) 0xFF, (byte) 0x3F, (byte) 0x3E, (byte) 0xFE, (byte) 0xFA, (byte) 0x3A, (byte) 0x3B, (byte) 0xFB, (byte) 0x39, (byte) 0xF9, (byte) 0xF8, (byte) 0x38,(byte) 0x28, (byte) 0xE8, (byte) 0xE9, (byte) 0x29, (byte) 0xEB, (byte) 0x2B, (byte) 0x2A, (byte) 0xEA, (byte) 0xEE, (byte) 0x2E, (byte) 0x2F, (byte) 0xEF, (byte) 0x2D, (byte) 0xED, (byte) 0xEC, (byte) 0x2C,(byte) 0xE4, (byte) 0x24, (byte) 0x25, (byte) 0xE5, (byte) 0x27, (byte) 0xE7, (byte) 0xE6, (byte) 0x26, (byte) 0x22, (byte) 0xE2, (byte) 0xE3, (byte) 0x23, (byte) 0xE1, (byte) 0x21, (byte) 0x20, (byte) 0xE0,(byte) 0xA0, (byte) 0x60, (byte) 0x61, (byte) 0xA1, (byte) 0x63, (byte) 0xA3, (byte) 0xA2, (byte) 0x62, (byte) 0x66, (byte) 0xA6, (byte) 0xA7, (byte) 0x67, (byte) 0xA5, (byte) 0x65, (byte) 0x64, (byte) 0xA4,(byte) 0x6C, (byte) 0xAC, (byte) 0xAD, (byte) 0x6D, (byte) 0xAF, (byte) 0x6F, (byte) 0x6E, (byte) 0xAE, (byte) 0xAA, (byte) 0x6A, (byte) 0x6B, (byte) 0xAB, (byte) 0x69, (byte) 0xA9, (byte) 0xA8, (byte) 0x68,(byte) 0x78, (byte) 0xB8, (byte) 0xB9, (byte) 0x79, (byte) 0xBB, (byte) 0x7B, (byte) 0x7A, (byte) 0xBA, (byte) 0xBE, (byte) 0x7E, (byte) 0x7F, (byte) 0xBF, (byte) 0x7D, (byte) 0xBD, (byte) 0xBC, (byte) 0x7C,(byte) 0xB4, (byte) 0x74, (byte) 0x75, (byte) 0xB5, (byte) 0x77, (byte) 0xB7, (byte) 0xB6, (byte) 0x76, (byte) 0x72, (byte) 0xB2, (byte) 0xB3, (byte) 0x73, (byte) 0xB1, (byte) 0x71, (byte) 0x70, (byte) 0xB0,(byte) 0x50, (byte) 0x90, (byte) 0x91, (byte) 0x51, (byte) 0x93, (byte) 0x53, (byte) 0x52, (byte) 0x92, (byte) 0x96, (byte) 0x56, (byte) 0x57, (byte) 0x97, (byte) 0x55, (byte) 0x95, (byte) 0x94, (byte) 0x54,(byte) 0x9C, (byte) 0x5C, (byte) 0x5D, (byte) 0x9D, (byte) 0x5F, (byte) 0x9F, (byte) 0x9E, (byte) 0x5E, (byte) 0x5A, (byte) 0x9A, (byte) 0x9B, (byte) 0x5B, (byte) 0x99, (byte) 0x59, (byte) 0x58, (byte) 0x98,(byte) 0x88, (byte) 0x48, (byte) 0x49, (byte) 0x89, (byte) 0x4B, (byte) 0x8B, (byte) 0x8A, (byte) 0x4A, (byte) 0x4E, (byte) 0x8E, (byte) 0x8F, (byte) 0x4F, (byte) 0x8D, (byte) 0x4D, (byte) 0x4C, (byte) 0x8C,(byte) 0x44, (byte) 0x84, (byte) 0x85, (byte) 0x45, (byte) 0x87, (byte) 0x47, (byte) 0x46, (byte) 0x86, (byte) 0x82, (byte) 0x42, (byte) 0x43, (byte) 0x83, (byte) 0x41, (byte) 0x81, (byte) 0x80, (byte) 0x40};int crc = 0x0000ffff;int ucCRCHi = 0x00ff;int ucCRCLo = 0x00ff;int iIndex;for (int i = 0; i < data.length; ++i) {iIndex = (ucCRCLo ^ data[i]) & 0x00ff;ucCRCLo = ucCRCHi ^ crc16_h[iIndex];ucCRCHi = crc16_l[iIndex];}crc = ((ucCRCHi & 0x00ff) << 8) | (ucCRCLo & 0x00ff) & 0xffff;//高低位互换,输出符合相关工具对Modbus CRC16的运算crc = ( (crc & 0xFF00) >> 8) | ( (crc & 0x00FF ) << 8);return String.format("%04X", crc);}

两个方法亲测可用后,我查了下算法原理,并且自己稍微修改了一下第一个方法显得更直观。附:可能有人和我一样当初对上面第一个方法为什么最后没有转换高低位感到奇怪,现在我的理解是看需求。

public static String getCRC2(byte[] bytes) {
//        ModBus 通信协议的 CRC ( 冗余循环校验码含2个字节, 即 16 位二进制数。
//        CRC 码由发送设备计算, 放置于所发送信息帧的尾部。
//        接收信息设备再重新计算所接收信息 (除 CRC 之外的部分)的 CRC,
//        比较计算得到的 CRC 是否与接收到CRC相符, 如果两者不相符, 则认为数据出错。
//
//        1) 预置 1 个 16 位的寄存器为十六进制FFFF(即全为 1) , 称此寄存器为 CRC寄存器。
//        2) 把第一个 8 位二进制数据 (通信信息帧的第一个字节) 与 16 位的 CRC寄存器的低 8 位相异或, 把结果放于 CRC寄存器。
//        3) 把 CRC 寄存器的内容右移一位( 朝低位)用 0 填补最高位, 并检查右移后的移出位。
//        4) 如果移出位为 0, 重复第 3 步 ( 再次右移一位); 如果移出位为 1, CRC 寄存器与多项式A001 ( 1010 0000 0000 0001) 进行异或。
//        5) 重复步骤 3 和步骤 4, 直到右移 8 次,这样整个8位数据全部进行了处理。
//        6) 重复步骤 2 到步骤 5, 进行通信信息帧下一个字节的处理。
//        7) 将该通信信息帧所有字节按上述步骤计算完成后,得到的16位CRC寄存器的高、低字节进行交换。
//        8) 最后得到的 CRC寄存器内容即为 CRC码。int CRC = 0x0000ffff;int POLYNOMIAL = 0x0000a001;int i, j;for (i = 0; i < bytes.length; i++) {CRC ^= (int) bytes[i];for (j = 0; j < 8; j++) {if ((CRC & 0x00000001) == 1) {CRC >>= 1;CRC ^= POLYNOMIAL;} else {CRC >>= 1;}}}//高低位转换,看情况使用(譬如本人这次对led彩屏的通讯开发就规定校验码高位在前低位在后,也就不需要转换高低位)//CRC = ( (CRC & 0x0000FF00) >> 8) | ( (CRC & 0x000000FF ) << 8);return Integer.toHexString(CRC);}
 @Testpublic void testCRC16() throws Exception {//字符串转16进制byte数组String str16 = encode("aabb");byte[] bytes = str16.getBytes();System.out.println("str16:" + str16);System.out.println(getCRC(bytes));System.out.println(getCRC2(bytes));System.out.println(getCRC3(bytes));}

本文转载自: https://www.cnblogs.com/lujiannt/p/9246256.html


http://chatgpt.dhexx.cn/article/cTL9mOUy.shtml

相关文章

Modbus CRC16校验方法及实现代码

0.前言 项目中所用到的DI数字开关模块通信方式为串口通信&#xff0c;内置数据校验方式为Modbus CRC16。 这种校验方式第一次遇到&#xff0c;于是百度了一下&#xff0c;找到这篇文章&#xff0c;按照步骤&#xff0c;自己写了下代码&#xff0c;对比了下文中代码&#xff0…

crc16计算

参考&#xff1a;https://blog.csdn.net/j824117879/article/details/82019545 crc这个名词听着特别熟悉&#xff0c;但是由于工作性质的原因很少接触到&#xff0c;但最近工作中却碰到了crc校验&#xff0c;开发资料给了crc的初值和使用的crc表格&#xff0c;就只能根据这个初…

CRC16浅析

CRC即循环冗余校验码&#xff08;Cyclic Redundancy Check&#xff09;&#xff0c;是数据通信领域中最常用的一种查错校验码。奇偶校验虽然简单&#xff0c;但是漏检率太高&#xff0c;而CRC则要低的多&#xff0c;所以大多数都是使用CRC来校验。CRC也称为多项式码。 任何一个…

C语言版CRC-16系列校验算法

一、CRC16校验码的使用 现选择最常用的CRC-16校验&#xff0c;说明它的使用方法。 根据Modbus协议&#xff0c;常规485通讯的信息发送形式如下&#xff1a; 地址 功能码 数据信息 校验码 1byte 1byte nbyte 2byte CRC校验是前面几段数据内容的校验值&#xff0c;为一个16位数据…

LWIP使用经验

LWIP使用经验 一 LWIP内存管理 LWIP的内存管理使用了2种方式&#xff1a;内存池memp和内存堆mem&#xff0c;如图1所示。 内存池的特点是预先开辟多组固定大小的内存块组织成链表&#xff0c;实现简单&#xff0c;分配和回收速度快&#xff0c;不会产生内存碎片&#xff0c;…

Linux线程ID与内核LWP的关系

先给出图&#xff0c;然后我将根据图来讲解二者的关系&#xff1a; 线程ID 在使用线程前&#xff0c;我们需要链接线程的pthread第三方库&#xff0c;在Linux中&#xff0c;第三方库被加载到PCB进程中的虚拟地址空间中的栈区与堆区之间的共享区。 我们所创建线程的控制块即维护…

LWP(Library for WWW in Perl)的基本使用

LWP (“Library for WWW in Perl” 的缩写) 是一个由多个模块组成&#xff0c;用来获取网络数据的的模块组。 和很多 Perl 的模块一样。每一个 LWP 模块都自带详细的文档&#xff0c;做为对这个模块的完整介绍。可是面对 LWP 里的众多模块&#xff0c;有时候即使是完成最简单的…

Linux进程和轻量级进程(LWP)

1.父子进程可以共享含有程序代码的页,但是他们各自有独立的数据拷贝(堆和栈)。 2.进程描述符 3.进程的TASK_INTERRUPTIBLE和TASK_UNINTERRUPTIBLE区别

lwIP 开发指南

目录 lwIP 初探TCP/IP 协议栈是什么TCP/IP 协议栈架构TCP/IP 协议栈的封包和拆包 lwIP 简介lwIP 源码下载lwIP 文件说明 MAC 内核简介(STM32 内置)PHY 芯片介绍(以太网芯片 外置)YT8512C 简介LAN8720A 简介 以太网接入MCU 方案软件TCP/IP 协议栈以太网接入方案硬件TCP/IP 协议栈…

Linux:获取线程的PID(TID、LWP)的几种方式

Linux&#xff1a;获取线程的PID&#xff08;TID、LWP&#xff09;的几种方式 在 Linux C/C 中通常是通过 pthread 库进行线程级别的操作。 在 pthread 库中有函数&#xff1a; pthread_t pthread_self(void);它返回一个 pthread_t 类型的变量&#xff0c;指代的是调用 pthr…

lwip-简介

文章目录 简介层次划分传输过程 简介 lwip的介绍就不累赘了&#xff0c;网上有许多教程。这里推荐野火的<<《[野火]LwIP应用开发实战指南—基于野火STM32》—20210122.pdf>>介绍的非常详细。&#xff08;该文档我已上传到资源了&#xff09;我们记录主要是一些用法…

Linux下的LWP(轻量级进程)、进程 、 线程、用户级线程、内核线程

一、定义 再看正文之前我要先强调一下几点&#xff1a; 1. Linux中没有真正的线程&#xff0c;但windows中确实有线程 2. Linux中没有的线程是由进程来模拟实现的&#xff08;又称作&#xff1a;轻量级进程&#xff09; 3. 所以在Linux中&#xff08;在CPU角度看&#xff0…

LWIP框架

目录 协议栈分层思想 1. 网络接口层 2. 网络层 3. 传输层 4. 应用层 进程模型 单进程模型 协议栈编程接口 1、Raw/Callback API 2、Netconn API 3、Socket API 协议栈分层思想 TCP/IP协议完整的包含了一系列构成互联网基础的网络协议&#xff0c;TCP/IP协议的开发出…

lwip协议

LWIP版本号 野火LwIP应用开发实战指南&#xff1a;基于STM32 lwIP 2.1.0 Lightweight IP stack lwip的版本号在哪个文件里能看到&#xff1f; 关于LWIP几篇不错的文章分享 LwIP应用开发实战指南 LwIP多TCP连接问题 lwIP TCP/IP 协议栈笔记之十&#xff1a; LwIP 数据流框架 wir…

LWP 与 WEB 的基本使用

简介 LWP (“Library for WWW in Perl” 的缩写) 是一个由多个模块组成&#xff0c;用来获取网络数据的的模块组。 和很多 Perl 的模块一样。每一个 LWP 模块都自带详细的文档&#xff0c;做为对这个模块的完整介绍。可是面对 LWP 里的众多模块&#xff0c;有时候即使是完成最简…

LWIP协议与TCP/IP

1. 学习一个东西&#xff0c;先了解这个东西是干什么用的&#xff0c;哪些场景会用到它&#xff0c;与自己已经掌握的其他知识的联系 a. 例如&#xff1a;LWIP这个东西是干什么用的&#xff1a;他就是一个裁剪后保持大部分TCP/IP功能的协议。用少量的资源消耗实现一个较为完整的…

[Linux]线程概念_线程控制(线程与进程的区别与联系 | 线程创建 | 线程等待 | 线程终止 | 线程分离 | LWP)

文章目录 线程概念进程和线程的关系线程的优点线程的缺点 线程控制Linux线程和接口关系的认识线程创建线程ID及进程地址空间布局线程等待线程终止线程终止状态线程分离LWP和pthread_t 线程概念 线程是在进程内部运行的一个执行分支&#xff08;执行流&#xff09;&#xff0c;…

实现线程的三种方式KLT/ULT/LWP

大家好&#xff0c;我是神韵&#xff0c;是一个技术&生活博主。关于文章都是定位为基础&#xff0c;我不敢讲的太深入&#xff0c;因为我怕自己没时间。欢迎来点赞打卡&#xff0c;你们的行动将是我无限的动力。 今日主题是&#xff1a;实现线程的三种方式KLT/LWP/ULT 三种…

Linux——一文彻底了解进程id和线程id的关系(什么是pid、tgid、lwp、pthread_t)

目录 一.内核层面&#xff1a;pid & tgid 二.函数调用层面&#xff1a;getpid & gettid & pthread_self 三.用户层面&#xff1a;PID & LWP&#xff08;TID&#xff09; 四.总结 一.内核层面&#xff1a;pid & tgid 首先&#xff0c;我们要清楚&#…

LWIP简介

1介绍 lwIP 是 TCP/IP 协议套件的小型独立实现。lwIP TCP/IP 实现的重点是减少 RAM 使用同时仍然有一个完整的 TCP。这使得 lwIP 适合使用在具有数十 KB 可用 RAM 和空间的嵌入式系统中大约 40 KB 的代码 ROM。 lwIP 最初由计算机和网络的 Adam Dunkels 开发瑞典计算机科学研究…