Modbus CRC16 VB6

這是一段使用VB6撰寫的Modbus RTU CRC16副程式

Function CRC16(Message() As Byte) As Integer

    Dim I As Long, J As Long, Odd As Boolean

    CRC16 = -1

    For I = 0 To UBound(Message) - 2

        CRC16 = (CRC16 And &HFF00) Or ((CRC16 And 255) Xor Message(I))

        For J = 1 To 8

            Odd = CRC16 And 1

            CRC16 = ((CRC16 And &HFFFE) \ 2) And &H7FFF 'shift right

            If Odd Then CRC16 = &HA001 Xor CRC16



End Function

3 thoughts on “Modbus CRC16 VB6

  1. Sorry, my english is poor, it may not express very well

    the "For I = 0 To UBound(Message) - 2" is modbus receive total buffer -2
    the two byte is receive message crc16 byte
    if you want to re-calculate the checksum, you must deduct the received checksum

    subprogram can be considered after the slave receives modbus crc calculation subroutine, so will have "-2" appearance


這個網站採用 Akismet 服務減少垃圾留言。進一步瞭解 Akismet 如何處理網站訪客的留言資料