Announcement

Collapse
No announcement yet.

Need Help to convet C to PB

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

  • Need Help to convet C to PB

    Hello all. I'm not a C coder at all. This is a short C routine that
    Calculates a CRC16. The Poly and Seed are correct for what I need.
    Could someone help me out converting this to PB. Thanks

    Code:
    word get_crc(ubyte *bufptr,word len)
    {
    register unsigned int
    newCRC;
    //Put the current byte in here.
    ubyte
    data;
    int
    bit_count;
    //This seed makes the output of this shift based algorithm match
    //the table based algorithm. The center 16 bits of the 32-bit
    //"newCRC" are used for the CRC. The MSb of the lower byte is used
    //to see what bit was shifted out of the center 16 bit CRC
    //accumulator ("carry flag analog");
    newCRC=0x00F32100;
    while(len--)
    {
    //Get the next byte in the stream.
    data=*bufptr++;
    //Push this byte’s bits through a software
    //implementation of a hardware shift & xor.
    for(bit_count=0;bit_count<=7;bit_count++)
    {
    //Shift the CRC accumulator
    newCRC>>=1;
    //The new MSB of the CRC accumulator comes
    //from the LSB of the current data byte.
    if(data&0x01)
    newCRC|=0x00800000;
    //If the low bit of the current CRC accumulator was set
    //before the shift, then we need to XOR the accumulator
    //with the polynomial (center 16 bits of 0x00840800)
    if(newCRC&0x00000080)
    newCRC^=0x00840800;
    //Shift the data byte to put the next bit of the stream
    //into position 0.
    data>>=1;
    }
    }
    //All the data has been done. Do 16 more bits of 0 data.
    for(bit_count=0;bit_count<=15;bit_count++)
    {
    //Shift the CRC accumulator
    newCRC>>=1;
    
    //If the low bit of the current CRC accumulator was set
    //before the shift we need to XOR the accumulator with
    //0x00840800.
    if(newCRC&0x00000080)
    newCRC^=0x00840800;
    }
    //Return the center 16 bits, making this CRC match the one’s
    //complement that is sent in the packet.
    return((~newCRC)>>8);
    }
    ------------------
    Doug McDonald
    KD5NWK
    www.redforksoftware.com

  • #2
    This might be of help - PB code

    HASH ALGORITHMS Advanced
    RIPEMD-160
    MD5
    SHA-1
    SHA-256

    HASH ALGORITHMS Simple
    CRC-16
    CRC-16 (DOS)
    CRC-32
    Adler-32
    FNV-32
    ELF-32
    http://www.pbcrypto.com/pbcrypto.php


    ------------------

    Comment


    • #3
      Thanks Stan. I can do it using a look up table like the example
      in the CRC16 hash from the site you suggested. I really need to
      do it using a bit shift as in the C code I posted.

      I know that using a table is faster (so I've been told) but I
      need to use the bit shift method.

      Thanks again




      ------------------
      Doug McDonald
      KD5NWK
      www.redforksoftware.com

      Comment


      • #4
        Doug,

        I don't do C/C++ usually, PB is far more fun, so maybe has a hole or two some C/C++ PB'er can plug.

        If you have known results to check against ... probably can checkout and nab any perpetrators here ...
        Code:
        #COMPILE EXE
        #DIM ALL
        
        FUNCTION GetCRC (BYVAL pBuff AS BYTE PTR,BYVAL buflen AS WORD) AS WORD
            REGISTER newCRC AS WORD
            LOCAL databyte  AS BYTE
            LOCAL Bit_Count AS LONG
        
            newCRC = &H00F32100
        
            WHILE buflen > 0
                databyte = @pBuff
                FOR Bit_Count = 0 TO 7
                    SHIFT RIGHT newCRC, 1
                    IF (databyte AND &H01) THEN
                        newCRC = (newCRC OR &H00800000)
                        IF (newCRC AND &H00000080) THEN
                            newCRC = (newCRC XOR &H00840800)
                        END IF
                    END IF
                    SHIFT RIGHT databyte, 1
                NEXT Bit_Count
                DECR buflen
                INCR pBuff
            WEND
            '
            FOR Bit_Count =  0 TO 15
                SHIFT RIGHT newCRC, 1
                IF (newCRC AND &H00000080) THEN
                    newCRC =  (newCRC XOR &H00840800)
                END IF
            NEXT Bit_Count
        
            newCRC = NOT newCRC    '1's compliment
            SHIFT RIGHT newCRC, 8  'shift right by 8 bits
            FUNCTION = newCRC      'retunn value
            
        END FUNCTION
        
        
        FUNCTION PBMAIN () AS LONG
        '    The correct CRC is 57102 or 0xDF0E
        
            LOCAL sd$
            LOCAL pSByte AS BYTE PTR
            LOCAL crc AS WORD
        
            sd$= CHR$(31) & CHR$(9) & CHR$(0) & CHR$(0) & "Hello"
            pSByte = STRPTR(sd$)
            
        
            crc = GetCRC(BYVAL pSByte, BYVAL LEN(sd$))
            
            ? HEX$(crc)
            ?
            ? ".... press any key to quit ..."
            
            WAITKEY$
        
        END FUNCTION
        note: revised code for test, still checking though



        [This message has been edited by Richard Angell (edited January 07, 2006).]
        Rick Angell

        Comment


        • #5
          I know that using a table is faster (so I've been told) but I need to use the bit shift method.
          Pray, why? <U>How</U> cannot possibly matter.. unless this is homework..

          Michael Mattias
          Tal Systems (retired)
          Port Washington WI USA
          [email protected]
          http://www.talsystems.com

          Comment


          • #6
            Pray, why? How cannot possibly matter.. unless this is homework..

            Normaly it wouldn't matter.

            But it matters for this reason..This is going to be used in a PIC MCU
            so I don't have room for a big table. I'm using Mbasic (for pic's).
            Basicly I want to emulate / debug the CRC code in powerbasic so I
            don't have to reflash the pic every time I make a code change.

            If all else fails I'll just do it in ASM


            ------------------


            [This message has been edited by Doug McDonald (edited January 07, 2006).]
            Doug McDonald
            KD5NWK
            www.redforksoftware.com

            Comment


            • #7
              Rick.

              Thank you very much for your help. I'm working on it.

              I noticed you didn't include newCRC=&h00F32100 as a crc seed,
              I added it but it didn't make a difference.

              This is the data that needs the CRC:

              strData = CHR$(31) & CHR$(9) & CHR$(0) & CHR$(0) & "Hello"
              Data_len = 9

              The cottect CRC is 57102 or 0xDF0E

              Other than that it looks correct to me. The C code is from the data sheet
              from a serial LCD. I'd assume it's correct but as in lots of data
              sheets out there it may not be.

              Thanks again.

              ------------------


              [This message has been edited by Doug McDonald (edited January 07, 2006).]
              Doug McDonald
              KD5NWK
              www.redforksoftware.com

              Comment


              • #8
                >This is going to be used in a PIC MCU.. emulate / debug the CRC code in powerbasic

                Damn. I was going to guess that next.

                Michael Mattias
                Tal Systems (retired)
                Port Washington WI USA
                [email protected]
                http://www.talsystems.com

                Comment


                • #9
                  Well Mike your a good guesser, congrats

                  ------------------
                  Doug McDonald
                  KD5NWK
                  www.redforksoftware.com

                  Comment


                  • #10
                    Doug,

                    Revised code so will run, still needs checking, something is still not kosher. the return is FF at the moment, but maybe some other thing overlooked or entered incorrectly. Gotta run, be playing with it later though.

                    note> BitCOunt loop is now 0 to 15

                    [This message has been edited by Richard Angell (edited January 07, 2006).]
                    Rick Angell

                    Comment


                    • #11
                      Thanks again... I changed newcrc from a word to dword and I get
                      a crc of 62026 instead of 255. I'll post the Table version of the
                      program soon so youll have something to check against.

                      ------------------
                      Doug McDonald
                      KD5NWK
                      www.redforksoftware.com

                      Comment


                      • #12
                        Richard, I added the "table Code" . When you run this app there
                        are two choices, the "Bit Shift" button (C code converted to PB)
                        and a "Table" button that runs some quickly converted VB6 code
                        and gives the correct CRC

                        Thank you


                        Code:
                        #PBFORMS CREATED V1.50
                        
                        #COMPILE EXE
                        #DIM ALL
                        
                        '------------------------------------------------------------------------------
                        '   ** Includes **
                        '------------------------------------------------------------------------------
                        #PBFORMS BEGIN INCLUDES
                        #IF NOT %DEF(%WINAPI)
                            #INCLUDE "WIN32API.INC"
                        #ENDIF
                        #PBFORMS END INCLUDES
                        '------------------------------------------------------------------------------
                        
                        '------------------------------------------------------------------------------
                        '   ** Constants **
                        '------------------------------------------------------------------------------
                        #PBFORMS BEGIN CONSTANTS
                        %IDD_DIALOG1 =  101
                        %IDC_txt1    = 1001
                        %IDC_BUTTON1 = 1002
                        %IDC_BUTTON2 = 1004
                        %IDC_txt2    = 1003
                        #PBFORMS END CONSTANTS
                        '------------------------------------------------------------------------------
                        
                        '------------------------------------------------------------------------------
                        '   ** Declarations **
                        '------------------------------------------------------------------------------
                        DECLARE CALLBACK FUNCTION ShowDIALOG1Proc()
                        DECLARE FUNCTION ShowDIALOG1(BYVAL hParent AS DWORD) AS LONG
                        #PBFORMS DECLARATIONS
                        '------------------------------------------------------------------------------
                        DECLARE FUNCTION GetCRC (BYVAL pBuff AS BYTE PTR ,BYVAL bufflen AS WORD) AS WORD
                        DECLARE SUB Start()
                        
                        GLOBAL hdlg AS DWORD
                        
                        
                         TYPE wWORD
                            loww AS BYTE
                            highgh AS BYTE
                         END TYPE
                        
                         TYPE PACKET_STRUCT
                            command AS BYTE
                            data_length AS BYTE
                            dData(22) AS BYTE
                            crc AS WORD
                         END TYPE
                        
                         TYPE xcrc WORD
                            low AS BYTE
                            high AS BYTE
                         END TYPE
                        
                        TYPE crclkup  WORD
                            low AS BYTE
                            high AS BYTE
                         END TYPE
                        DECLARE SUB SetcrcTable ()
                        DECLARE SUB TableStart()
                        DECLARE SUB Send_Packet(BYREF packet AS PACKET_STRUCT)
                        DECLARE FUNCTION Get_Crc(BYREF ddata()AS BYTE , BYVAL length AS INTEGER) AS WORD
                        GLOBAL crclowokupTable() AS crclkup
                        '------------------------------------------------------------------------------
                        '   ** Main Application Entry Point **
                        '------------------------------------------------------------------------------
                        FUNCTION PBMAIN()
                            ShowDIALOG1 %HWND_DESKTOP
                        END FUNCTION
                        '------------------------------------------------------------------------------
                        
                        '------------------------------------------------------------------------------
                        '   ** CallBacks **
                        '------------------------------------------------------------------------------
                        CALLBACK FUNCTION ShowDIALOG1Proc()
                        LOCAL hdlg AS DWORD
                            SELECT CASE AS LONG CBMSG
                                CASE %WM_INITDIALOG
                                    ' Initialization handler
                        
                                CASE %WM_NCACTIVATE
                                    STATIC hWndSaveFocus AS DWORD
                                    IF ISFALSE CBWPARAM THEN
                                        ' Save control focus
                                        hWndSaveFocus = GetFocus()
                                    ELSEIF hWndSaveFocus THEN
                                        ' Restore control focus
                                        SetFocus(hWndSaveFocus)
                                        hWndSaveFocus = 0
                                    END IF
                        
                                CASE %WM_COMMAND
                                    ' Process control notifications
                                    SELECT CASE AS LONG CBCTL
                                        ' /* Inserted by PB/Forms 01-07-2006 15:49:34
                                        CASE %IDC_txt2
                        
                                        CASE %IDC_BUTTON2
                                            IF CBCTLMSG = %BN_CLICKED OR CBCTLMSG = 1 THEN
                                                TableStart
                                            END IF
                                        ' */
                        
                                        CASE %IDC_txt1
                        
                                        CASE %IDC_BUTTON1
                                            IF CBCTLMSG = %BN_CLICKED OR CBCTLMSG = 1 THEN
                                                start
                                            END IF
                        
                                    END SELECT
                            END SELECT
                        END FUNCTION
                        '------------------------------------------------------------------------------
                        
                        '------------------------------------------------------------------------------
                        '   ** Dialogs **
                        '------------------------------------------------------------------------------
                        FUNCTION ShowDIALOG1(BYVAL hParent AS DWORD) AS LONG
                            LOCAL lRslt AS LONG
                        
                        #PBFORMS BEGIN DIALOG %IDD_DIALOG1->->
                            'LOCAL hDlg  AS DWORD
                        
                            DIALOG NEW  hParent, "CRC16", 271, 143, 201, 121, %WS_POPUP OR %WS_BORDER _
                                OR %WS_DLGFRAME OR %WS_SYSMENU OR %WS_CLIPSIBLINGS OR %WS_VISIBLE OR _
                                %DS_MODALFRAME OR %DS_3DLOOK OR %DS_NOFAILCREATE OR %DS_SETFONT, _
                                %WS_EX_WINDOWEDGE OR %WS_EX_CONTROLPARENT OR %WS_EX_LEFT OR _
                                %WS_EX_LTRREADING OR %WS_EX_RIGHTSCROLLBAR, TO hDlg
                            CONTROL ADD TEXTBOX, hDlg, %IDC_txt1, "", 50, 30, 100, 13
                            CONTROL ADD BUTTON,  hDlg, %IDC_BUTTON1, "Bit Shift", 75, 50, 50, 15
                            CONTROL ADD TEXTBOX, hDlg, %IDC_txt2, "", 50, 70, 100, 13
                            CONTROL ADD BUTTON,  hDlg, %IDC_BUTTON2, "Table CRC", 75, 90, 50, 15
                        #PBFORMS END DIALOG
                            SetcrcTable
                            DIALOG SHOW MODAL hDlg, CALL ShowDIALOG1Proc TO lRslt
                        
                        #PBFORMS BEGIN CLEANUP %IDD_DIALOG1
                        #PBFORMS END CLEANUP
                        
                            FUNCTION = lRslt
                        END FUNCTION
                        '------------------------------------------------------------------------------
                        
                        
                        SUB Start()
                            '    The correct CRC is 57102 or 0xDF0E
                        
                            LOCAL sd$
                            LOCAL pSByte AS BYTE PTR
                            LOCAL crc AS WORD
                        
                            sd$= CHR$(31) & CHR$(9) & CHR$(0) & CHR$(0) & "Hello"
                            pSByte = STRPTR(sd$)
                        
                        
                            crc = GetCRC(BYVAL pSByte, BYVAL LEN(sd$))
                        
                            CONTROL SET TEXT  hdlg,%idc_txt1, STR$(crc)
                        '   CRC = 57102    correct crc
                        
                        END SUB
                        
                        
                         FUNCTION GetCRC (BYVAL pBuff AS BYTE PTR,BYVAL  bufflen AS WORD) AS WORD
                             REGISTER newCRC AS DWORD
                            LOCAL databyte  AS BYTE
                            LOCAL Bit_Count AS LONG
                        
                            newCRC = &H00F32100
                        
                            WHILE bufflen > 0
                                databyte = @pBuff
                                FOR Bit_Count = 0 TO 7
                                    SHIFT RIGHT newCRC, 1
                                    IF (databyte AND &H01) THEN
                                        newCRC = (newCRC OR &H00800000)
                                        IF (newCRC AND &H00000080) THEN
                                            newCRC = (newCRC XOR &H00840800)
                                        END IF
                                    END IF
                                    SHIFT RIGHT databyte, 1
                                NEXT Bit_Count
                                DECR bufflen
                                INCR pBuff
                            WEND
                            '
                            FOR Bit_Count =  0 TO 15
                                SHIFT RIGHT newCRC, 1
                                IF (newCRC AND &H00000080) THEN
                                    newCRC =  (newCRC XOR &H00840800)
                                END IF
                            NEXT Bit_Count
                        
                            newCRC = NOT newCRC    '1's compliment
                            SHIFT RIGHT newCRC, 8  'shift right by 8 bits
                            FUNCTION = newCRC      'retunn value
                        
                        END FUNCTION
                        
                        
                        '**********Table versio******from VB6 quickly moded to PB************
                        SUB TableStart()
                        
                        LOCAL ret AS LONG
                        LOCAL tmp AS STRING
                        LOCAL ln AS LONG
                        LOCAL i AS LONG
                        
                        LOCAL ds AS PACKET_STRUCT
                        
                                               'cnd=31     len = 9  ds.ddata(0) = 0     ds.ddata(1) = 0    tmp
                              ' same as   sd$= CHR$(31) & CHR$(9)    & CHR$(0) &         CHR$(0) &        "Hello"
                              ds.command =  31
                              tmp = "Hello"                  'len = 5
                              ln = LEN(tmp)
                              ds.data_length = ln + 2        'now len = 7
                              ds.ddata(0) = 0        'col
                              ds.ddata(1) = 0        'row    now len = 9
                              FOR i = 1 TO ln
                                  ds.ddata(i+1)= ASC(MID$(tmp,i,1))
                              NEXT i
                              Send_Packet ds
                        
                        END SUB
                        
                        SUB Send_Packet(BYREF packet AS PACKET_STRUCT)
                        
                        DIM Index AS INTEGER
                        LOCAL ret AS LONG
                        'Need to put the whole packet into a linear array
                        'since you can’t do type overrides. VB, gotta lowve it.
                        DIM linear_array(26) AS BYTE
                        linear_array(0) = packet.command
                        linear_array(1) = packet.data_length
                        FOR Index = 0 TO packet.data_length - 1
                        linear_array(Index + 2) = packet.ddata(Index)
                        NEXT Index
                        
                        packet.crc = Get_Crc(linear_array(), packet.data_length + 2)
                        CONTROL SET TEXT  hdlg,%idc_txt2, STR$(packet.crc)
                        'Might as well move the CRC into the linear array too
                        'linear_array(packet.data_length + 2) = LO(BYTE,packet.crc)
                        'linear_array(packet.data_length + 3) = HI(BYTE,packet.crc)
                        'Now a simple loop can dump it out the port.
                        'FOR Index = 0 TO packet.data_length + 3
                        '     COMM SEND hComm, CHR$(linear_array(Index))
                        'NEXT Index
                        
                        END SUB
                        
                        
                        FUNCTION Get_Crc(BYREF ddata()AS BYTE , BYVAL length AS INTEGER) AS WORD
                        
                        DIM Index AS INTEGER
                        DIM Table_Index AS INTEGER
                        LOCAL ret AS WORD
                        'dim newCrc As WORD
                        
                        DIM newcrc AS xcrc
                        newCrc.low = &HFF
                        newCrc.high = &HFF
                        FOR Index = 0 TO length - 1
                        
                            'exclusive-or the input byte with the loww-order byte of the CRC register
                            'to get an index into crclowokupTable
                            Table_Index = newCrc.low XOR ddata(Index)
                        
                            'shighft the CRC register eight bits to the right
                            newCrc.low = newCrc.high
                            newCrc.high = 0
                        
                            ' exclusive-or the CRC register with the contents of Table at Table_Index
                            newCrc.low = newCrc.low XOR crclowokupTable(Table_Index).low
                        
                            newCrc.high = newCrc.high XOR crclowokupTable(Table_Index).high
                        NEXT Index
                        
                            'Invert & return newCrc
                             ret = MAK(WORD,newCrc.low XOR &HFF,newCrc.high XOR &HFF)
                             FUNCTION = ret
                        END FUNCTION
                        
                        
                        SUB SetcrcTable ()
                         REDIM  crclowokupTable(255) AS crclkup
                        
                          crclowokupTable(0).low = &H0
                          crclowokupTable(0).high = &H0
                          crclowokupTable(1).low = &H89
                          crclowokupTable(1).high = &H11
                          crclowokupTable(2).low = &H12
                          crclowokupTable(2).high = &H23
                          crclowokupTable(3).low = &H9B
                          crclowokupTable(3).high = &H32
                          crclowokupTable(4).low = &H24
                          crclowokupTable(4).high = &H46
                          crclowokupTable(5).low = &HAD
                          crclowokupTable(5).high = &H57
                          crclowokupTable(6).low = &H36
                          crclowokupTable(6).high = &H65
                          crclowokupTable(7).low = &HBF
                          crclowokupTable(7).high = &H74
                          crclowokupTable(8).low = &H48
                          crclowokupTable(8).high = &H8C
                          crclowokupTable(9).low = &HC1
                          crclowokupTable(9).high = &H9D
                          crclowokupTable(10).low = &H5A
                          crclowokupTable(10).high = &HAF
                          crclowokupTable(11).low = &HD3
                          crclowokupTable(11).high = &HBE
                          crclowokupTable(12).low = &H6C
                          crclowokupTable(12).high = &HCA
                          crclowokupTable(13).low = &HE5
                          crclowokupTable(13).high = &HDB
                          crclowokupTable(14).low = &H7E
                          crclowokupTable(14).high = &HE9
                          crclowokupTable(15).low = &HF7
                          crclowokupTable(15).high = &HF8
                          crclowokupTable(16).low = &H81
                          crclowokupTable(16).high = &H10
                          crclowokupTable(17).low = &H8
                          crclowokupTable(17).high = &H1
                          crclowokupTable(18).low = &H93
                          crclowokupTable(18).high = &H33
                          crclowokupTable(19).low = &H1A
                          crclowokupTable(19).high = &H22
                          crclowokupTable(20).low = &HA5
                          crclowokupTable(20).high = &H56
                          crclowokupTable(21).low = &H2C
                          crclowokupTable(21).high = &H47
                          crclowokupTable(22).low = &HB7
                          crclowokupTable(22).high = &H75
                          crclowokupTable(23).low = &H3E
                          crclowokupTable(23).high = &H64
                          crclowokupTable(24).low = &HC9
                          crclowokupTable(24).high = &H9C
                          crclowokupTable(25).low = &H40
                          crclowokupTable(25).high = &H8D
                          crclowokupTable(26).low = &HDB
                          crclowokupTable(26).high = &HBF
                          crclowokupTable(27).low = &H52
                          crclowokupTable(27).high = &HAE
                          crclowokupTable(28).low = &HED
                          crclowokupTable(28).high = &HDA
                          crclowokupTable(29).low = &H64
                          crclowokupTable(29).high = &HCB
                          crclowokupTable(30).low = &HFF
                          crclowokupTable(30).high = &HF9
                          crclowokupTable(31).low = &H76
                          crclowokupTable(31).high = &HE8
                          crclowokupTable(32).low = &H2
                          crclowokupTable(32).high = &H21
                          crclowokupTable(33).low = &H8B
                          crclowokupTable(33).high = &H30
                          crclowokupTable(34).low = &H10
                          crclowokupTable(34).high = &H2
                          crclowokupTable(35).low = &H99
                          crclowokupTable(35).high = &H13
                          crclowokupTable(36).low = &H26
                          crclowokupTable(36).high = &H67
                          crclowokupTable(37).low = &HAF
                          crclowokupTable(37).high = &H76
                          crclowokupTable(38).low = &H34
                          crclowokupTable(38).high = &H44
                          crclowokupTable(39).low = &HBD
                          crclowokupTable(39).high = &H55
                          crclowokupTable(40).low = &H4A
                          crclowokupTable(40).high = &HAD
                          crclowokupTable(41).low = &HC3
                          crclowokupTable(41).high = &HBC
                          crclowokupTable(42).low = &H58
                          crclowokupTable(42).high = &H8E
                          crclowokupTable(43).low = &HD1
                          crclowokupTable(43).high = &H9F
                          crclowokupTable(44).low = &H6E
                          crclowokupTable(44).high = &HEB
                          crclowokupTable(45).low = &HE7
                          crclowokupTable(45).high = &HFA
                          crclowokupTable(46).low = &H7C
                          crclowokupTable(46).high = &HC8
                          crclowokupTable(47).low = &HF5
                          crclowokupTable(47).high = &HD9
                          crclowokupTable(48).low = &H83
                          crclowokupTable(48).high = &H31
                          crclowokupTable(49).low = &HA
                          crclowokupTable(49).high = &H20
                          crclowokupTable(50).low = &H91
                          crclowokupTable(50).high = &H12
                          crclowokupTable(51).low = &H18
                          crclowokupTable(51).high = &H3
                          crclowokupTable(52).low = &HA7
                          crclowokupTable(52).high = &H77
                          crclowokupTable(53).low = &H2E
                          crclowokupTable(53).high = &H66
                          crclowokupTable(54).low = &HB5
                          crclowokupTable(54).high = &H54
                          crclowokupTable(55).low = &H3C
                          crclowokupTable(55).high = &H45
                          crclowokupTable(56).low = &HCB
                          crclowokupTable(56).high = &HBD
                          crclowokupTable(57).low = &H42
                          crclowokupTable(57).high = &HAC
                          crclowokupTable(58).low = &HD9
                          crclowokupTable(58).high = &H9E
                          crclowokupTable(59).low = &H50
                          crclowokupTable(59).high = &H8F
                          crclowokupTable(60).low = &HEF
                          crclowokupTable(60).high = &HFB
                          crclowokupTable(61).low = &H66
                          crclowokupTable(61).high = &HEA
                          crclowokupTable(62).low = &HFD
                          crclowokupTable(62).high = &HD8
                          crclowokupTable(63).low = &H74
                          crclowokupTable(63).high = &HC9
                          crclowokupTable(64).low = &H4
                          crclowokupTable(64).high = &H42
                          crclowokupTable(65).low = &H8D
                          crclowokupTable(65).high = &H53
                          crclowokupTable(66).low = &H16
                          crclowokupTable(66).high = &H61
                          crclowokupTable(67).low = &H9F
                          crclowokupTable(67).high = &H70
                          crclowokupTable(68).low = &H20
                          crclowokupTable(68).high = &H4
                          crclowokupTable(69).low = &HA9
                          crclowokupTable(69).high = &H15
                          crclowokupTable(70).low = &H32
                          crclowokupTable(70).high = &H27
                          crclowokupTable(71).low = &HBB
                          crclowokupTable(71).high = &H36
                          crclowokupTable(72).low = &H4C
                          crclowokupTable(72).high = &HCE
                          crclowokupTable(73).low = &HC5
                          crclowokupTable(73).high = &HDF
                          crclowokupTable(74).low = &H5E
                          crclowokupTable(74).high = &HED
                          crclowokupTable(75).low = &HD7
                          crclowokupTable(75).high = &HFC
                          crclowokupTable(76).low = &H68
                          crclowokupTable(76).high = &H88
                          crclowokupTable(77).low = &HE1
                          crclowokupTable(77).high = &H99
                          crclowokupTable(78).low = &H7A
                          crclowokupTable(78).high = &HAB
                          crclowokupTable(79).low = &HF3
                          crclowokupTable(79).high = &HBA
                          crclowokupTable(80).low = &H85
                          crclowokupTable(80).high = &H52
                          crclowokupTable(81).low = &HC
                          crclowokupTable(81).high = &H43
                          crclowokupTable(82).low = &H97
                          crclowokupTable(82).high = &H71
                          crclowokupTable(83).low = &H1E
                          crclowokupTable(83).high = &H60
                          crclowokupTable(84).low = &HA1
                          crclowokupTable(84).high = &H14
                          crclowokupTable(85).low = &H28
                          crclowokupTable(85).high = &H5
                          crclowokupTable(86).low = &HB3
                          crclowokupTable(86).high = &H37
                          crclowokupTable(87).low = &H3A
                          crclowokupTable(87).high = &H26
                          crclowokupTable(88).low = &HCD
                          crclowokupTable(88).high = &HDE
                          crclowokupTable(89).low = &H44
                          crclowokupTable(89).high = &HCF
                          crclowokupTable(90).low = &HDF
                          crclowokupTable(90).high = &HFD
                          crclowokupTable(91).low = &H56
                          crclowokupTable(91).high = &HEC
                          crclowokupTable(92).low = &HE9
                          crclowokupTable(92).high = &H98
                          crclowokupTable(93).low = &H60
                          crclowokupTable(93).high = &H89
                          crclowokupTable(94).low = &HFB
                          crclowokupTable(94).high = &HBB
                          crclowokupTable(95).low = &H72
                          crclowokupTable(95).high = &HAA
                          crclowokupTable(96).low = &H6
                          crclowokupTable(96).high = &H63
                          crclowokupTable(97).low = &H8F
                          crclowokupTable(97).high = &H72
                          crclowokupTable(98).low = &H14
                          crclowokupTable(98).high = &H40
                          crclowokupTable(99).low = &H9D
                          crclowokupTable(99).high = &H51
                          crclowokupTable(100).low = &H22
                          crclowokupTable(100).high = &H25
                          crclowokupTable(101).low = &HAB
                          crclowokupTable(101).high = &H34
                          crclowokupTable(102).low = &H30
                          crclowokupTable(102).high = &H6
                          crclowokupTable(103).low = &HB9
                          crclowokupTable(103).high = &H17
                          crclowokupTable(104).low = &H4E
                          crclowokupTable(104).high = &HEF
                          crclowokupTable(105).low = &HC7
                          crclowokupTable(105).high = &HFE
                          crclowokupTable(106).low = &H5C
                          crclowokupTable(106).high = &HCC
                          crclowokupTable(107).low = &HD5
                          crclowokupTable(107).high = &HDD
                          crclowokupTable(108).low = &H6A
                          crclowokupTable(108).high = &HA9
                          crclowokupTable(109).low = &HE3
                          crclowokupTable(109).high = &HB8
                          crclowokupTable(110).low = &H78
                          crclowokupTable(110).high = &H8A
                          crclowokupTable(111).low = &HF1
                          crclowokupTable(111).high = &H9B
                          crclowokupTable(112).low = &H87
                          crclowokupTable(112).high = &H73
                          crclowokupTable(113).low = &HE
                          crclowokupTable(113).high = &H62
                          crclowokupTable(114).low = &H95
                          crclowokupTable(114).high = &H50
                          crclowokupTable(115).low = &H1C
                          crclowokupTable(115).high = &H41
                          crclowokupTable(116).low = &HA3
                          crclowokupTable(116).high = &H35
                          crclowokupTable(117).low = &H2A
                          crclowokupTable(117).high = &H24
                          crclowokupTable(118).low = &HB1
                          crclowokupTable(118).high = &H16
                          crclowokupTable(119).low = &H38
                          crclowokupTable(119).high = &H7
                          crclowokupTable(120).low = &HCF
                          crclowokupTable(120).high = &HFF
                          crclowokupTable(121).low = &H46
                          crclowokupTable(121).high = &HEE
                          crclowokupTable(122).low = &HDD
                          crclowokupTable(122).high = &HDC
                          crclowokupTable(123).low = &H54
                          crclowokupTable(123).high = &HCD
                          crclowokupTable(124).low = &HEB
                          crclowokupTable(124).high = &HB9
                          crclowokupTable(125).low = &H62
                          crclowokupTable(125).high = &HA8
                          crclowokupTable(126).low = &HF9
                          crclowokupTable(126).high = &H9A
                          crclowokupTable(127).low = &H70
                          crclowokupTable(127).high = &H8B
                          crclowokupTable(128).low = &H8
                          crclowokupTable(128).high = &H84
                          crclowokupTable(129).low = &H81
                          crclowokupTable(129).high = &H95
                          crclowokupTable(130).low = &H1A
                          crclowokupTable(130).high = &HA7
                          crclowokupTable(131).low = &H93
                          crclowokupTable(131).high = &HB6
                          crclowokupTable(132).low = &H2C
                          crclowokupTable(132).high = &HC2
                          crclowokupTable(133).low = &HA5
                          crclowokupTable(133).high = &HD3
                          crclowokupTable(134).low = &H3E
                          crclowokupTable(134).high = &HE1
                          crclowokupTable(135).low = &HB7
                          crclowokupTable(135).high = &HF0
                          crclowokupTable(136).low = &H40
                          crclowokupTable(136).high = &H8
                          crclowokupTable(137).low = &HC9
                          crclowokupTable(137).high = &H19
                          crclowokupTable(138).low = &H52
                          crclowokupTable(138).high = &H2B
                          crclowokupTable(139).low = &HDB
                          crclowokupTable(139).high = &H3A
                          crclowokupTable(140).low = &H64
                          crclowokupTable(140).high = &H4E
                          crclowokupTable(141).low = &HED
                          crclowokupTable(141).high = &H5F
                          crclowokupTable(142).low = &H76
                          crclowokupTable(142).high = &H6D
                          crclowokupTable(143).low = &HFF
                          crclowokupTable(143).high = &H7C
                          crclowokupTable(144).low = &H89
                          crclowokupTable(144).high = &H94
                          crclowokupTable(145).low = &H0
                          crclowokupTable(145).high = &H85
                          crclowokupTable(146).low = &H9B
                          crclowokupTable(146).high = &HB7
                          crclowokupTable(147).low = &H12
                          crclowokupTable(147).high = &HA6
                          crclowokupTable(148).low = &HAD
                          crclowokupTable(148).high = &HD2
                          crclowokupTable(149).low = &H24
                          crclowokupTable(149).high = &HC3
                          crclowokupTable(150).low = &HBF
                          crclowokupTable(150).high = &HF1
                          crclowokupTable(151).low = &H36
                          crclowokupTable(151).high = &HE0
                          crclowokupTable(152).low = &HC1
                          crclowokupTable(152).high = &H18
                          crclowokupTable(153).low = &H48
                          crclowokupTable(153).high = &H9
                          crclowokupTable(154).low = &HD3
                          crclowokupTable(154).high = &H3B
                          crclowokupTable(155).low = &H5A
                          crclowokupTable(155).high = &H2A
                          crclowokupTable(156).low = &HE5
                          crclowokupTable(156).high = &H5E
                          crclowokupTable(157).low = &H6C
                          crclowokupTable(157).high = &H4F
                          crclowokupTable(158).low = &HF7
                          crclowokupTable(158).high = &H7D
                          crclowokupTable(159).low = &H7E
                          crclowokupTable(159).high = &H6C
                          crclowokupTable(160).low = &HA
                          crclowokupTable(160).high = &HA5
                          crclowokupTable(161).low = &H83
                          crclowokupTable(161).high = &HB4
                          crclowokupTable(162).low = &H18
                          crclowokupTable(162).high = &H86
                          crclowokupTable(163).low = &H91
                          crclowokupTable(163).high = &H97
                          crclowokupTable(164).low = &H2E
                          crclowokupTable(164).high = &HE3
                          crclowokupTable(165).low = &HA7
                          crclowokupTable(165).high = &HF2
                          crclowokupTable(166).low = &H3C
                          crclowokupTable(166).high = &HC0
                          crclowokupTable(167).low = &HB5
                          crclowokupTable(167).high = &HD1
                          crclowokupTable(168).low = &H42
                          crclowokupTable(168).high = &H29
                          crclowokupTable(169).low = &HCB
                          crclowokupTable(169).high = &H38
                          crclowokupTable(170).low = &H50
                          crclowokupTable(170).high = &HA
                          crclowokupTable(171).low = &HD9
                          crclowokupTable(171).high = &H1B
                          crclowokupTable(172).low = &H66
                          crclowokupTable(172).high = &H6F
                          crclowokupTable(173).low = &HEF
                          crclowokupTable(173).high = &H7E
                          crclowokupTable(174).low = &H74
                          crclowokupTable(174).high = &H4C
                          crclowokupTable(175).low = &HFD
                          crclowokupTable(175).high = &H5D
                          crclowokupTable(176).low = &H8B
                          crclowokupTable(176).high = &HB5
                          crclowokupTable(177).low = &H2
                          crclowokupTable(177).high = &HA4
                          crclowokupTable(178).low = &H99
                          crclowokupTable(178).high = &H96
                          crclowokupTable(179).low = &H10
                          crclowokupTable(179).high = &H87
                          crclowokupTable(180).low = &HAF
                          crclowokupTable(180).high = &HF3
                          crclowokupTable(181).low = &H26
                          crclowokupTable(181).high = &HE2
                          crclowokupTable(182).low = &HBD
                          crclowokupTable(182).high = &HD0
                          crclowokupTable(183).low = &H34
                          crclowokupTable(183).high = &HC1
                          crclowokupTable(184).low = &HC3
                          crclowokupTable(184).high = &H39
                          crclowokupTable(185).low = &H4A
                          crclowokupTable(185).high = &H28
                          crclowokupTable(186).low = &HD1
                          crclowokupTable(186).high = &H1A
                          crclowokupTable(187).low = &H58
                          crclowokupTable(187).high = &HB
                          crclowokupTable(188).low = &HE7
                          crclowokupTable(188).high = &H7F
                          crclowokupTable(189).low = &H6E
                          crclowokupTable(189).high = &H6E
                          crclowokupTable(190).low = &HF5
                          crclowokupTable(190).high = &H5C
                          crclowokupTable(191).low = &H7C
                          crclowokupTable(191).high = &H4D
                          crclowokupTable(192).low = &HC
                          crclowokupTable(192).high = &HC6
                          crclowokupTable(193).low = &H85
                          crclowokupTable(193).high = &HD7
                          crclowokupTable(194).low = &H1E
                          crclowokupTable(194).high = &HE5
                          crclowokupTable(195).low = &H97
                          crclowokupTable(195).high = &HF4
                          crclowokupTable(196).low = &H28
                          crclowokupTable(196).high = &H80
                          crclowokupTable(197).low = &HA1
                          crclowokupTable(197).high = &H91
                          crclowokupTable(198).low = &H3A
                          crclowokupTable(198).high = &HA3
                          crclowokupTable(199).low = &HB3
                          crclowokupTable(199).high = &HB2
                          crclowokupTable(200).low = &H44
                          crclowokupTable(200).high = &H4A
                          crclowokupTable(201).low = &HCD
                          crclowokupTable(201).high = &H5B
                          crclowokupTable(202).low = &H56
                          crclowokupTable(202).high = &H69
                          crclowokupTable(203).low = &HDF
                          crclowokupTable(203).high = &H78
                          crclowokupTable(204).low = &H60
                          crclowokupTable(204).high = &HC
                          crclowokupTable(205).low = &HE9
                          crclowokupTable(205).high = &H1D
                          crclowokupTable(206).low = &H72
                          crclowokupTable(206).high = &H2F
                          crclowokupTable(207).low = &HFB
                          crclowokupTable(207).high = &H3E
                          crclowokupTable(208).low = &H8D
                          crclowokupTable(208).high = &HD6
                          crclowokupTable(209).low = &H4
                          crclowokupTable(209).high = &HC7
                          crclowokupTable(210).low = &H9F
                          crclowokupTable(210).high = &HF5
                          crclowokupTable(211).low = &H16
                          crclowokupTable(211).high = &HE4
                          crclowokupTable(212).low = &HA9
                          crclowokupTable(212).high = &H90
                          crclowokupTable(213).low = &H20
                          crclowokupTable(213).high = &H81
                          crclowokupTable(214).low = &HBB
                          crclowokupTable(214).high = &HB3
                          crclowokupTable(215).low = &H32
                          crclowokupTable(215).high = &HA2
                          crclowokupTable(216).low = &HC5
                          crclowokupTable(216).high = &H5A
                          crclowokupTable(217).low = &H4C
                          crclowokupTable(217).high = &H4B
                          crclowokupTable(218).low = &HD7
                          crclowokupTable(218).high = &H79
                          crclowokupTable(219).low = &H5E
                          crclowokupTable(219).high = &H68
                          crclowokupTable(220).low = &HE1
                          crclowokupTable(220).high = &H1C
                          crclowokupTable(221).low = &H68
                          crclowokupTable(221).high = &HD
                          crclowokupTable(222).low = &HF3
                          crclowokupTable(222).high = &H3F
                          crclowokupTable(223).low = &H7A
                          crclowokupTable(223).high = &H2E
                          crclowokupTable(224).low = &HE
                          crclowokupTable(224).high = &HE7
                          crclowokupTable(225).low = &H87
                          crclowokupTable(225).high = &HF6
                          crclowokupTable(226).low = &H1C
                          crclowokupTable(226).high = &HC4
                          crclowokupTable(227).low = &H95
                          crclowokupTable(227).high = &HD5
                          crclowokupTable(228).low = &H2A
                          crclowokupTable(228).high = &HA1
                          crclowokupTable(229).low = &HA3
                          crclowokupTable(229).high = &HB0
                          crclowokupTable(230).low = &H38
                          crclowokupTable(230).high = &H82
                          crclowokupTable(231).low = &HB1
                          crclowokupTable(231).high = &H93
                          crclowokupTable(232).low = &H46
                          crclowokupTable(232).high = &H6B
                          crclowokupTable(233).low = &HCF
                          crclowokupTable(233).high = &H7A
                          crclowokupTable(234).low = &H54
                          crclowokupTable(234).high = &H48
                          crclowokupTable(235).low = &HDD
                          crclowokupTable(235).high = &H59
                          crclowokupTable(236).low = &H62
                          crclowokupTable(236).high = &H2D
                          crclowokupTable(237).low = &HEB
                          crclowokupTable(237).high = &H3C
                          crclowokupTable(238).low = &H70
                          crclowokupTable(238).high = &HE
                          crclowokupTable(239).low = &HF9
                          crclowokupTable(239).high = &H1F
                          crclowokupTable(240).low = &H8F
                          crclowokupTable(240).high = &HF7
                          crclowokupTable(241).low = &H6
                          crclowokupTable(241).high = &HE6
                          crclowokupTable(242).low = &H9D
                          crclowokupTable(242).high = &HD4
                          crclowokupTable(243).low = &H14
                          crclowokupTable(243).high = &HC5
                          crclowokupTable(244).low = &HAB
                          crclowokupTable(244).high = &HB1
                          crclowokupTable(245).low = &H22
                          crclowokupTable(245).high = &HA0
                          crclowokupTable(246).low = &HB9
                          crclowokupTable(246).high = &H92
                          crclowokupTable(247).low = &H30
                          crclowokupTable(247).high = &H83
                          crclowokupTable(248).low = &HC7
                          crclowokupTable(248).high = &H7B
                          crclowokupTable(249).low = &H4E
                          crclowokupTable(249).high = &H6A
                          crclowokupTable(250).low = &HD5
                          crclowokupTable(250).high = &H58
                          crclowokupTable(251).low = &H5C
                          crclowokupTable(251).high = &H49
                          crclowokupTable(252).low = &HE3
                          crclowokupTable(252).high = &H3D
                          crclowokupTable(253).low = &H6A
                          crclowokupTable(253).high = &H2C
                          crclowokupTable(254).low = &HF1
                          crclowokupTable(254).high = &H1E
                          crclowokupTable(255).low = &H78
                          crclowokupTable(255).high = &HF
                        
                          END SUB
                        ------------------
                        Doug McDonald
                        KD5NWK
                        www.redforksoftware.com

                        Comment


                        • #13
                          Code:
                          SUB SetcrcTable ()
                           REDIM  crclowokupTable(255) AS crclkup
                           crclowokupTable(0).low = &H0
                           crclowokupTable(0).high = &H0
                           crclowokupTable(1).low = &H89
                           crclowokupTable(1).high = &H11
                           ...
                           crclowokupTable(255).high = &HF
                          See also: DATA, READ$, FOR/NEXT
                          Michael Mattias
                          Tal Systems (retired)
                          Port Washington WI USA
                          [email protected]
                          http://www.talsystems.com

                          Comment


                          • #14
                            Mike, I'm fully aware of the proper way to do a look up table

                            As I said before this was just a quick conversion from VB to PB
                            to calculate the correct CRC and check it against the GetCRC
                            function that uses a bit shift and not a table.

                            maybe you could help me out here because I do not see why GetCRC
                            doees not work

                            Thanks

                            ------------------
                            Doug McDonald
                            KD5NWK
                            www.redforksoftware.com

                            Comment


                            • #15
                              Doug,

                              Was your original C source routine copy and pasted or is there the possibility of a typo.

                              I found another routine converted as below, added comments, and it seems to be closer.

                              Also it looks like you are passing 7 as the second characer in the string, not 9 for the table fetch routine.
                              Shouldn't the C conversions use : strData = CHR$(31) & CHR$(7) & CHR$(0) & CHR$(0) & "Hello"

                              Code:
                              'REF: http://www.experts-exchange.com/Prog..._21581495.html 
                              FUNCTION AGetCRC(BYVAL CommData AS BYTE PTR,BYVAL buflen AS WORD) AS WORD
                                  REGISTER uCRC16 AS WORD
                                  DIM abWord      AS WORD  'added to break out the MAK function
                                  DIM abData(2)   AS BYTE
                                  uCRC16       =  &HFFFF   'the algo uses  0
                                  abData(0)    =  0
                              
                                  WHILE buflen > 0
                                      DECR buflen
                                      abData(1) = abData(0)
                                      abData(0) = @CommData
                                      INCR CommData
                                      IF (uCRC16 AND &H8000) THEN
                                          uCRC16 = (uCRC16 AND &H7FFF)      'the algo uses &H7FFF , try &H8FFF
                                          SHIFT LEFT uCRC16, 1
                                          uCRC16 = (uCRC16 XOR &H8005)      'the algo uses &H8005
                                      ELSE
                                          SHIFT LEFT uCRC16, 1
                                      END IF
                                      abWord = MAK(WORD, abData(0), abData(1))
                                      uCRC16 = (uCRC16 XOR abWord)
                                  WEND
                              
                                  FUNCTION = uCRC16
                              
                              END FUNCTION

                              ------------------
                              Rick Angell
                              Rick Angell

                              Comment


                              • #16
                                The C code is Copied from a data sheet for the device that needs
                                the CRC That said, errors in data sheets are not uncommon.
                                I'll try the new code you posted.

                                Thanks again for your time


                                ------------------
                                Doug McDonald
                                KD5NWK
                                www.redforksoftware.com

                                Comment


                                • #17
                                  Doug,

                                  The new code needs some mods still. maybe in the XOR'd values or seed or ??, however it seems to get more in the range of CRC you are looking for. Do you have a link to the datasheet mentioned.


                                  ------------------
                                  Rick Angell
                                  Rick Angell

                                  Comment


                                  • #18
                                    Rick, The data sheet is
                                    http://www.crystalfontz.com/products...ata_sheet.html

                                    I'm pretty shure there's a error in thier code. When I get to work
                                    tomorrow I'll compile the C code and test it.

                                    Thanks again

                                    ------------------
                                    Doug McDonald
                                    KD5NWK
                                    www.redforksoftware.com

                                    Comment


                                    • #19
                                      No need , found the problem, try this revision for the function, after changing sd$
                                      sd$= CHR$(31) & CHR$(7) & CHR$(0) & CHR$(0) & "Hello"

                                      Code:
                                      FUNCTION GetCRC (BYVAL pBuff AS BYTE PTR,BYVAL  bufflen AS WORD) AS WORD
                                          REGISTER newCRC AS DWORD
                                          LOCAL databyte  AS BYTE
                                          LOCAL Bit_Count AS LONG
                                      
                                          newCRC = &H00F32100
                                      
                                          WHILE bufflen > 0
                                              DECR bufflen
                                      
                                              databyte = @pBuff
                                              INCR pBuff
                                      
                                              FOR Bit_Count = 0 TO 7
                                      
                                                  SHIFT RIGHT newCRC, 1
                                      
                                                  IF (databyte AND &H01)     THEN  newCRC = (newCRC OR  &H00800000)
                                      
                                                  IF (newCRC AND &H00000080) THEN  newCRC = (newCRC XOR &H00840800)
                                      
                                                  SHIFT RIGHT databyte, 1
                                      
                                              NEXT Bit_Count
                                      
                                          WEND
                                          '
                                          FOR Bit_Count =  0 TO 15
                                      
                                              SHIFT RIGHT newCRC, 1
                                      
                                              IF (newCRC AND &H00000080) THEN  newCRC =  (newCRC XOR &H00840800)
                                      
                                          NEXT Bit_Count
                                      
                                          newCRC = NOT newCRC    '1's compliment
                                          SHIFT RIGHT newCRC, 8  'shift right by 8 bits
                                          FUNCTION = newCRC      'return value
                                      
                                      END FUNCTION
                                      ------------------
                                      Rick Angell
                                      Rick Angell

                                      Comment


                                      • #20
                                        Thank you, It works perfect.

                                        Doug

                                        ------------------
                                        Doug McDonald
                                        KD5NWK
                                        www.redforksoftware.com

                                        Comment

                                        Working...
                                        X