Sub CalcModbusCRC(Bytes() As Byte) As String
Dim CRCr As Int = 0xFFFF
For i = 0 To Bytes.Length - 1
CRCr = Bit.Xor(CRCr, Bit.And(Bytes(i), 0xFF))
For j = 0 To 7
If (Bit.And(CRCr, 1) = 1) Then
CRCr = Bit.Xor(Bit.ShiftRight(CRCr, 1), 0xA001)
Else
CRCr = Bit.ShiftRight(CRCr, 1)
End If
Next
Next
'Low byte first (Modbus standard)
Dim low As Int = Bit.And(CRCr, 0xFF)
Dim high As Int = Bit.And(Bit.ShiftRight(CRCr, 8), 0xFF)
Return Bit.ToHexString(high).ToUpperCase & Bit.ToHexString(low).ToUpperCase
End Sub