Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2004-09-23
7
Medium Priority
?
401 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 22

Expert Comment

by:grg99
ID: 12132176
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
ID: 12132241
>>it behaves differently compared to the original
Whats the difference? What goes wrong?

Paul
0
 

Author Comment

by:Actia
ID: 12132384
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Actia
ID: 12158513
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
ID: 12481543
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
ID: 12519856
PAQd, 250 points refunded.

GhostMod
Community Support Moderator
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.

718 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