CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2008
    Posts
    63

    Question how to pass LPBYTE value?

    Hi all,

    I'm trying to perform CRC Calculation for a set of byte.
    My Transmit Buffer is LPBYTE.

    here is my code

    Code:
    	CRCCalculator objCRCCalculator;
    	LPBYTE TransBuf = new BYTE[12];
    	TransBuf[0]=0xE1;
    	TransBuf[1]=0x80;
    	TransBuf[2]=0x01;
    	TransBuf[3]=0xE1;
    	TransBuf[4]=0xD2;
    	TransBuf[5]=0xC3;
    	TransBuf[6]=0x00;
    	TransBuf[7]=0x00;
    	TransBuf[8]=0x00;
    	TransBuf[9]=0x00;
    	TransBuf[10]=0x00;
    	TransBuf[11]=0x00;
    	objCRCCalculator.CalculateCRC(TransBuf);
    
    
    
    
    //here is my CRC calculation
    
    
    unsigned short CRCCalculator::update_crc_16( unsigned short crc, unsigned char  c ) 
    {
        unsigned short tmp, short_c;	
        short_c = 0x00ff & (unsigned short)c;
        if ( ! crc_tab16_init )
    		init_crc16_tab();
        tmp =  crc       ^ short_c;
        crc = (crc >> 8) ^ crc_tab16[ tmp & 0xff ];   
        return crc;
    
    }  
    
    static void init_crc16_tab( void ) 
    {
        int i, j;
        unsigned short crc, c;
        for (i=0; i<256; i++) 
    	{
    		crc = 0;
            c   = (unsigned short) i;
    
            for (j=0; j<8; j++)
    		{
    
                if ( (crc ^ c) & 0x0001 )
    				crc = ( crc >> 1 ) ^ P_16;
                else                      
    				crc =   crc >> 1;
                c = c >> 1;
            }
    
            crc_tab16[i] = crc;
        }
        crc_tab16_init = TRUE;
    }
    
    void CRCCalculator::CalculateCRC(LPBYTE& TxBuf)
    {	
    	DWORD CRCValue;
    	WORD crc_16_modbus  = 0xffff;
    	for(int i=0;i<12;i++)
    		crc_16_modbus = update_crc_16(crc_16_modbus,TxBuf[i]);	
    	CRCValue=crc_16_modbus;
    	LPBYTE CRCByte = new BYTE[2];
    	CRCByte[0]=HIBYTE(CRCValue);
    	CRCByte[1]=LOBYTE(CRCValue);
    }
    I'm trying to get the CRC value for transmit buffer. but some constant value is overwrite the current values of transmit buffer (TxBuf). I think it is because of passing the LPBYTE by reference. Can anyone tell me how to pass this value? or suggest some way to implement it.

    With Thanks & regards,
    Saravana

  2. #2
    Join Date
    Apr 2009
    Location
    Cochin
    Posts
    83

    Re: how to pass LPBYTE value?

    @ Saravana.

    Your function is expecting an Adress to a pointer to buffer... LPBYTE is BYTE* and your function parameter is expecting adress of a BYTE*.


    And, you are passing plainly a buffer into the function. Are you sure you had given that for the corect intent?
    "I studied everything but never topped. Today, toppers of the world's best universities are my employees"

    -William Henry Gates (Bill Gates)

  3. #3
    Join Date
    Sep 2008
    Posts
    63

    Re: how to pass LPBYTE value?

    yes I'm sure that i'm passing an LPBYTE only

    see the declaration which is shown in the 2nd line of code

    Code:
    LPBYTE TransBuf = new BYTE[12];
    And I have got it.. It is working well. I have made one bleddy mistake in my code. becoz of that only the problem araised.....

    whats tat is in my CRC calculation

    Code:
    void CRCCalculator::CalculateCRC(LPBYTE& TxBuf)
    {	
    	DWORD CRCValue;
    	WORD crc_16_modbus  = 0xffff;
    	for(int i=0;i<=12;i++)                         //here the problem is, range i have given is not correct
    		crc_16_modbus = update_crc_16(crc_16_modbus,TxBuf[i]);	
    	CRCValue=crc_16_modbus;
    	LPBYTE CRCByte = new BYTE[2];
    	CRCByte[0]=HIBYTE(CRCValue);
    	CRCByte[1]=LOBYTE(CRCValue);
    }
    Thanks for your response. Its now working well

    with regards,
    Saravana

  4. #4
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: how to pass LPBYTE value?

    Why are you passing a reference to the buffer (LPBYTE& TxBuf)? You only need that if you want to modify the pointer that points to the start of the buffer. Your function should look like this:
    Code:
    void CRCCalculator::CalculateCRC(const LPBYTE TxBuf)
    And in general, when you pass buffers, also pass the size of the buffers and don't assume.
    Code:
    void CRCCalculator::CalculateCRC(const LPBYTE TxBuf, size_t size)
    {
    }
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  5. #5
    Join Date
    Sep 2008
    Posts
    63

    Re: how to pass LPBYTE value?

    yeah. Thanks for your update Cilu.. I'll take the second one which is more suitable for any implementation..

    thanks once again.

    With regards,
    Saravana

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