Modbus CRC校验,直接输入byte[],输出bool
public static bool CRC_Check(byte[] byteData){
bool Flag = false;byte[] CRC = new byte[2];UInt16 wCrc = 0xFFFF;for (int i = 0; i < byteData.Length-2; i++){
wCrc ^= Convert.ToUInt16(byteData[i]);for (int j = 0; j < 8; j++){
if ((wCrc & 0x0001) == 1){
wCrc >>= 1;wCrc ^= 0xA001;}else{
wCrc >>= 1;}}}CRC[1] = (byte)((wCrc & 0xFF00) >> 8);CRC[0] = (byte)(wCrc & 0x00FF); if (CRC[1] == byteData[byteData.Length - 1]&& CRC[0] == byteData[byteData.Length - 2]){
Flag = true;}return Flag;}
调用
byte[] Hex = new byte[8] {
0x01, 0x03, 0x00, 0x2A, 0x00, 0x02, 0xE5, 0xC3 };if (CRC_Check(Hex)){
//你的代码}