Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.

crc-ccitt (Kermit)

[es] :: .NET :: crc-ccitt (Kermit)

[ Pregleda: 2511 | Odgovora: 1 ] > FB > Twit

Postavi temu Odgovori

Autor

Pretraga teme: Traži
Markiranje Štampanje RSS

bobby63
Nis

Član broj: 11398
Poruke: 180
*.dynamic.sbb.rs.



Profil

icon crc-ccitt (Kermit)09.02.2012. u 22:46 - pre 148 meseci
Pisem neki program za komunikaciju sa Bill Acceptorom, preko rs232 gde u komunikacionom stringu ima ima i ceksum preko CRC-CCITT (Kermit), jedne od varijacija na CRC-CCITT.
Ima li neko mozda snippet za taj "CRC-CCITT (Kermit)" u C# ili VB net.

Nasao sam neki snippet za c++ ali ne mogu posle konverzije da ga nateram da daje ispravne brojeve jer, da budem potpuno iskren, apsolutno mi nisu jasne kalkulacije u tom algoritmu za CRC.

Pozz svima i unapred hvala.
Hear all, trust nothing!
 
Odgovor na temu

bobby63
Nis

Član broj: 11398
Poruke: 180
*.dynamic.sbb.rs.



Profil

icon Re: crc-ccitt (Kermit)12.02.2012. u 18:00 - pre 148 meseci
Ako nekome zatreba converotvao sam cod iz c++ u VB net i racuna uspesno 9 vrsta crc i to
crc_16
crc_16_modbus
crc_dnp
crc_sick
crc_ccitt_0000
crc_ccitt_ffff
crc_ccitt_1d0f
crc_kermit
crc_32

Ulaz je Byte array.

Code (vbnet):
Public Class Lib_crc

    '    ******************************************************************\
    '    *                                                                   *
    '    *   unsigned short update_crc_16( unsigned short crc, char c );     *
    '    *                                                                   *
    '    *   The function update_crc_16 calculates a  new  CRC-16  value     *
    '    *   based  on  the  previous value of the CRC and the next byte     *
    '    *   of the data to be checked.                                      *
    '    *                                                                   *
    '    \******************************************************************

    '    ******************************************************************\
    '    *                                                                   *
    '    *   Library         : lib_crc                                       *
    '    *   File            : lib_crc.h                                     *
    '    *   Author          : Lammert Bies  1999-2008                       *
    '    *   E-mail          : [email protected]                           *
    '    *   Language        : ANSI C                                        *
    '    *                                                                   *
    '    *                                                                   *
    '    *   Description                                                     *
    '    *   ===========                                                     *
    '    *                                                                   *
    '    *   The file lib_crc.h contains public definitions  and  proto-     *
    '    *   types for the CRC functions present in lib_crc.c.               *
    '    *                                                                   *
    '    *                                                                   *
    '    *   Dependencies                                                    *
    '    *   ============                                                    *
    '    *                                                                   *
    '    *   none                                                            *
    '    *                                                                   *
    '    *                                                                   *
    '    *   Modification history                                            *
    '    *   ====================                                            *
    '    *                                                                   *
    '    *   Date        Version Comment                                     *
    '    *                                                                   *
    '    *   2008-04-20  1.16    Added CRC-CCITT routine for Kermit          *
    '    *                                                                   *
    '    *   2007-04-01  1.15    Added CRC16 calculation for Modbus          *
    '    *                                                                   *
    '    *   2007-03-28  1.14    Added CRC16 routine for Sick devices        *
    '    *                                                                   *
    '    *   2005-12-17  1.13    Added CRC-CCITT with initial 0x1D0F         *
    '    *                                                                   *
    '    *   2005-02-14  1.12    Added CRC-CCITT with initial 0x0000         *
    '    *                                                                   *
    '    *   2005-02-05  1.11    Fixed bug in CRC-DNP routine                *
    '    *                                                                   *
    '    *   2005-02-04  1.10    Added CRC-DNP routines                      *
    '    *                                                                   *
    '    *   2005-01-07  1.02    Changes in tst_crc.c                        *
    '    *                                                                   *
    '    *   1999-02-21  1.01    Added FALSE and TRUE mnemonics              *
    '    *                                                                   *
    '    *   1999-01-22  1.00    Initial source                              *
    '    *                                                                   *
    '    \******************************************************************



