Private Function ShiftLeft(word As Integer) As Integer
' correr word un bit hacia la izquierda
Dim word2long As Long
word2long = 0
Call CopyMemory(word2long, word, 2)
word2long = word2long * 2
Call CopyMemory(ShiftLeft, word2long, 2)
'solo para debug
word2long = 0
Call CopyMemory(word2long, ShiftLeft, 2)
End Function
Private Function ShiftNLeft(word As Integer, n As Integer) As Integer
' correr word n bits hacia la izquierda
Dim bitNumber As Integer
Dim word2long As Long
ShiftNLeft = word
For bitNumber = 1 To n
ShiftNLeft = ShiftLeft(ShiftNLeft)
Next bitNumber
' solo para debug
word2long = 0
Call CopyMemory(word2long, ShiftNLeft, 2)
End Function
Private Function crc16(ByRef data() As Byte, lenght As Integer) As Integer
Dim byteIndex, bitIndex, tmpWord As Integer
Dim carry As Boolean
crc16 = &H0
For byteIndex = 0 To lenght - 1
tmpWord = data(byteIndex)
crc16 = crc16 Xor ShiftNLeft(tmpWord, 8) 'for each data and CRC register XOR
bitIndex = 8
Do While bitIndex > 0
carry = crc16 < 0
crc16 = ShiftLeft(crc16)
If carry Then
crc16 = crc16 Xor &H1021
End If
bitIndex = bitIndex - 1
Loop
Next byteIndex
End Function