亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關于我們
? 蟲蟲下載站

?? vcthread.cpp

?? c++實現的一個個串口通訊類
?? CPP
?? 第 1 頁 / 共 2 頁
字號:
///////////////////////////////////////////////////////////////
// 文件名: ApexComm.cpp
// 功能:注冊控件類
// 作者:陳++
// 時間:2004.4.15-1999.4.22 創建
//       2004.5.15修改,整理
///////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "vcThread.h"
#include "define.h"

IMPLEMENT_DYNAMIC(TReadThread, CWinThread)

////////////////////////////////////////////////////////
//  過程: TReadThread.Thread
//
//  目的: 讀線程運行并執行相應的功能.
//
//  參數:
//    None.
//
//  返回值:
//    None.
//
//  注釋:
//
//    The Read Thread uses overlapped ReadFile and sends any data
//    read from the comm port to the Comm32Window.  This is
//    eventually done through a PostMessage so that the Read Thread
//    is never away from the comm port very long.  This also provides
//    natural desynchronization between the Read thread and the UI.
//
//    If the CloseEvent object is signaled, the Read Thread returns.
//
//        Separating the Read and Write threads is natural for a application
//    where there is no need for synchronization between
//    reading and writing.  However, if there is such a need (for example,
//    most file transfer algorithms synchronize the reading and writing),
//     it would make a lot more sense to have a single thread to handle
//    both reading and writing.
//

TReadThread::TReadThread()
{

}

BOOL TReadThread::InitInstance()
{
   char szInputBuffer[TINPUTBUFFERSIZE];
   DWORD nNumberOfBytesRead;
   HANDLE HandlesToWaitFor[3];
   DWORD dwHandleSignaled;
   DWORD fdwEvtMask;
   // Needed for overlapped I/O (ReadFile)
   OVERLAPPED overlappedRead;
   // Needed for overlapped Comm Event handling.
   OVERLAPPED overlappedCommEvent;
   memset( &overlappedRead,0, sizeof(overlappedRead));
   memset( &overlappedCommEvent,0, sizeof(overlappedCommEvent));
   // Lets put an event in the Read overlapped structure.
   overlappedRead.hEvent = CreateEvent( NULL, true, false, NULL);
   if (overlappedRead.hEvent == 0 )
   {
      PostHangupCall();
      goto EndReadThread;
   }
   ResetEvent(overlappedRead.hEvent);
   // And an event for the CommEvent overlapped structure.
   overlappedCommEvent.hEvent = CreateEvent( NULL, true, false, NULL);
   if (overlappedCommEvent.hEvent == 0 )
   {
      PostHangupCall();
      goto EndReadThread;
   }
   ResetEvent(overlappedCommEvent.hEvent);
   // We will be waiting on these objects.
   HandlesToWaitFor[0] = m_hCloseEvent;
   HandlesToWaitFor[1] = overlappedCommEvent.hEvent;
   HandlesToWaitFor[2] = overlappedRead.hEvent;
   // Setup CommEvent handling.
   // Set the comm mask so we receive error signals.
   if (! SetCommMask(m_hCommFile, EV_ERR | EV_RLSD | EV_RING | EV_CTS | EV_DSR)) 
   {
      PostHangupCall();
      goto EndReadThread;
   }

   // Start waiting for CommEvents (Errors)
   if (! SetupCommEvent( &overlappedCommEvent,  fdwEvtMask ) )
      goto EndReadThread;

   // Start waiting for Read events.
   if (! SetupReadEvent( &overlappedRead,
      szInputBuffer, TINPUTBUFFERSIZE,
      nNumberOfBytesRead ) )
      goto EndReadThread;
   // Keep looping until we break out.
   while (true)
   {
      // Wait until some event occurs (data to read; error; stopping).
      dwHandleSignaled = WaitForMultipleObjects(3, HandlesToWaitFor,false, INFINITE);
      // Which event occured?
      switch (dwHandleSignaled) 
      {
      case  WAIT_OBJECT_0:     // Signal to end the thread.
         // Time to return.
         goto EndReadThread;         
      case WAIT_OBJECT_0 + 1: // CommEvent signaled.
         // Handle the CommEvent.
         if (! HandleCommEvent( &overlappedCommEvent, fdwEvtMask, true ) )
            goto EndReadThread;
         // Start waiting for the next CommEvent.
         if (! SetupCommEvent( &overlappedCommEvent, fdwEvtMask ) )
            goto EndReadThread;
         break;
      case WAIT_OBJECT_0 + 2: // Read Event signaled.
         // Get the new data!
         if (! HandleReadEvent( &overlappedRead,szInputBuffer,
            TINPUTBUFFERSIZE,nNumberOfBytesRead )) 
            goto EndReadThread;
         // Wait for more new data.
         if (! SetupReadEvent( &overlappedRead,szInputBuffer, TINPUTBUFFERSIZE,
            nNumberOfBytesRead ) )
            goto EndReadThread;
         break;
      case WAIT_FAILED:       // Wait failed.  Shouldn't happen.
         PostHangupCall();
         goto EndReadThread;         
      default:    // This case should never occur.
         PostHangupCall();
         goto EndReadThread;
      } //{case dwHandleSignaled}
   } //{while True}
   // Time to clean up Read Thread.
EndReadThread:
   PurgeComm( m_hCommFile, PURGE_RXABORT + PURGE_RXCLEAR );
   CloseHandle( overlappedRead.hEvent );
   CloseHandle( overlappedCommEvent.hEvent );
   return false;
}


