默认 Modbus 校验,小端false
#region 数组计算CRC
public byte[] getCRC16(byte[] bytes, bool b)//数组,端向(小端false)
{
int len = bytes.Length;
if (len > 0)
{
ushort crc = 0xFFFF;
for (int i = 0; i < len; i++)
{
crc = (ushort)(crc ^ (bytes[i]));
for (int j = 0; j < 8; j++)
{
crc = (crc & 1) != 0 ? (ushort)((crc >> 1) ^ 0xA001) : (ushort)(crc >> 1);
}
}
byte hi = (byte)((crc & 0xFF00) >> 8); //高位置
byte lo = (byte)(crc & 0x00FF); //低位置
if (b)//端向
{
return new byte[] { hi, lo };//大端
}
else
{//默认小端
return new byte[] { lo, hi, };//小端
}
}
return new byte[] { 0, 0 };
}
#endregion