Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

Calc CRC-CCITT (XModem)

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Calc CRC-CCITT (XModem)

    Dear all,

    Maybe someone might find this useful:
    Code:
    ' ************************************
    ' * Original code in C, by Mr L.Bies *
    ' * Ported to PB9 by Mr J.A.du Toit  *
    ' ****************************************************************************************
    ' * + Usefull Links:                                                                     *
    ' *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
    ' *   http://www.lammertbies.nl/comm/software/index.html                                 *
    ' *   http://www.lammertbies.nl/comm/info/crc-calculation.html?crc=000b001600&method=hex *
    ' ****************************************************************************************
    #COMPILE EXE
    #DIM ALL
    
    %TRUE = -1
    
    GLOBAL crc_tabccitt() AS WORD
    GLOBAL crc_tabccitt_init AS LONG
    
    SUB init_crcccitt_tab()
     LOCAL i AS WORD
     LOCAL j AS WORD
     LOCAL crc AS WORD
     LOCAL c AS WORD
     LOCAL P_CCITT AS WORD
      P_CCITT = &h1021
      REDIM crc_tabccitt(0 TO 255)
      
      FOR i = 0 TO 255
        crc = 0
        c = i : SHIFT LEFT c, 8
        FOR j = 0 TO 7
          IF ISTRUE((crc XOR c) AND &h8000) THEN
            SHIFT LEFT crc, 1
            crc = crc XOR P_CCITT
          ELSE
            SHIFT LEFT crc, 1
          END IF
    
          SHIFT LEFT c, 1
        NEXT
        crc_tabccitt(i) = crc
      NEXT
      crc_tabccitt_init = %TRUE
    END SUB
    
    FUNCTION update_crc_ccitt(BYVAL crc AS WORD, BYVAL c AS BYTE) AS WORD
     LOCAL tmp AS WORD
     LOCAL short_c AS WORD
      short_c = &h00FF AND c
      IF ISFALSE(crc_tabccitt_init) THEN init_crcccitt_tab
      tmp = crc: SHIFT RIGHT tmp, 8
      tmp = tmp XOR short_c
      SHIFT LEFT crc, 8
      crc = crc XOR crc_tabccitt(tmp)
      FUNCTION = crc
    END FUNCTION
      
    FUNCTION PBMAIN () AS LONG
     DIM A() AS BYTE
     DIM B() AS BYTE
     LOCAL i AS LONG
     LOCAL crc_ccitt_0000 AS WORD
    
      REDIM A(0 TO 4)
      ARRAY ASSIGN A() = &h00, &h0b, &h00, &h16, &h00
    
      crc_ccitt_0000 = 0
      FOR i = 0 TO 4
        crc_ccitt_0000 = update_crc_ccitt(crc_ccitt_0000, A(i))
      NEXT
      'crc_ccitt_0000 = MAK(WORD, HI(BYTE, crc_ccitt_0000), LO(BYTE, crc_ccitt_0000))
      MSGBOX HEX$(crc_ccitt_0000, 4) + "  expect B7 CA"
    
    
      REDIM B(0 TO 8)
      ARRAY ASSIGN B() = &h31, &h32, &h33, &h34, &h35, &h36, &h37, &h38, &h39 ' "123456789"
    
      crc_ccitt_0000 = 0
      FOR i = 0 TO 8
        crc_ccitt_0000 = update_crc_ccitt(crc_ccitt_0000, B(i))
      NEXT
      MSGBOX HEX$(crc_ccitt_0000, 4) + "  expect 31 C3"
    END FUNCTION
    JADT
Working...
X