///////////////////////////////////////////////////////
//讀線程類提供的方法
//
//  函數: SetupCommEvent(LPOVERLAPPED, LPDWORD)
//
//  目的: Sets up the overlapped WaitCommEvent call.
//
//  參數:
//    lpOverlappedCommEvent - Pointer to the overlapped structure to use.
//    lpfdwEvtMask          - Pointer to DWORD to received Event data.
//
//  返回值:
//    TRUE if able to successfully setup the WaitCommEvent.
//    FALSE if unable to setup WaitCommEvent, unable to handle
//    an existing outstanding event or if the CloseEvent has been signaled.
//
//  注釋:
//
//    This function is a helper function for the Read Thread that sets up
//    the WaitCommEvent so we can deal with comm events (like Comm errors)
//    if they occur.
//

bool TReadThread::SetupCommEvent( OVERLAPPED* lpOverlappedCommEvent,DWORD & lpfdwEvtMask)
{
   DWORD dwLastError;
   //Result = false;

StartSetupCommEvent:   
   //確定關閉事件還沒有被啟動,查看它的目的是為了防止關閉事件遞歸調用.
   //當返回值不為WAIT_TIMEOUT時表明關閉事件已被啟動
   if (WAIT_TIMEOUT != WaitForSingleObject( m_hCloseEvent,0 ) )
      return false;
   // Start waiting for Comm Errors.
   if (WaitCommEvent( m_hCommFile, &lpfdwEvtMask, lpOverlappedCommEvent ) )
   {
      // This could happen if there was an error waiting on the
      // comm port.  Lets try and handle it.
      if (! HandleCommEvent( NULL, lpfdwEvtMask, false ) )
      {
         //{??? GetOverlappedResult does not handle "NULL" as defined by Borland}
         return false;
      }
      // What could cause infinite recursion at this point?
      goto StartSetupCommEvent;
   }

   // We expect ERROR_IO_PENDING returned from WaitCommEvent
   // because we are waiting with an overlapped structure.
   dwLastError = GetLastError();
   // LastError was ERROR_IO_PENDING, as expected.
   if (dwLastError == ERROR_IO_PENDING)    
      return true;
   
   // Its possible for this error to occur if the
   // service provider has closed the port.  Time to end.
   if (dwLastError == ERROR_INVALID_HANDLE )
      return false;
   // Unexpected error. No idea what could cause this to happen.
   PostHangupCall();
   return false;
}//{TReadThread.SetupCommEvent}


