//The CommThread Function.
///thread function
///monitor thread process roughly:
///check the serial port - & gt; Into the loop {WaitCommEvent (not blocking to ask) about events - & gt; If there are events come -- & gt; To the corresponding processing (closed, read, write)}
//
UINT CSerialPort: : CommThread (LPVOID pParam)
{
//Cast the void pointer passed to the thread back to
//a pointer of CSerialPort class
///CSerialPort class pointer
PParam CSerialPort * port=(CSerialPort *);
//Set the status variable in the dialog class to
//TRUE to indicate the thread is running.
///TRUE said thread running
The port - & gt; M_bThreadAlive=TRUE;
.
//the Main wait function. This function will normally block the thread
//until one of nine events occur that require action.
///waiting for the three events: shut off/read/write, there is an incident would return
Event=WaitForMultipleObjects (3,///three events I click send but the Event has been sending card 1 dead
The port - & gt; M_hEventArray,///events array
FALSE,///one incident is returned
INFINITE);///timeout
The switch (Event)
{
Case 0:
{
//the Shutdown event. This is the event zero so it will be
//the higest priority and be serviced first.
///shut off event, close a serial port
The CloseHandle (port - & gt; M_hComm);
The port - & gt; M_hComm=NULL;
The port - & gt; M_bThreadAlive=FALSE;
//Kill this thread. The break is not men, but top service me feel better.
AfxEndThread (100);
break;
}
Case 1:///read event will define all kinds of messages are sent out
{
Memset (& amp; Comstat, 0, sizeof (comstat));
GetCommMask (port - & gt; M_hComm, & amp; CommEvent);
If (CommEvent & amp; EV_RXCHAR)//receive the characters and the input buffer
//the Receive character event from the port.
ReceiveChar (port, comstat);
/* if (CommEvent & amp; EV_CTS)//CTS signal state change
: : SendMessage (port - & gt; M_pOwner - & gt; M_hWnd, WM_COMM_CTS_DETECTED, (WPARAM) 0, (LPARAM) port - & gt; M_nPortNr); */
If (CommEvent & amp; EV_BREAK)//a disruption in the input
: : SendMessage (port - & gt; M_pOwner - & gt; M_hWnd, WM_COMM_BREAK_DETECTED, (WPARAM) 0, (LPARAM) port - & gt; M_nPortNr);
If (CommEvent & amp; EV_ERR)//line state error, line state errors include CE_FRAME, CE_OVERRUN and CE_RXPARITY
: : SendMessage (port - & gt; M_pOwner - & gt; M_hWnd, WM_COMM_ERR_DETECTED, (WPARAM) 0, (LPARAM) port - & gt; M_nPortNr);
If (CommEvent & amp; EV_RING)//detect ringing indicating
: : SendMessage (port - & gt; M_pOwner - & gt; M_hWnd, WM_COMM_RING_DETECTED, (WPARAM) 0, (LPARAM) port - & gt; M_nPortNr);
If (CommEvent & amp; EV_RXFLAG)//receives the event character, and the input buffer
: : SendMessage (port - & gt; M_pOwner - & gt; M_hWnd, WM_COMM_RXFLAG_DETECTED, (WPARAM) 0, (LPARAM) port - & gt; M_nPortNr);
break;
}
Case 2:///write the event to send data
{
//Write character event from the port
WriteChar (port);
break;
}
}
Please teach ah, I got a long time, can't find the reason, I have been send function calls a serial port, send, point for a long time can't send out!!!!!! O great god only way ah,,,,,,,
CodePudding user response:
Multiple Threads in the User Interface of http://msdn.microsoft.com/zh-cn/library/ms810439.aspxCodePudding user response:
To sincerely seek counsel, and who else had this problem,,CodePudding user response:
SetEvent (m_hWriteEvent) really good ah, can't trigger WriteChar function, to master how to make,,,CodePudding user response:
1. Post code can't use the source code format, looked tired!2 SerialPort class encapsulated by yourself?
3'd better take a closer look at the win32 serial port programming, as well as the original poster for multithreaded seems to also don't understand,
CodePudding user response:
@ tajon1226 can you add a QQ 353356315 you thank you thank you!!!!CodePudding user response:
http://wenku.baidu.com/link? Url=DPQlrIq36d2iHoLUe_DSO1duhZFQuHTYBjH8iabQ79ENCj0bR0jP5EG2Ref3BTdAbfIZfbfs7CMr8W2aJtx4ly0yQBiRSVI - LalJO17cjZ3Watch this,
Look at the Windows programming mutex
CodePudding user response:
Remember CodeProject CSerialPort class, you to the one directly download the source code to use on it is ok, don't need to do packaging,CodePudding user response:
This problem you later how to solve?