java modbus-crc
时间: 2023-07-05 22:10:58 浏览: 55
Java Modbus CRC (Cyclic Redundancy Check) is a method used to detect errors in data transmission. The Modbus protocol is a common communication protocol used in industrial automation systems. To calculate the CRC for Modbus, you can use the following Java code:
```java
public static int calculateCRC(byte[] data) {
int crc = 0xFFFF;
for (int i = 0; i < data.length; i++) {
crc ^= (int) data[i] & 0xFF;
for (int j = 0; j < 8; j++) {
if ((crc & 0x0001) != 0) {
crc >>= 1;
crc ^= 0xA001;
} else {
crc >>= 1;
}
}
}
return crc;
}
```
This code takes an array of bytes as input and returns the CRC value as an integer. You can use this method to calculate the CRC for Modbus messages in your Java application.