You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace MaterialTraceability.Common
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 校验工具类
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class CheckUtil
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 异或校验
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="data"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static int BCC(byte[] data)
|
|
|
|
|
{
|
|
|
|
|
int temp = 0;
|
|
|
|
|
for (int index = 0; index < data.Length; index++)
|
|
|
|
|
{
|
|
|
|
|
temp = temp ^ data[index];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return temp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// CRC16_Modbus效验
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="byteData">要进行计算的字节数组</param>
|
|
|
|
|
/// <returns>计算后的数组</returns>
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|