⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tty.cpp

📁 一个用C++写的电力系统故障录波数据管理系统
💻 CPP
📖 第 1 页 / 共 2 页
字号:
               // force a paint
            }
         }
         while ( nLength > 0 ) ;
      }
   }

   // get rid of event handle

   CloseHandle( os.hEvent ) ;

   // clear information in structure (kind of a "we're done flag")

   THREADID( npTTYInfo ) = 0 ;
   HTHREAD( npTTYInfo ) = NULL ;

   return( TRUE ) ;

} // end of CommWatchProc()



//---------------------------------------------------------------------------
//  int NEAR ReadCommBlock( LPSTR lpszBlock, int nMaxLength )
//
//  Description:
//     Reads a block from the COM port and stuffs it into
//     the provided buffer.
//
//  Parameters:
//     HWND hWnd
//        handle to TTY window
//
//     LPSTR lpszBlock
//        block used for storage
//
//     int nMaxLength
//        max length of block to read
//
//  Win-32 Porting Issues:
//     - ReadComm() has been replaced by ReadFile() in Win-32.
//     - Overlapped I/O has been implemented.
//
//---------------------------------------------------------------------------

int NEAR ReadCommBlock( NPTTYINFO npTTYInfo, LPSTR lpszBlock, int nMaxLength )
{
   BOOL       fReadStat ;
   COMSTAT    ComStat ;
   DWORD      dwErrorFlags;
   DWORD      dwLength;
   DWORD      dwError;
   
   char       szError[ 10 ] ;

   if (NULL == npTTYInfo)
      return ( FALSE ) ;

   // only try to read number of bytes in queue
   ClearCommError( COMDEV( npTTYInfo ), &dwErrorFlags, &ComStat ) ;
   dwLength = min( (DWORD) nMaxLength, ComStat.cbInQue ) ;

   if (dwLength > 0)
   {
      fReadStat = ReadFile( COMDEV( npTTYInfo ), lpszBlock,
		                    dwLength, &dwLength, &READ_OS( npTTYInfo ) ) ;
      if (!fReadStat)
      {
         if (GetLastError() == ERROR_IO_PENDING)
         {
            OutputDebugString("\n\rIO Pending");
            // We have to wait for read to complete.
            // This function will timeout according to the
            // CommTimeOuts.ReadTotalTimeoutConstant variable
            // Every time it times out, check for port errors
            while(!GetOverlappedResult( COMDEV( npTTYInfo ),
               &READ_OS( npTTYInfo ), &dwLength, TRUE ))
            {
               dwError = GetLastError();
               if(dwError == ERROR_IO_INCOMPLETE)
                  // normal result if not finished
                  continue;
               else
               {
                  // an error occurred, try to recover
                  wsprintf( szError, "<CE-%u>", dwError ) ;
                  //WriteTTYBlock( npTTYInfo, szError, lstrlen( szError ) ) ;
                  ClearCommError( COMDEV( npTTYInfo ), &dwErrorFlags, &ComStat ) ;
                  if ((dwErrorFlags > 0) && DISPLAYERRORS( npTTYInfo ))
                  {
	                  wsprintf( szError, "<CE-%u>", dwErrorFlags ) ;
	                  //WriteTTYBlock(  npTTYInfo, szError, lstrlen( szError ) ) ;
                  }
                  break;
               }

            }

	      }
         else
         {
            // some other error occurred
            dwLength = 0 ;
            ClearCommError( COMDEV( npTTYInfo ), &dwErrorFlags, &ComStat ) ;
            if ((dwErrorFlags > 0) && DISPLAYERRORS( npTTYInfo ))
            {
	            wsprintf( szError, "<CE-%u>", dwErrorFlags ) ;
	            //WriteTTYBlock(  npTTYInfo, szError, lstrlen( szError ) ) ;
            }
         }
      }
   }

   return ( dwLength ) ;

} // end of ReadCommBlock()

//---------------------------------------------------------------------------
//  BOOL NEAR WriteCommBlock(  BYTE *pByte )
//
//  Description:
//     Writes a block of data to the COM port specified in the associated
//     TTY info structure.
//
//  Parameters:
//     
//     BYTE *pByte
//        pointer to data to write to port
//
//  Win-32 Porting Issues:
//     - WriteComm() has been replaced by WriteFile() in Win-32.
//     - Overlapped I/O has been implemented.
//
//---------------------------------------------------------------------------