#Const CRC_VERSION = True



    '#Const _FALSE = True
    '#Const _TRUE = True



    Public Shared Function update_crc_16(ByVal crc As UShort, ByVal c As Byte) As UShort

        Dim tmp As UShort
        Dim short_c As UShort

        short_c = &HFF And CUShort(c)

        If crc_tab16_init = 0 Then
            init_crc16_tab()
        End If

        tmp = crc Xor short_c
        crc = (crc >> 8) Xor crc_tab16(tmp And &HFF)

        Return crc

    End Function ' update_crc_16

    '    ******************************************************************\
    '    *                                                                   *
    '    *   unsigned long update_crc_32( unsigned long crc, char c );       *
    '    *                                                                   *
    '    *   The function update_crc_32 calculates a  new  CRC-32  value     *
    '    *   based  on  the  previous value of the CRC and the next byte     *
    '    *   of the data to be checked.                                      *
    '    *                                                                   *
    '    \******************************************************************

    Public Shared Function update_crc_32(ByVal crc As UInteger, ByVal c As Byte) As UInteger

        Dim tmp As UInteger
        Dim long_c As UInteger

        long_c = &HFFL And CUInt(c)

        If crc_tab32_init = 0 Then
            init_crc32_tab()
        End If

        tmp = crc Xor long_c
        crc = (crc >> 8) Xor crc_tab32(tmp And &HFF)

        Return crc

    End Function ' update_crc_32

    '    ******************************************************************\
    '    *                                                                   *
    '    *   unsigned short update_crc_ccitt( unsigned long crc, char c );   *
    '    *                                                                   *
    '    *   The function update_crc_ccitt calculates  a  new  CRC-CCITT     *
    '    *   value  based  on the previous value of the CRC and the next     *
    '    *   byte of the data to be checked.                                 *
    '    *                                                                   *
    '    \******************************************************************

    Public Shared Function update_crc_ccitt(ByVal crc As UShort, ByVal c As Byte) As UShort

        Dim tmp As UShort
        Dim short_c As UShort

        short_c = &HFF And CUShort(c)

        If crc_tabccitt_init = 0 Then
            init_crcccitt_tab()
        End If

        tmp = (crc >> 8) Xor short_c
        crc = (crc << 8) Xor crc_tabccitt(tmp)

        Return crc

    End Function ' update_crc_ccitt

    '    ******************************************************************\
    '    *                                                                   *
    '    *   unsigned short update_crc_dnp( unsigned short crc, char c );    *
    '    *                                                                   *
    '    *   The function update_crc_dnp calculates a new CRC-DNP  value     *
    '    *   based  on  the  previous value of the CRC and the next byte     *
    '    *   of the data to be checked.                                      *
    '    *                                                                   *
    '    \******************************************************************

    Public Shared Function update_crc_dnp(ByVal crc As UShort, ByVal c As Byte) As UShort

        Dim tmp As UShort
        Dim short_c As UShort

        short_c = &HFF And CUShort(c)

        If crc_tabdnp_init = 0 Then
            init_crcdnp_tab()
        End If

        tmp = crc Xor short_c
        crc = (crc >> 8) Xor crc_tabdnp(tmp And &HFF)

        Return crc

    End Function ' update_crc_dnp

    '    ******************************************************************\
    '    *                                                                   *
    '    *   unsigned short update_crc_kermit( unsigned short crc, char c ); *
    '    *                                                                   *
    '    *   The function update_crc_kermit calculates a  new  CRC value     *
    '    *   based  on  the  previous value of the CRC and the next byte     *
    '    *   of the data to be checked.                                      *
    '    *                                                                   *
    '    \******************************************************************

    Public Shared Function update_crc_kermit(ByVal crc As UShort, ByVal c As Byte) As UShort

        Dim tmp As UShort
        Dim short_c As UShort

        short_c = &HFF And CUShort(c)

        If crc_tabkermit_init = 0 Then
            init_crckermit_tab()
        End If

        tmp = crc Xor short_c
        crc = (crc >> 8) Xor crc_tabkermit(tmp And &HFF)

        Return crc

    End Function ' update_crc_kermit

    '    ******************************************************************\
    '    *                                                                   *
    '    *   unsigned short update_crc_sick(                                 *
    '    *             unsigned long crc, char c, char prev_byte );          *
    '    *                                                                   *
    '    *   The function  update_crc_sick  calculates  a  new  CRC-SICK     *
    '    *   value  based  on the previous value of the CRC and the next     *
    '    *   byte of the data to be checked.                                 *
    '    *                                                                   *
    '    \******************************************************************

    Public Shared Function update_crc_sick(ByVal crc As UShort, ByVal c As Byte, ByVal prev_byte As Byte) As UShort

        Dim short_c As UShort
        Dim short_p As UShort

        short_c = &HFF And CUShort(c)
        short_p = (&HFF And CUShort(prev_byte)) << 8

        If crc And &H8000 Then
            crc = (crc << 1) Xor DefineConstantsLib_crc.P_SICK
        Else
            crc = crc << 1
        End If

        crc = crc And &HFFFF
        crc = crc Xor (short_c Or short_p)

        Return crc

    End Function ' update_crc_sick




    '    ******************************************************************\
    '    *                                                                   *
    '    *   Library         : lib_crc                                       *
    '    *   File            : lib_crc.c                                     *
    '    *   Author          : Lammert Bies  1999-2008                       *
    '    *   E-mail          : [email protected]                           *
    '    *   Language        : ANSI C                                        *
    '    *                                                                   *
    '    *                                                                   *
    '    *   Description                                                     *
    '    *   ===========                                                     *
    '    *                                                                   *
    '    *   The file lib_crc.c contains the private  and  public  func-     *
    '    *   tions  used  for  the  calculation of CRC-16, CRC-CCITT and     *
    '    *   CRC-32 cyclic redundancy values.                                *
    '    *                                                                   *
    '    *                                                                   *
    '    *   Dependencies                                                    *
    '    *   ============                                                    *
    '    *                                                                   *
    '    *   lib_crc.h       CRC definitions and prototypes                  *
    '    *                                                                   *
    '    *                                                                   *
    '    *   Modification history                                            *
    '    *   ====================                                            *
    '    *                                                                   *
    '    *   Date        Version Comment                                     *
    '    *                                                                   *
    '    *   2008-04-20  1.16    Added CRC-CCITT calculation for Kermit      *
    '    *                                                                   *
    '    *   2007-04-01  1.15    Added CRC16 calculation for Modbus          *
    '    *                                                                   *
    '    *   2007-03-28  1.14    Added CRC16 routine for Sick devices        *
    '    *                                                                   *
    '    *   2005-12-17  1.13    Added CRC-CCITT with initial 0x1D0F         *
    '    *                                                                   *
    '    *   2005-05-14  1.12    Added CRC-CCITT with start value 0          *
    '    *                                                                   *
    '    *   2005-02-05  1.11    Fixed bug in CRC-DNP routine                *
    '    *                                                                   *
    '    *   2005-02-04  1.10    Added CRC-DNP routines                      *
    '    *                                                                   *
    '    *   1999-02-21  1.01    Added FALSE and TRUE mnemonics              *
    '    *                                                                   *
    '    *   1999-01-22  1.00    Initial source                              *
    '    *                                                                   *
    '    \******************************************************************



    '    ******************************************************************\
    '    *                                                                   *
    '    *   #define P_xxxx                                                  *
    '    *                                                                   *
    '    *   The CRC's are computed using polynomials. The  coefficients     *
    '    *   for the algorithms are defined by the following constants.      *
    '    *                                                                   *
    '    \******************************************************************

