//
// The CommThread Function.
///執行緒函式
///監視執行緒的大致流程:
///檢查串口-->進入回圈{WaitCommEvent(不阻塞詢問)詢問事件-->如果有事件來到-->到相應處理(關閉\讀\寫)}
//
UINT CSerialPort::CommThread(LPVOID pParam)
{
// Cast the void pointer passed to the thread back to
// a pointer of CSerialPort class
///CSerialPort類的指標
CSerialPort *port = (CSerialPort*)pParam;
// Set the status variable in the dialog class to
// TRUE to indicate the thread is running.
///TRUE表示執行緒正在運行
port->m_bThreadAlive = TRUE;
.....
// Main wait function. This function will normally block the thread
// until one of nine events occur that require action.
///等待3個事件:關斷/讀/寫,有一個事件發生就回傳
Event = WaitForMultipleObjects(3, ///3個事件 我點擊發送 但Event一直是1 發送卡死了
port->m_hEventArray, ///事件陣列
FALSE, ///有一個事件發生就回傳
INFINITE);///超時時間
switch (Event)
{
case 0:
{
// Shutdown event. This is event zero so it will be
// the higest priority and be serviced first.
///關斷事件,關閉串口
CloseHandle(port->m_hComm);
port->m_hComm=NULL;
port->m_bThreadAlive = FALSE;
// Kill this thread. break is not needed, but makes me feel better.
AfxEndThread(100);
break;
}
case 1: /// read event將定義的各種訊息發送出去
{
memset(&comstat, 0, sizeof(COMSTAT));
GetCommMask(port->m_hComm, &CommEvent);
if (CommEvent & EV_RXCHAR)//接收到字符,并置于輸入緩沖區中
// Receive character event from port.
ReceiveChar(port, comstat);
/*if (CommEvent & EV_CTS)//CTS信號狀態發生變化
::SendMessage(port->m_pOwner->m_hWnd, WM_COMM_CTS_DETECTED, (WPARAM) 0, (LPARAM) port->m_nPortNr);*/
if (CommEvent & EV_BREAK)//輸入中發生中斷
::SendMessage(port->m_pOwner->m_hWnd, WM_COMM_BREAK_DETECTED, (WPARAM) 0, (LPARAM) port->m_nPortNr);
if (CommEvent & EV_ERR)//發生線路狀態錯誤,線路狀態錯誤包括CE_FRAME,CE_OVERRUN和CE_RXPARITY
::SendMessage(port->m_pOwner->m_hWnd, WM_COMM_ERR_DETECTED, (WPARAM) 0, (LPARAM) port->m_nPortNr);
if (CommEvent & EV_RING)//檢測到振鈴指示
::SendMessage(port->m_pOwner->m_hWnd, WM_COMM_RING_DETECTED, (WPARAM) 0, (LPARAM) port->m_nPortNr);
if (CommEvent & EV_RXFLAG)//接收到事件字符,并置于輸入緩沖區中
::SendMessage(port->m_pOwner->m_hWnd, WM_COMM_RXFLAG_DETECTED, (WPARAM) 0, (LPARAM) port->m_nPortNr);
break;
}
case 2: /// write event發送資料
{
// Write character event from port
WriteChar(port);
break;
}
}
求指教啊,我弄了好久了,都找不到原因,我一直在呼叫串口發送函式,不停的發送,點久了就發送不出去了!!!求大神只條明路啊。。。。。
uj5u.com熱心網友回復:
Multiple Threads in the User Interface http://msdn.microsoft.com/zh-cn/library/ms810439.aspxuj5u.com熱心網友回復:
真心求指教。。。還有誰 遇到過這個問題沒。。uj5u.com熱心網友回復:
SetEvent(m_hWriteEvent) 不能觸發WriteChar函式 真的 好急啊。。求高手 怎么弄的。。。uj5u.com熱心網友回復:
1.貼代碼的時候能不能用原始碼格式,看著累!2 SerialPort類 自己封裝的?
3 最好再仔細看看win32串口編程,還有樓主對多執行緒好像也不怎么理解。
uj5u.com熱心網友回復:
@tajon1226 能否加個QQ 353356315 謝謝 謝謝!!uj5u.com熱心網友回復:
http://wenku.baidu.com/link?url=DPQlrIq36d2iHoLUe_DSO1duhZFQuHTYBjH8iabQ79ENCj0bR0jP5EG2Ref3BTdAbfIZfbfs7CMr8W2aJtx4ly0yQBiRSVI-LalJO17cjZ3看會這個。
再看windows編程的互斥
uj5u.com熱心網友回復:
記得CodeProject有CSerialPort類,你到那個上面直接下載源代碼使用就可以了,不需要自己去做封裝了。uj5u.com熱心網友回復:
您這個問題后來如何解決的呢?轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/128906.html
標籤:基礎類
上一篇:引入msword.tlh檔案出錯