BOOL NEAR WriteCommBlock( NPTTYINFO npTTYInfo, BYTE *lpByte , DWORD dwBytesToWrite)
{

   BOOL        fWriteStat ;
   DWORD       dwBytesWritten ;
   
   DWORD       dwErrorFlags;
   DWORD   	   dwError;
   DWORD       dwBytesSent=0;
   COMSTAT     ComStat;
   char        szError[ 128 ] ;

   if (NULL == npTTYInfo)
      return ( FALSE ) ;

   fWriteStat = WriteFile( COMDEV( npTTYInfo ), lpByte, dwBytesToWrite,
                           &dwBytesWritten, &WRITE_OS( npTTYInfo ) ) ;

   // Note that normally the code will not execute the following
   // because the driver caches write operations. Small I/O requests
   // (up to several thousand bytes) will normally be accepted
   // immediately and WriteFile will return true even though an
   // overlapped operation was specified

   if (!fWriteStat)
   {
      if(GetLastError() == ERROR_IO_PENDING)
      {
         // We should wait for the completion of the write operation
         // so we know if it worked or not

         // This is only one way to do this. It might be beneficial to
         // place the write operation in a separate thread
         // so that blocking on completion will not negatively
         // affect the responsiveness of the UI

         // If the write takes too long to complete, this
         // function will timeout according to the
         // CommTimeOuts.WriteTotalTimeoutMultiplier variable.
         // This code logs the timeout but does not retry
         // the write.

         while(!GetOverlappedResult( COMDEV( npTTYInfo ),
            &WRITE_OS( npTTYInfo ), &dwBytesWritten, TRUE ))
         {
            dwError = GetLastError();
            if(dwError == ERROR_IO_INCOMPLETE)
            {
               // normal result if not finished
               dwBytesSent += dwBytesWritten;
               continue;
            }
            else
            {
               // an error occurred, try to recover
               wsprintf( szError, "<CE-%u>", dwError ) ;
               //WriteTTYBlock(  npTTYInfo, szError, lstrlen( szError ) ) ;
               ClearCommError( COMDEV( npTTYInfo ), &dwErrorFlags, &ComStat ) ;
               if ((dwErrorFlags > 0) && DISPLAYERRORS( npTTYInfo ))
               {
                  wsprintf( szError, "<CE-%u>", dwErrorFlags ) ;
                  //WriteTTYBlock(  npTTYInfo, szError, lstrlen( szError ) ) ;
               }
               break;
            }
         }

         dwBytesSent += dwBytesWritten;

         if( dwBytesSent != dwBytesToWrite )
             wsprintf(szError,"\nProbable Write Timeout: Total of %ld bytes sent", dwBytesSent);
         else
             wsprintf(szError,"\n%ld bytes written", dwBytesSent);

         OutputDebugString(szError);

      }
      else
      {
         // some other error occurred
         ClearCommError( COMDEV( npTTYInfo ), &dwErrorFlags, &ComStat ) ;
         if ((dwErrorFlags > 0) && DISPLAYERRORS( npTTYInfo ))
         {
            wsprintf( szError, "<CE-%u>", dwErrorFlags ) ;
            //WriteTTYBlock(  npTTYInfo, szError, lstrlen( szError ) ) ;
         }
         return ( FALSE );
      }
   }
   return ( TRUE ) ;

} // end of WriteCommBlock()


//---------------------------------------------------------------------------
//  BOOL NEAR WriteTTYBlock( HWND hWnd, LPSTR lpBlock, int nLength )
//
//  Description:
//     Writes block to TTY screen.  Nothing fancy - just
//     straight TTY.
// gl
//  Parameters:
//     HWND hWnd
//        handle to TTY window
//
//     LPSTR lpBlock
//        far pointer to block of data
//
//     int nLength
//        length of block
//
//---------------------------------------------------------------------------
#define WM_MYMESSAGE WM_USER + 101

BOOL NEAR WriteTTYBlock( NPTTYINFO npTTYInfo, LPSTR lpBlock, int nLength )
{
   int  i ;
  /* int   WyWparam[2][7] = {
	   {
		RECE_SOH_FOR_START_OF_SEND,
		RECE_ACK_FOR_HEAD_OF_SEND,
		RECE_ACK_FOR_SOH_OF_SEND,
		RECE_ACK_FOR_BLOCK_OF_SEND,
		RECE_ACK_FOR_REQST_OF_SEND,
		RECE_512_REQST_OF_SEND,
		RECE_OTHER_OF_SEND
	   },
	   {
		RECE_ACK_FOR_START_OF_RECE,
		RECE_80_HEAD_OF_RECE,
		RECE_SOH_FOR_BLOCK_OF_RECE,
		RECE_4100BLOCK_OF_SEND,
		RECE_ACK_FOR_REQST_OF_RECE,
		RECE_512_REQST_OF_RECE,
		RECE_OTHER_OF_RECE
		}, 
   };*/

   if (NULL == npTTYInfo)
      return ( FALSE ) ;
   for(i=0; i<nLength; i++)
      {
       npTTYInfo->Queue[npTTYInfo->HeadPtr] = lpBlock[i];
       npTTYInfo->HeadPtr++;
       if(npTTYInfo->HeadPtr >= 5000)npTTYInfo->HeadPtr=0;
	   if(MyTrans.TransStatus == 0)
	     {
		   if(abs(npTTYInfo->HeadPtr - npTTYInfo->TailPtr) >= 512)
		      {
			    MyTrans.TransStatus = -1;
			    ::PostMessage(MyTrans.m_hWnd, WM_MYMESSAGE, 512, 0L);
			  }
		 } 
	   //if(abs(npTTYInfo->HeadPtr - npTTYInfo->TailPtr) >= MyTrans.TransFlag[MyTrans.SendOrRece][MyTrans.FLAG])
		 //  ::PostMessage(MyTrans.m_hWnd, WM_MYMESSAGE, WyWparam[MyTrans.SendOrRece][MyTrans.FLAG], 0L);
		//if( MyTrans.TransStatus == 0 && 
		//	abs(npTTYInfo->HeadPtr - npTTYInfo->TailPtr) >= 512)
     }
   return ( TRUE ) ;
  
} // end of WriteTTYBlock()



