Sub CRC16Calc(data() As Byte) As Short
'Public short CRC16Calc(uint8_t *data, short size)
'#define CRC16 0x8005 //Polinomio
Dim CRC16 As Short = 0x8005
'uint16_t out = 0;
Dim out As Short
'uint16_t bits_read = 0, bit_flag;
Dim bits_read As Short = 0
Dim bit_flag As Short
'uint16_t idx = 0;
Dim idx As Short = 0
Dim size As Short = data.Length
Do While size > 0
'bit_flag = out >> 15;
bit_flag = Bit.ShiftRight(out, 15)
Log("first while")
'out <<= 1;
'out |= (data[idx] >> bits_read) & 1;
out = Bit.ShiftLeft(out, 1)
out = Bit.Or(out, Bit.And(Bit.ShiftRight(data(idx), bits_read), 1))
bits_read=bits_read+1
'If (bits_read > 7)
If(Bit.ShiftRight(bits_read, 7) <> 0) Then
bits_read = 0
idx=idx+1
size=size-1
Log("*********************** first if")
End If
'If (bit_flag) out ^= CRC16;
If(bit_flag > 0) Then out = Bit.Xor(out, CRC16)
Loop
'uint16_t i;
Dim i As Short
'For (i = 0; i < 16; ++i)
For i = 0 To 16
Log("first for")
'bit_flag = out >> 15;
bit_flag = Bit.ShiftRight(out, 15)
'out <<= 1;
out = Bit.ShiftLeft(out, 1)
'If (bit_flag) out ^= CRC16;
If(bit_flag <> 0) Then out = Bit.Xor(out, CRC16)
Next
'uint16_t crc = 0;
Dim crc As Short = 0
i = 0x8000
'uint16_t j = 0x0001;
Dim j As Short = 0x0001
'For (; i != 0; i >>= 1, j <<= 1)
Do While i <> 0
'Log("second while")
'If (i & out) crc |= j;
If(Bit.And(i, out) <> 0) Then crc = Bit.Or(crc, j)
i = Bit.ShiftRight(i, 1)
j = Bit.ShiftLeft(j, 1)
' Dim bc As String
' bc = i
' bc = bc & " " & j
' Log(bc)
Loop
Return crc
End Sub