#Const P_16 = True
#Const P_32 = True
#Const P_CCITT = True
#Const P_DNP = True
#Const P_KERMIT = True
#Const P_SICK = True



    '    ******************************************************************\
    '    *                                                                   *
    '    *   static int crc_tab...init                                       *
    '    *   static unsigned ... crc_tab...[]                                *
    '    *                                                                   *
    '    *   The algorithms use tables with precalculated  values.  This     *
    '    *   speeds  up  the calculation dramaticaly. The first time the     *
    '    *   CRC function is called, the table for that specific  calcu-     *
    '    *   lation  is set up. The ...init variables are used to deter-     *
    '    *   mine if the initialization has taken place. The  calculated     *
    '    *   values are stored in the crc_tab... arrays.                     *
    '    *                                                                   *
    '    *   The variables are declared static. This makes them  invisi-     *
    '    *   ble for other modules of the program.                           *
    '    *                                                                   *
    '    \******************************************************************

    Friend Shared crc_tab16_init As Integer = DefineConstantsLib_crc._FALSE
    Friend Shared crc_tab32_init As Integer = DefineConstantsLib_crc._FALSE
    Friend Shared crc_tabccitt_init As Integer = DefineConstantsLib_crc._FALSE
    Friend Shared crc_tabdnp_init As Integer = DefineConstantsLib_crc._FALSE
    Friend Shared crc_tabkermit_init As Integer = DefineConstantsLib_crc._FALSE

    Friend Shared crc_tab16(255) As UShort
    Friend Shared crc_tab32(255) As UInteger
    Friend Shared crc_tabccitt(255) As UShort
    Friend Shared crc_tabdnp(255) As UShort
    Friend Shared crc_tabkermit(255) As UShort

    '    ******************************************************************\
    '    *                                                                   *
    '    *   static void init_crc16_tab( void );                             *
    '    *                                                                   *
    '    *   The function init_crc16_tab() is used  to  fill  the  array     *
    '    *   for calculation of the CRC-16 with values.                      *
    '    *                                                                   *
    '    \******************************************************************




    '    ******************************************************************\
    '    *                                                                   *
    '    *   static void init_crc...tab();                                   *
    '    *                                                                   *
    '    *   Three local functions are used  to  initialize  the  tables     *
    '    *   with values for the algorithm.                                  *
    '    *                                                                   *
    '    \******************************************************************

    Friend Shared Sub init_crc16_tab()

        Dim i As Integer
        Dim j As Integer
        Dim crc As UShort
        Dim c As UShort

        For i = 0 To 255

            crc = 0
            c = CUShort(i)

            For j = 0 To 7

                If (crc Xor c) And &H1 Then
                    crc = (crc >> 1) Xor DefineConstantsLib_crc.P_16
                Else
                    crc = crc >> 1
                End If

                c = c >> 1
            Next j

            crc_tab16(i) = crc
        Next i

        crc_tab16_init = DefineConstantsLib_crc._TRUE

    End Sub ' init_crc16_tab

    '    ******************************************************************\
    '    *                                                                   *
    '    *   static void init_crc32_tab( void );                             *
    '    *                                                                   *
    '    *   The function init_crc32_tab() is used  to  fill  the  array     *
    '    *   for calculation of the CRC-32 with values.                      *
    '    *                                                                   *
    '    \******************************************************************

    Friend Shared Sub init_crc32_tab()

        Dim i As Integer
        Dim j As Integer
        Dim crc As UInteger

        For i = 0 To 255

            crc = CUInt(i)

            For j = 0 To 7

                If crc And &H1L Then
                    crc = (crc >> 1) Xor DefineConstantsLib_crc.P_32
                Else
                    crc = crc >> 1
                End If
            Next j

            crc_tab32(i) = crc
        Next i

        crc_tab32_init = DefineConstantsLib_crc._TRUE

    End Sub ' init_crc32_tab

    '    ******************************************************************\
    '    *                                                                   *
    '    *   static void init_crcccitt_tab( void );                          *
    '    *                                                                   *
    '    *   The function init_crcccitt_tab() is used to fill the  array     *
    '    *   for calculation of the CRC-CCITT with values.                   *
    '    *                                                                   *
    '    \******************************************************************

    Friend Shared Sub init_crcccitt_tab()

        Dim i As Integer
        Dim j As Integer
        Dim crc As UShort
        Dim c As UShort

        For i = 0 To 255

            crc = 0
            c = (CUShort(i)) << 8

            For j = 0 To 7

                If (crc Xor c) And &H8000 Then
                    crc = (crc << 1) Xor DefineConstantsLib_crc.P_CCITT
                Else
                    crc = crc << 1
                End If

                c = c << 1
            Next j

            crc_tabccitt(i) = crc
        Next i

        crc_tabccitt_init = DefineConstantsLib_crc._TRUE

    End Sub ' init_crcccitt_tab

    '    ******************************************************************\
    '    *                                                                   *
    '    *   static void init_crcdnp_tab( void );                            *
    '    *                                                                   *
    '    *   The function init_crcdnp_tab() is used  to  fill  the  array    *
    '    *   for calculation of the CRC-DNP with values.                     *
    '    *                                                                   *
    '    \******************************************************************

    Friend Shared Sub init_crcdnp_tab()

        Dim i As Integer
        Dim j As Integer
        Dim crc As UShort
        Dim c As UShort

        For i = 0 To 255

            crc = 0
            c = CUShort(i)

            For j = 0 To 7

                If (crc Xor c) And &H1 Then
                    crc = (crc >> 1) Xor DefineConstantsLib_crc.P_DNP
                Else
                    crc = crc >> 1
                End If

                c = c >> 1
            Next j

            crc_tabdnp(i) = crc
        Next i

        crc_tabdnp_init = DefineConstantsLib_crc._TRUE

    End Sub ' init_crcdnp_tab

    '    ******************************************************************\
    '    *                                                                   *
    '    *   static void init_crckermit_tab( void );                         *
    '    *                                                                   *
    '    *   The function init_crckermit_tab() is used to fill the array     *
    '    *   for calculation of the CRC Kermit with values.                  *
    '    *                                                                   *
    '    \******************************************************************

    Friend Shared Sub init_crckermit_tab()

        Dim i As Integer
        Dim j As Integer
        Dim crc As UShort
        Dim c As UShort

        For i = 0 To 255

            crc = 0
            c = CUShort(i)

            For j = 0 To 7

                If (crc Xor c) And &H1 Then
                    crc = (crc >> 1) Xor DefineConstantsLib_crc.P_KERMIT
                Else
                    crc = crc >> 1
                End If

                c = c >> 1
            Next j

            crc_tabkermit(i) = crc
        Next i

        crc_tabkermit_init = DefineConstantsLib_crc._TRUE

    End Sub ' init_crckermit_tab

    Public Function ComputeChecksumBytes(ByVal crc_Input As Byte()) As Byte()
        Dim input_string As String '(New Char(DefineConstantsTst_crc.MAX_STRING_SIZE - 1) {})
        Dim ptr As String
        'Dim dest As String
        Dim hex_val As Byte
        Dim prev_byte As Byte
        Dim crc_16 As UShort
        Dim crc_16_modbus As UShort
        Dim crc_ccitt_ffff As UShort
        Dim crc_ccitt_0000 As UShort
        Dim crc_ccitt_1d0f As UShort
        Dim crc_dnp As UShort
        Dim crc_sick As UShort
        Dim crc_kermit As UShort
        'Dim low_byte As UShort
        'Dim high_byte As UShort
        Dim crc_32 As UInteger
        'Dim a As Integer
        'Dim ch As Integer
        'Dim do_ascii As Integer
        'Dim do_hex As Integer
        'Dim fp As FILE

        prev_byte = 0
        ptr = input_string

        'Do While ptr <> ChrW(&H80)
        Dim x As Integer
        For x = 0 To crc_Input.Length - 1
            'crc_16 = update_crc_16(crc_16, crc_Input(x))
            'crc_16_modbus = update_crc_16(crc_16_modbus, crc_Input(x))
            'crc_dnp = update_crc_dnp(crc_dnp, crc_Input(x))
            'crc_sick = update_crc_sick(crc_sick, crc_Input(x), prev_byte)
            'crc_ccitt_0000 = update_crc_ccitt(crc_ccitt_0000, crc_Input(x))
            'crc_ccitt_ffff = update_crc_ccitt(crc_ccitt_ffff, crc_Input(x))
            'crc_ccitt_1d0f = update_crc_ccitt(crc_ccitt_1d0f, crc_Input(x))
            crc_kermit = update_crc_kermit(crc_kermit, crc_Input(x))
            'crc_32 = update_crc_32(crc_32, crc_Input(x))

            prev_byte = crc_Input(x)
        Next
        'Loop

        input_string = Nothing

        Return New Byte() {CByte((crc_kermit And &HFF)), CByte((crc_kermit >> 8))}      ' To return Byte Array
        'Return crc_kermit                                                              'To return number
    End Function
End Class


Friend NotInheritable Class DefineConstantsLib_crc
    Public Const CRC_VERSION As String = "1.16"
    Public Const _FALSE As Integer = 0
    Public Const _TRUE As Integer = 1
    Public Const P_16 As Integer = &HA001
    Public Const P_32 As Long = &HEDB88320L
    Public Const P_CCITT As Integer = &H1021
    Public Const P_DNP As Integer = &HA6BC
    Public Const P_KERMIT As Integer = &H8408
    Public Const P_SICK As Integer = &H8005
End Class
 


[Ovu poruku je menjao mmix dana 13.02.2012. u 10:04 GMT+1]
Hear all, trust nothing!
 
Odgovor na temu

[es] :: .NET :: crc-ccitt (Kermit)

[ Pregleda: 2511 | Odgovora: 1 ] > FB > Twit

Postavi temu Odgovori

Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.