LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

RS-232 speed different on console vs. window

Solved!
Go to solution

I have a simple application that I made to test the data rate of a device.  I was having trouble with slow communications with another program, so I wrote this one to test just the RS-232 communications. My problem is that I get vastly different result depending upon whether I compile the program as a console application versus a windowed application.

 

My device is sending 70-byte message packets at 100Hz using 460800 baud, 8-N-1.  I'm using a USB to Serial adapter, which is supposed to support that baud rate.  Using the console application I get ~7000 bytes/second, which is what I expect.  However, when running as a windowed application,  I get ~34 bytes/second.  Anyone have any ideas about why there is such a vast difference?  (CVI 2013 on Win7)

 

Code is below.  Inputs I use are:  [COM port number], 460800, 1.

#include <utility.h>
#include <ansi_c.h>
#include <rs232.h>


int main ()
{                  
   FILE * outFile ;
   FILE * binFile ;
   int iNumBytes = 0 ;
   char cMessageArray [720] ;
   int iStatus ;
   int iPortNum ;
   long lBaud ;
   int iByteRead ;
   int iGetRate ;
   char cFileName [20] ;
   time_t timeStart ;
   time_t timeStop ;
   long lByteCount = 0 ;
   long lTimeoutCount = 0 ;
   long lNumSeconds = 0 ;         
   
   
   printf ( "Port recorder.  \nWrites bytes received on the selected port to a file ( {Port #}.txt ).\n\nEnter COM port: " ) ;
   scanf ( "%i", &iPortNum ) ;
   printf ( "\nEnter baud rate: " ) ;
   scanf ( "%u", &lBaud ) ;
   printf ( "\nEnter 1 to just calculate byte rate: " ) ;
   scanf ( "%i", &iGetRate ) ;

   printf ( "\nListening on port %i at baud rate %i (8-N-1) w/half-second timeout.  Press any key to stop.\n", iPortNum, lBaud ) ;

   DisableBreakOnLibraryErrors() ;
   
   iStatus = OpenComConfig( iPortNum, "", lBaud, 0, 8, 1, 8192, 1024 ) ;
   SetComTime ( iPortNum, 0.5 ) ; // COM read timeout in seconds

   if ( 0 <= iStatus )
   {
      // Open output files
      sprintf ( cFileName, "%u.txt", iPortNum ) ;
      outFile = fopen ( cFileName , "wt" ) ;
      sprintf ( cFileName, "%u.bin", iPortNum ) ;
      binFile = fopen ( cFileName , "wb" ) ;
      
      time ( &timeStart ) ;

      if ( 1 != iGetRate )
      {
         while ( !KeyHit() )
         {
            iByteRead = ComRdByte( iPortNum ) ;
         
            if ( -99 != iByteRead ) // IF not a timeout
            {
               iByteRead = iByteRead & 0x00FF ;
               fprintf( outFile, "%.2X ", iByteRead ) ;
               printf( "%.2X ", iByteRead ) ;
               cMessageArray[ iNumBytes ] = (char)( iByteRead & 0x00FF );
               iNumBytes++ ;
               lByteCount++ ;
            
               if ( 70 == iNumBytes )
               {  
                  fwrite ( cMessageArray, 1, 70, binFile ) ;
                  iNumBytes = 0 ;
                  printf( "\n" ) ; 
               }
            }
            else 
            {
               fprintf( outFile, "TIMEOUT " ) ;
               printf( "TIMEOUT " ) ;
               lTimeoutCount++ ;
            }  
         }
      }
      else
      {
         while ( !KeyHit() )
         {
            iByteRead = ComRdByte( iPortNum ) ;
         
            if ( -99 != iByteRead ) // IF not a timeout
            { 
               lByteCount++ ;
            }   
            else
            {
               lTimeoutCount++ ;
            }
         }
      }
      
      time ( &timeStop ) ;
      lNumSeconds = timeStop - timeStart ;
      
      printf ( "Bytes = %i, Seconds = %i, Bytes/Sec. = %f\nTimeouts = %i\n", lByteCount, lNumSeconds, (float)( (float) lByteCount / (float) lNumSeconds), lTimeoutCount );

      fflush( outFile ) ;
      fclose ( outFile ) ;
      CloseCom( iPortNum ) ;
   }
   else
   {
      printf ( "Couldn't open COM port!" ) ;
   }
   
   printf ( "\nPress any key to close.\n" ) ;
   
   GetKey() ; 
   
   while ( !KeyHit() )
   {}
   
   return 0 ;
}

Thanks!

0 Kudos
Message 1 of 2
(2,933 Views)
Solution
Accepted by topic author S2rt

Your GUI program may be affected by CVI Sleep Policy set in Environment options.

I seem to remember that the default value for this parameter is Sleep More, which means CVI grants very much time to the OS to serve other tasks/programs. You may want to try with Sleep Some or Do Not Sleep; in the latter case be sure to insert some ProcessSystemEvents somewhere in your application to permit honouring UI events otherwise you won't be able to exit the loop.

The same option can be set programmatically by calling SetSleepPolicy ()



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
Message 2 of 2
(2,922 Views)