BOOL NEAR CTTY::DestroyTTYInfo(void)
{
   if (NULL == npTTYInfo)
      return ( FALSE ) ;

   // force connection closed (if not already closed)

   if (CONNECTED( npTTYInfo ))
       CloseConnection( ) ;

   // clean up event objects

   CloseHandle( READ_OS( npTTYInfo ).hEvent ) ;
   CloseHandle( WRITE_OS( npTTYInfo ).hEvent ) ;
   CloseHandle( POSTEVENT( npTTYInfo ) ) ;

   DeleteObject( HTTYFONT( npTTYInfo ) ) ;

   LocalFree( npTTYInfo ) ;
   return ( TRUE ) ;

} // end of DestroyTTYInfo()



//---------------------------------------------------------------------------
//  BOOL NEAR CloseConnection( HWND hWnd )
//
//  Description:
//     Closes the connection to the port.  Resets the connect flag
//     in the TTYINFO struct.
//
//  Parameters:
//     HWND hWnd
//        handle to TTY window
//
//  Win-32 Porting Issues:
//     - Needed to stop secondary thread.  SetCommMask() will signal the
//       WaitCommEvent() event and the thread will halt when the
//       CONNECTED() flag is clear.
//     - Use new PurgeComm() API to clear communications driver before
//       closing device.
//
//---------------------------------------------------------------------------

BOOL NEAR CTTY::CloseConnection( void )
{
   BYTE TranSerial[] = "+++";
   BYTE Hangup[] = "ATH0\r";   

   if (NULL == npTTYInfo)
      return ( FALSE ) ;

   // set connected flag to FALSE

   CONNECTED( npTTYInfo ) = FALSE ;
   if(!MyTrans.MissConnected())
   {
   if( CONNECTEDTO( npTTYInfo ) == TRUE)
     {
	   CONNECTEDTO( npTTYInfo ) = FALSE;
	   Sleep(1000);
       WriteCommBlock( MyTTY.npTTYInfo, TranSerial, lstrlen((char *)TranSerial));
       Sleep(1000);
       WriteCommBlock( MyTTY.npTTYInfo, Hangup, lstrlen((char *)Hangup));
	 }
   // disable event notification and wait for thread
   // to halt
   else 
     {       
	   Sleep(1000);
       WriteCommBlock( MyTTY.npTTYInfo, Hangup, lstrlen((char *)Hangup));
     }
   }
   SetCommMask( COMDEV( npTTYInfo ), 0 ) ;

   // block until thread has been halted

   while(THREADID(npTTYInfo) != 0);

   // kill the focus


   // drop DTR

   EscapeCommFunction( COMDEV( npTTYInfo ), CLRDTR ) ;

   // purge any outstanding reads/writes and close device handle

   PurgeComm( COMDEV( npTTYInfo ), PURGE_TXABORT | PURGE_RXABORT |
                                   PURGE_TXCLEAR | PURGE_RXCLEAR ) ;
   CloseHandle( COMDEV( npTTYInfo ) ) ;

   // change the selectable items in the menu

   return ( TRUE ) ;

} // end of CloseConnection()


/////// CloseDialerProc
/*DWORD FAR PASCAL CloseDialerProc( LPVOID lpData )
{

   while(MyTTY.npTTYInfo->HeadPtr < 1)
     { Sleep(5); }

   MMyDialing.EndDialog(FALSE);

   return ( TRUE ) ;
}*/

/*
COMMCONFIG cc;
   cc.dwSize = sizeof(COMMCONFIG);
   cc.wVersion = 0x100;
   cc.dcb.BaudRate = 19200;
   if(!CommConfigDialog("COM2", NULL, &cc))
   {
	AfxMessageBox("error");return FALSE;
   }*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -