You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
35 lines
850 B
35 lines
850 B
8 months ago
|
using System;
|
||
|
using System.Collections.Generic;
|
||
|
using System.Linq;
|
||
|
using System.Text;
|
||
|
using System.Threading.Tasks;
|
||
|
|
||
|
namespace Guidaoji
|
||
|
{
|
||
|
internal class ModbusCRC16
|
||
|
{
|
||
|
public static ushort CalculateCrc(byte[] bytes)
|
||
|
{
|
||
|
const ushort polynomial = 0xA001;
|
||
|
ushort crc = 0xFFFF;
|
||
|
for (int i = 0; i < bytes.Length; i++)
|
||
|
{
|
||
|
crc ^= (ushort)(bytes[i] & 0xFF);
|
||
|
for (int j = 0; j < 8; j++)
|
||
|
{
|
||
|
if ((crc & 0x0001) == 0x0001)
|
||
|
{
|
||
|
crc >>= 1;
|
||
|
crc ^= polynomial;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
crc >>= 1;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return crc;
|
||
|
}
|
||
|
}
|
||
|
}
|