CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    [RESOLVED] CRC calculation implementation HELL..

    So now i can comms with the device, however the CRC calculation does not work correctly... (I have 0 access to the Firmware) but the suppliers graciously gave us the CRC calculation in C++, which is as such
    Code:
    static unsigned BitsSet (unsigned char ch)
    {
        unsigned n;
        n = 0;
        while (ch)
        {
            n += (ch & 1);
            ch >>= 1;
        }
        return(n);
    }
    
    unsigned CRCof (const char *message, unsigned len)
    {
        unsigned i;
        unsigned crc;
        unsigned char k;
        crc = 0;
        for (i=0; i<len; i++)
        {
            k = (unsigned char)(message[i]) ^ crc;
            crc = (crc / 256) ^ (k*128) ^ (k*64);
            if ((BitsSet(k) & 1) != 0)
                crc ^= 0xC001;
            }
        return(crc);
    }
    Now my VB implementation
    Code:
            Friend Function CRC(ByVal ParamArray Msg As Byte()) As Byte() 'CRC is 2 byte length
                Dim Result As UInt16 = 0
                Dim tmpB As Byte = 0
                Dim tmpint As Integer
                For tmpint = 0 To Msg.Length - 1
                    tmpB = Msg(tmpint) Xor Result And &HFF
                    Result = (Result \ 256) Xor (tmpB * 128) Xor (tmpB * 64)
                    If ((BitsSet(tmpUint) And 1) <> 0) Then
                        Result = Result Xor &HC001
                    End If
                Next
                CRC = ToByteArray(Result)
            End Function
    
            Friend Function BitsSet(ByVal data As UInt32) As Byte
                BitsSet = 0
                While data
                    BitsSet += (data And 1)
                    data >>= 1
                End While
            End Function
    Now no matter what i send to the device it always reply's with CRC error "GL!ER20A624" (the last 4 digits are the modules calculated CRC)... And if i send the module some Garbage i get a Invalid request "GL!ER2166E5"..

    now using my code i'm trying to replicate the CRC, with 0 luck ....

    What have i converted incorrectly ???? I just cant see it.....
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  2. #2
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: CRC calculation implementation HELL..

    True's nuts... Post about it and all of a sardine you fix it, never mind you've spent day's on it...

    Still not 100% sure what was wrong, but i tidied up the code to post it here, and when i put it back into the app from here, it worked...

    Must have been a typo somewhere, that i missed but accidentally fixed when making it tidy to post...

    Regardless.. it works now \o/ \o/ \o/ ....
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured