• C

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

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;
}
ActiaAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
GhostModConnect With a Mentor Commented:
PAQd, 250 points refunded.

GhostMod
Community Support Moderator
0
 
grg99Commented:
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
 
PaulCaswellCommented:
>>it behaves differently compared to the original
Whats the difference? What goes wrong?

Paul
0
Get 10% Off Your First Squarespace Website

Ready to showcase your work, publish content or promote your business online? With Squarespace’s award-winning templates and 24/7 customer service, getting started is simple. Head to Squarespace.com and use offer code ‘EXPERTS’ to get 10% off your first purchase.

 
ActiaAuthor Commented:
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
 
ActiaAuthor Commented:
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
 
ActiaAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.