Function CRC16_BIN(ByRef ModBus_Data() As Long, NumBytes As Integer) As Long
  
    Dim Temp As Long
    Dim CRC As Long
    Dim Polynomial As Long
    Dim i As Integer
    Dim j As Integer
'—– following are read sequence checksum verification vectors  — test where either hi or lo byte == 0
'— Example temperature= 1 :   1  3  64  0  1  217  144
'—         temperature=255:   1  3  64  0  255  88  16
'—         temperature=256:   1  3  64  1  0  25  192
'—         temperature=257:   1  3  64  1  1  216  0
'—                     =0 :   1  3  64  0  0  24  80
  '— verified write string = Chr(1) & Chr(6) & Chr(64) & Chr(3) & Chr(232) & Chr(24) & Chr(34)
    CRC = 65535          '–bottom 16 bits are all 1's
    Polynomial = 40961   '–poly = A001
    For i = 0 To NumBytes – 1
      CRC = CRC Xor ModBus_Data(i)
      For j = 0 To 7
        If (CRC And 1) Then
          CRC = (ShiftRight(CRC) Xor 40961)
        Else
          CRC = ShiftRight(CRC)
        End If
      Next j
    Next i
    CRC16_BIN = CRC And 65535
   
    End Function

 

Research Links

Categories: CodingVisual-Basic

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *