public byte[] GetCRC16(string content)
{
byte[] bytes = Encoding.UTF8.GetBytes(content);
byte[] crc16 = CRC16(bytes);
return crc16;
}
public byte[] CRC16(byte[] bytes)
{
int length = bytes.Length;
if (length > 0)
{
ushort crc = 0xFFFF;
for (int i = 0; i < length; i++)
{
crc = (ushort)(crc ^ bytes[i]);
for (int j = 0; j < 8; j++)
{
if ((crc & 1) != 0)
{
crc = (ushort)((crc >> 1) ^ 0xA001);
}
else
{
crc = (ushort)(crc >> 1);
}
}
}
byte high_order = (byte)((crc & 0xFF00) >> 8);
byte Low_order = (byte)(crc & 0x00FF);
return new byte[] { high_order, Low_order };
}
return new byte[] { 0x00, 0x00 };
}
C# CRC16 MODBUS 校验
最新推荐文章于 2024-07-16 21:10:00 发布