FabulaTech Forum (http://www.usb-over-network.com/forum)
Serial Port Solution >> Network Serial Port Kit >> Issue with NSP
(Message started by: random0000 on Feb 14th, 2006, 3:44pm)

Title: Issue with NSP
Post by random0000 on Feb 14th, 2006, 3:44pm
We recently purchased this software and are very impressed.  However we are running into an issue where existing code works fine over normal serial cables but not using the software.  

The code was written in c++ of which I am barely familiar with but I will post below.  Essentially we are sending a single byte ACK in response to a previous communication.  The below code loops indefinitely at the WaitForMultipleObjects.  Again this code works fine from a "real" serial port.

bool RWPort::output_worker(BYTE c, RWPort *port)
{
 OVERLAPPED AsyncWriteInfo = { 0 };
 AsyncWriteInfo.hEvent = CreateEvent( NULL, true, false, NULL );
 assert( AsyncWriteInfo.hEvent );
 HANDLE handles[ 2 ] = { port->m_hKillInputThreadEvent,
                         AsyncWriteInfo.hEvent };
 bool running = true;
 for ( bool done = false ; NOT done ; )

{
   DWORD result_count = 0;
   if (NOT WriteFile( port->m_hPort, &c, 1, &result_count, &AsyncWriteInfo ) )


{
     if ( GetLastError() EQ ERROR_IO_PENDING )



{
       switch ( WaitForMultipleObjects( 2, handles, false, INFINITE ) )




{
         case 0 : //m_hKillOutputThreadEvent
           done = true;
           running = false;
           PurgeComm( port->m_hPort, PURGE_TXABORT );
           break;
         case 1 : //AsyncWriteInfo.hEvent
           if ( NOT GetOverlappedResult( port->m_hPort, &AsyncWriteInfo, &result_count,







                           false ) OR result_count NE 1 )






{
             if ( GetLastError() EQ ERROR_IO_PENDING )
               port->clear_error();
             else
               port->translate_last_error();
             done = true;                        
           }
           break;
         default :
           assert( false );
       }
     }



else



{
       port->translate_last_error();
       done = true;                        
     }
   }


else


{
     if ( result_count NE 1 )
       port->translate_last_error();
     done = true;
   }
 }
 //
 // On the way out, close the event handle
 //
 CloseHandle( AsyncWriteInfo.hEvent );
 return running;
}

Title: Re: Issue with NSP
Post by Andrew on Feb 15th, 2006, 1:50am
There is error in your below code:

   case 1 : //AsyncWriteInfo.hEvent
 if ( NOT GetOverlappedResult( port->m_hPort, &AsyncWriteInfo, &result_count,
       false ) OR result_count NE 1 )  
{
   if ( GetLastError() EQ ERROR_IO_PENDING )
     port->clear_error();
   else
     port->translate_last_error();
   done = true;    
 }  
 break;

In your code fragment, flag "done" will not set and loop will not be breaked if GetOverlappedResult() function returns TRUE and "result_count" value will be 1. So correct code has to be:

case 1 : //AsyncWriteInfo.hEvent
if ( NOT GetOverlappedResult( port->m_hPort, &AsyncWriteInfo, &result_count,
      false ) OR result_count NE 1 )  
{
  if ( GetLastError() EQ ERROR_IO_PENDING )
    port->clear_error();
  else
    port->translate_last_error();
}  
  done = true;     // <------------------------
break;