//////////////////////////////////////////////////////////
//  函數: SetupReadEvent(LPOVERLAPPED, LPSTR, DWORD, LPDWORD)
//
//  目的: Sets up an overlapped ReadFile
//
//  參數:
//    lpOverlappedRead      - address of overlapped structure to use.
//    lpszInputBuffer       - Buffer to place incoming bytes.
//    dwSizeofBuffer        - size of lpszInputBuffer.
//    lpnNumberOfBytesRead  - address of DWORD to place the number of read bytes.
//
//  返回值:
//    TRUE if able to successfully setup the ReadFile.  FALSE if there
//    was a failure setting up or if the CloseEvent object was signaled.
//
//  注釋:
//
//    This function is a helper function for the Read Thread.  This
//    function sets up the overlapped ReadFile so that it can later
//    be waited on (or more appropriatly, so the event in the overlapped
//    structure can be waited upon).  If there is data waiting, it is
//    handled and the next ReadFile is initiated.
//    Another possible reason for returning FALSE is if the comm port
//    is closed by the service provider.
//
//

bool TReadThread::SetupReadEvent( OVERLAPPED* lpOverlappedRead,LPSTR lpszInputBuffer,DWORD dwSizeofBuffer,DWORD & lpnNumberOfBytesRead)
{
   DWORD dwLastError;
   //Result = False;
StartSetupReadEvent:
   // Make sure the CloseEvent hasn't been signaled yet.
   // Check is needed because this function is potentially recursive.
   if (WAIT_TIMEOUT != WaitForSingleObject(m_hCloseEvent,0) )
      return false;
   // Start the overlapped ReadFile.
   if (ReadFile( m_hCommFile,
      lpszInputBuffer, dwSizeofBuffer,
      &lpnNumberOfBytesRead, lpOverlappedRead ) )
   {
      // This would only happen if there was data waiting to be read.
      // Handle the data.
      if (! HandleReadData( lpszInputBuffer, lpnNumberOfBytesRead ) )
         return false;
      // Start waiting for more data.
      goto StartSetupReadEvent;
   }
   // ReadFile failed.  Expected because of overlapped I/O.
   dwLastError = GetLastError();
   // LastError was ERROR_IO_PENDING, as expected.
   
   if (dwLastError == ERROR_IO_PENDING )   
      return true;
   
   // Its possible for this error to occur if the
   // service provider has closed the port.  Time to end.
   if (dwLastError == ERROR_INVALID_HANDLE )
      return false;
   // Unexpected error come here. No idea what could cause this to happen.
   PostHangupCall();
   return false;
}//{TReadThread.SetupReadEvent}


///////////////////////////////////////////////////////
//  函數: HandleCommEvent(LPOVERLAPPED, LPDWORD, BOOL)
//
//  目的: 處理標準的通訊事件.
//
//  參數:
//    lpOverlappedCommEvent - Pointer to the overlapped structure to use.
//    lpfdwEvtMask          - Pointer to DWORD to received Event data.
//    fRetrieveEvent        - Flag to signal if the event needs to be
//                            retrieved, or has already been retrieved.
//
//  返回值:
//    TRUE if able to handle a Comm Event.
//    FALSE if unable to setup WaitCommEvent, unable to handle
//    an existing outstanding event or if the CloseEvent has been signaled.
//
//  注釋:
//
//    This function is a helper function for the Read Thread that (if
//    fRetrieveEvent == TRUE) retrieves an outstanding CommEvent and
//    deals with it.  The only event that should occur is an EV_ERR event,
//    signalling that there has been an error on the comm port.
//
//    Normally, comm errors would not be put into the normal data stream
//    as this sample is demonstrating.  Putting it in a status bar would
//    be more appropriate for a real application.
//

bool TReadThread::HandleCommEvent(OVERLAPPED* lpOverlappedCommEvent,DWORD & lpfdwEvtMask,bool fRetrieveEvent)
{
   DWORD dwDummy;
   DWORD dwErrors;
   DWORD dwLastError;
   // If this fails, it could be because the file was closed (and I/O is
   // finished) or because the overlapped I/O is still in progress.  In
   // either case (or any others) its a bug and return FALSE.
   if (fRetrieveEvent) 
   {
      if (! GetOverlappedResult( m_hCommFile,lpOverlappedCommEvent, &dwDummy, false))
      {
         dwLastError = GetLastError();
         // Its possible for this error to occur if the
         // service provider has closed the port.  Time to end.
         if (dwLastError == ERROR_INVALID_HANDLE )
            return false;
         PostHangupCall();//ERROR_IO_INCOMPLETE
         return false;
      }
   }
   // Was the event an error? EV_ERR 0x80
   if ((lpfdwEvtMask && EV_ERR) != 0 )
   {
      // Which error was it?
      if (! ClearCommError( m_hCommFile, &dwErrors, NULL ) )
      {
         dwLastError = GetLastError();
         // Its possible for this error to occur if the
         // service provider has closed the port.  Time to end.
         if (dwLastError == ERROR_INVALID_HANDLE )
            return false;
         PostHangupCall ();
         return false;

?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
久久综合色综合88| 欧美在线一二三四区| 久久久久久久性| 国产一区二区三区久久悠悠色av| 日韩精品一区二| 福利91精品一区二区三区| 18欧美乱大交hd1984| 欧美无砖专区一中文字| 免费日韩伦理电影| 久久久不卡网国产精品一区| 岛国av在线一区| 亚洲美女在线国产| 日韩色视频在线观看| 国产乱人伦偷精品视频免下载| 中文无字幕一区二区三区| 一本色道综合亚洲| 蜜桃av噜噜一区二区三区小说| 久久免费美女视频| 91精品1区2区| 激情都市一区二区| 亚洲男人的天堂一区二区| 欧美精品一二三| 国产成人免费视频一区| 亚洲午夜精品网| 久久久久久久久久久久久女国产乱 | 国产欧美精品国产国产专区 | 日韩小视频在线观看专区| 国产成人精品一区二| 亚洲成人动漫av| 国产亚洲精品bt天堂精选| 欧美日韩一二区| 国产mv日韩mv欧美| 日韩不卡一区二区三区| 中文av一区特黄| 欧美成人a视频| 欧美伊人久久大香线蕉综合69| 国产乱妇无码大片在线观看| 午夜av一区二区| 亚洲欧洲无码一区二区三区| www成人在线观看| 欧美图区在线视频| 成人国产电影网| 精品影院一区二区久久久| 亚洲一区二区综合| 中文字幕制服丝袜成人av| 欧美成人a视频| 欧美肥胖老妇做爰| 91久久一区二区| 成人h动漫精品一区二区| 美女视频黄 久久| 午夜久久久影院| 亚洲在线中文字幕| 国产精品视频线看| 精品福利一区二区三区免费视频| 精品视频在线看| 91美女蜜桃在线| www.成人在线| 国产成人av福利| 国产精品一卡二卡| 国产一区中文字幕| 美腿丝袜亚洲三区| 亚洲国产美女搞黄色| 亚洲视频1区2区| 中文字幕日本不卡| 国产精品久久久久天堂| 国产色婷婷亚洲99精品小说| 亚洲精品一区二区三区蜜桃下载| 欧美卡1卡2卡| 6080yy午夜一二三区久久| 欧美性猛交一区二区三区精品 | 国产精品一区二区三区四区| 麻豆精品在线看| 蜜桃91丨九色丨蝌蚪91桃色| 日韩高清不卡一区二区三区| 日日骚欧美日韩| 视频在线在亚洲| 久久99国产精品免费| 七七婷婷婷婷精品国产| 日本中文字幕一区二区有限公司| 日韩av中文在线观看| 蜜桃视频第一区免费观看| 久久91精品久久久久久秒播| 国产自产2019最新不卡| 国产成人精品aa毛片| 不卡电影一区二区三区| 91丨九色丨国产丨porny| 色婷婷综合久久久中文一区二区 | 日韩一级视频免费观看在线| 日韩三级伦理片妻子的秘密按摩| 日韩免费一区二区| 国产日韩欧美高清在线| 国产精品国产a| 亚洲一区二区欧美日韩| 蜜臀国产一区二区三区在线播放| 久草精品在线观看| aaa欧美色吧激情视频| 在线精品视频小说1| 欧美一区二区免费| 国产三级一区二区| 亚洲精品视频在线观看免费| 全部av―极品视觉盛宴亚洲| 国产剧情一区二区| av高清不卡在线| 欧美日韩不卡视频| www亚洲一区| 亚洲久本草在线中文字幕| 日韩精品视频网| 丁香亚洲综合激情啪啪综合| 99久久久免费精品国产一区二区| 欧美色国产精品| 久久精品亚洲一区二区三区浴池| 亚洲女与黑人做爰| 另类的小说在线视频另类成人小视频在线 | 蜜臀av性久久久久av蜜臀妖精| 国产成人av一区二区| 欧美日韩一级二级| 中文字幕精品—区二区四季| 亚洲电影视频在线| 成人午夜又粗又硬又大| 欧美日本精品一区二区三区| 国产婷婷色一区二区三区在线| 亚洲一区二区成人在线观看| 国产一区二区三区精品视频| 在线一区二区视频| 久久久不卡影院| 蜜臀av性久久久久蜜臀aⅴ流畅| 99r国产精品| 久久久久久久综合| 丝瓜av网站精品一区二区| 99久久精品国产网站| 精品88久久久久88久久久| 一区二区成人在线| 国产91在线看| 精品日韩在线一区| 亚洲电影中文字幕在线观看| 99精品久久99久久久久| 久久新电视剧免费观看| 香蕉久久夜色精品国产使用方法 | 欧美日韩国产综合一区二区三区 | 国产精品传媒视频| 激情综合网天天干| 欧美精品色综合| 亚洲一二三四久久| 91日韩一区二区三区| 国产欧美日韩三级| 国产一区二区免费视频| 欧美一区二区在线播放| 亚洲二区在线视频| 91福利区一区二区三区| 中文字幕在线一区免费| 成人黄色在线网站| 国产午夜亚洲精品不卡| 久久er精品视频| 日韩欧美国产一二三区| 婷婷一区二区三区| 欧美日韩卡一卡二| 天堂一区二区在线| 欧美日韩一级片在线观看| 亚洲一区二区av在线| 欧美性生活大片视频| 亚洲国产毛片aaaaa无费看| 在线精品视频免费观看| 一区二区三区精品在线| 91黄色免费看| 亚洲一区二区影院| 精品视频一区二区不卡| 亚洲一二三四在线| 在线播放国产精品二区一二区四区 | 国产精品一区二区不卡| 久久久久久久久久久久电影| 国产成人鲁色资源国产91色综| 中文字幕乱码亚洲精品一区| 成人免费看黄yyy456| 中文字幕综合网| 在线视频你懂得一区二区三区| 亚洲国产毛片aaaaa无费看 | 欧美一级电影网站| 蜜桃一区二区三区在线观看| 精品国产污网站| 成人性视频免费网站| 亚洲色图20p| 91精品国产日韩91久久久久久| 麻豆91在线播放| 久久精品亚洲精品国产欧美 | 精品视频1区2区| 美女尤物国产一区| 久久久久久久久久看片| hitomi一区二区三区精品| 亚洲一区二区三区四区在线 | 3atv在线一区二区三区| 蜜桃传媒麻豆第一区在线观看| 久久久激情视频| 色综合夜色一区| 久久综合综合久久综合| 中文字幕一区二区三区视频| 欧美日韩一区三区| 国产成人欧美日韩在线电影| 亚洲午夜精品久久久久久久久| 精品国产乱码久久久久久免费 | 99久久免费国产|