Solved

Problems converting method from 16bit to 32bit... please help!

Posted on 2004-09-23
7
395 Views
Last Modified: 2008-02-01
I think i managed to convert some 16bit method to its 32bit equivalent, but when i attempt to execute it on the legacy platform, it behaves differently compared to the original method. Please could someone check if i have converted the code correctly, as im not sure what could be wrong. Thanks.

Original 16bit method
---------------------------------------------------------------------------------------------------------------
int ReceiveData(void)
{
      COMSTAT         CommsState;
      int                     iError;
      int             Length;
      int                     ByteCount;


      if (bCommsOK)
      {
            iError = GetCommError(iCommsID, &CommsState);

            if (iError == 0)
            {
                  Length = CommsState.cbInQue;

          /*Check if there is any data buffered by Windows to be read */
                  if (Length > 0)
                  {
            /*Make sure the buffer is big enough to recieve the     */
                        /*buffered data                                                                                 */
                        if ((BUFSIZE - RxBuf.Count) < Length)
                        {
                              /*buffer does not have enough room to receive all       */
                              /*the buffered data.  So throw away data untill     */
                              /*it will fit.                                                                          */
                              while ((Length > 0) && (iError >= 0))
                              {
                                    if (Length > BUFSIZE)
                                    {
                                          ByteCount = BUFSIZE;
                                    }
                                    else
                                    {
                                          ByteCount = Length;
                                    }
                                    Length -= ByteCount;
                                    iError = ReadComm(iCommsID,&RxBuf.Buf[0],ByteCount);
                              }
                              Length = ByteCount;
                              RxBuf.Count = ByteCount;
                              RxBuf.op = 0;
                              RxBuf.ip = ByteCount;
                        }
            else
            {
                              /*Receive the data                                                                              */

                              /* If necessary, receive the data in two passes,                */
                              /* wrapping around the buffer.                                                  */

                              if ((BUFSIZE - RxBuf.ip) < Length)
                              {
                                    /* Fill to end of buffer.                                                       */

                                    iError = ReadComm(iCommsID, &RxBuf.Buf[RxBuf.ip],
                                                             BUFSIZE - RxBuf.ip);
                                    if (iError > 0)
                                    {
                                          /* Now put the rest in the start of the buffer. */
                                          iError = ReadComm(iCommsID, &RxBuf.Buf[0],
                                                                   Length - BUFSIZE - RxBuf.ip);

                                          /* Wrap the input pointer.                                              */
                                          RxBuf.ip = ((RxBuf.ip + Length) %BUFSIZE);

                                          /* Update the number of chars in the buffer     */
                                          RxBuf.Count += Length;
                                    }
                              }
                              /* otherwise just receive it in 1 go.                                   */
                              else
                              {
                                    iError = ReadComm(iCommsID, &RxBuf.Buf[RxBuf.ip],Length);

                                    /* Update input posn and number of chars in buffer      */
                                    RxBuf.ip += Length;
                                    RxBuf.Count += Length;
                              }
                        
                        }
                        if (iError < 0)
                        {      
                              iError = GetCommError(iCommsID, &CommsState);
                              return -4200 - iError;
                              /* Comms Read Error     */
                        }
                        else
            {
                              if      (iError == 0)
                              {
                                    iError = GetCommError(iCommsID, &CommsState);
                                    if (iError != 0)
                  {
                                          return -4400 - iError;
                                          /* Comms Read Error */
                  }
                                    else
                                    {
                                          return Length; /*Number of bytes transferred*/
                  }
                }
            }
                  }
                  else
                  {
                        return 0;       /* No data to be transferred                            */
                  }
      }
            else
      {
                  return -4600 - iError;
                  /*Comms Read channel Fault */
      }
      }
      else
      {
            return -4800;
      }
}

Converted 32bit method
---------------------------------------------------------------------------------------------------------------

int ReceiveData(void)
{
      COMSTAT CommsState;
      int iError;
      int Length;
      int ByteCount;
      int nBytesRead;
      int dwErrors;
      char buffer[255];

      if (bCommsOK)
      {
            log_(LOG_LEVEL_INFO,INF_RECEIVE_DATA,"","");
            iError = GetCommState(hComm, &dcb);

            if (iError == 0)
            {
                  ClearCommError(hComm, &dwErrors, &CommsState); // Get ComState
                  Length = CommsState.cbInQue;

                  // Check if there is any data buffered by Windows to be read
                  if (Length > 0)
                  {
                        // Make sure the buffer is big enough to recieve the buffered data                                                                                 */
                        if ((BUFSIZE - RxBuf.Count) < Length)
                        {
                              // buffer does not have enough room to receive all the
                              // buffered data.  So throw away data untill it will fit.                                                                          */
                              while ((Length > 0) && (iError >= 0))
                              {
                                    if (Length > BUFSIZE)
                                    {
                                          ByteCount = BUFSIZE;
                                    }
                                    else
                                    {
                                          ByteCount = Length;
                                    }
                                    Length -= ByteCount;
                                    
                                    // Attempt a synchronous read operation.
                                    iError = ReadFile(hComm, &RxBuf.Buf[0], ByteCount,
                                          &nBytesRead, NULL) ;
                              }
                              Length = ByteCount;
                              RxBuf.Count = ByteCount;
                              RxBuf.op = 0;
                              RxBuf.ip = ByteCount;
                        }
            else
            {
                              // Receive the data If necessary, receive the data in two
                              // passes, wrapping around the buffer.

                              if ((BUFSIZE - RxBuf.ip) < Length)
                              {
                                    // Fill to end of buffer.
                                    iError = ReadFile(hComm, &RxBuf.Buf[RxBuf.ip], BUFSIZE
                                          - RxBuf.ip, &nBytesRead, NULL);

                                    if (iError > 0)
                                    {
                                          // Now put the rest in the start of the buffer.
                                          iError = ReadFile(hComm, &RxBuf.Buf[0], Length -
                                                BUFSIZE - RxBuf.ip, &nBytesRead, NULL);

                                          // Wrap the input pointer.
                                          RxBuf.ip = ((RxBuf.ip + Length) %BUFSIZE);

                                          // Update the number of chars in the buffer
                                          RxBuf.Count += Length;
                                    }
                              }
                              // otherwise just receive it in 1 go.
                              else
                              {
                                    iError = ReadFile(hComm, &RxBuf.Buf[RxBuf.ip], Length,
                                          &nBytesRead, NULL);

                                    // Update input posn and number of chars in buffer
                                    RxBuf.ip += Length;
                                    RxBuf.Count += Length;
                              }
                        
                        }
                        if (iError < 0)
                        {      
                              iError = GetCommState(hComm, &dcb);

                              itoa(iError,buffer,2);
                              log_(LOG_LEVEL_ERROR,ERR_RECEIVE_DATA, buffer, "");

                              return -4200 - iError;
                              // Comms Read Error
                        }
                        else
            {
                              if      (iError == 0)
                              {
                                    iError = GetCommState(hComm, &dcb);
                                    if (iError != 0)
                  {
                                          itoa(iError,buffer,2);
                                          log_(LOG_LEVEL_ERROR,ERR_RECEIVE_DATA, buffer, "");

                                          return -4400 - iError;
                                          // Comms Read Error
                  }
                                    else
                                    {
                                          return Length; // Number of bytes transferred
                  }
                }
            }
                  }
                  else
                  {
                        return 0; // No data to be transferred
                  }
      }
            else
      {
                  itoa(iError,buffer,2);
                  log_(LOG_LEVEL_ERROR,ERR_RECEIVE_DATA, buffer, "");
                  return -4600 - iError;
                  // Comms Read channel Fault
      }
      }
      else
      {
            itoa(iError,buffer,2);
            log_(LOG_LEVEL_ERROR,ERR_RECEIVE_DATA, buffer, "");
            return -4800;
      }

      return 0;
}
0
Comment
Question by:Actia
7 Comments
 
LVL 22

Expert Comment

by:grg99
Comment Utility
The problem is likely to be in the lengths of "int".  

first look in whatever header file defines the ReadFile() API's, you should see something like "INT16 * ReadLen" for the 4th parameter to ReadFile.


Try using the ORIGINAL 16-bit code, but change all the "int" vars to INT16 or whatever type the header file says.

Then change any other int's to that type if they are used in math with that INT16 variable.

Also compile it with all warnings turned on, it should warn about int length incompatibilities a lot better with warnings turned on.  
0
 
LVL 16

Expert Comment

by:PaulCaswell
Comment Utility
>>it behaves differently compared to the original
Whats the difference? What goes wrong?

Paul
0
 

Author Comment

by:Actia
Comment Utility
It does not receive response from the rig, so therefore something can't be right, but all i have done is convert api from 16bit to 32bit equivallents
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:Actia
Comment Utility
Does the actual conversion itself seem correct? The trouble is i am unable to debug the code, and the only testing I can do is offsite
0
 

Author Comment

by:Actia
Comment Utility
Heh problem was solved, the issue was due to

iError = GetCommState(hComm, &dcb);

          if (iError == 0)
          { ...

16bit GetCommError returns 0 if successfull and a non zero value if an error occurs.
32bit GetCommState returns 1 if successfull and a minus value if an error occurs!

The actual methods were never executed and would always return an Error, cos to be successfull it was expecting a value of 1.

Doh! Problem solved! :)
0
 
LVL 1

Accepted Solution

by:
GhostMod earned 0 total points
Comment Utility
PAQd, 250 points refunded.

GhostMod
Community Support Moderator
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now