using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MaterialTraceability.Common
{
///
/// 校验工具类
///
public class CheckUtil
{
///
/// 异或校验
///
///
///
public static int BCC(byte[] data)
{
int temp = 0;
for (int index = 0; index < data.Length; index++)
{
temp = temp ^ data[index];
}
return temp;
}
///
/// CRC16_Modbus效验
///
/// 要进行计算的字节数组
/// 计算后的数组
public static byte[] ToModbus(byte[] byteData)
{
byte[] CRC = new byte[2];
UInt16 wCrc = 0xFFFF;
for (int i = 0; i < byteData.Length; 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); //低位在前
return CRC;
}
}
}