I am a freshman small white recently in self-study using MFC API function to write a serial port communication, and mainly is the realization of an electronic weighing system, I did out in synchronous operation way (because it's easy), but the use of asynchronous do not to come out, the main reason is: to use multithreading knowledge, but after check the information on the Internet or don't understand the concept,,
The problem is mainly how to write a thread function?? Thread function should be given where the CPP?? Read and write the following code is my serial code (haven't join the thread)
But there have been a problem: the fourth parameter writefile function returns the actual number of bytes to write. But I always returns 0 is how to return a responsibility,,
BOOL write () { X55 char buffer [6]={0, 0 x11, 0 x00 to 0 x00 to 0 x11}; The COMSTAT COMSTAT; DWORD dwRetwrite=0; DWORD dwErrorFlags; OVERLAPPED m_osWrite; BOOL bWriteStat; Memset (& amp; M_osWrite, 0, sizeof (m_osWrite)); The ClearCommError (hcom, & amp; DwErrorFlags, & amp; The ComStat); PurgeComm (hcom, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR); BWriteStat=WriteFile (hcom, buffer, 5, & amp; DwRetwrite, & amp; M_osWrite); if(! BWriteStat) { If (GetLastError ()==ERROR_IO_PENDING) WaitForSingleObject (m_osWrite hEvent, INFINITE); The else Return FALSE; } Return TRUE; } BOOL read () { Char inBuffer [6]={0}; DWORD dwRetread=0; OVERLAPPED m_osRead; Memset (& amp; M_osRead, 0, sizeof (OVERLAPPED)); M_osRead. HEvent=CreateEvent (NULL, TRUE, FALSE, NULL); BOOL bReadStatus; BReadStatus=ReadFile (hcom, inBuffer, 5, & amp; DwRetread, & amp; M_osRead); if(! BReadStatus) { If (GetLastError ()==ERROR_IO_PENDING) { WaitForSingleObject (m_osRead hEvent, INFINITE); } Return FALSE; } Return TRUE; } CodePudding user response:
Another is a createevent function in the read function, this is what's the point?
CodePudding user response:
The GetOverlappedResult access to hang the results
BWriteStat=WriteFile (hcom, buffer, 5, & amp; DwRetwrite, & amp; M_osWrite); if(! BWriteStat & amp; & GetLastError ()==ERROR_IO_PENDING) { BWriteStat=the GetOverlappedResult (hcom, & amp; M_osWrite, & amp; DwRetwrite, TRUE) } CodePudding user response:
Quote: refer to the second floor zgl7903 response: The GetOverlappedResult access to hang the results [code]=c BWriteStat=WriteFile (hcom, buffer, 5, & amp; DwRetwrite, & amp; M_osWrite); if(! BWriteStat & amp; & GetLastError ()==ERROR_IO_PENDING) { BWriteStat=the GetOverlappedResult (hcom, & amp; M_osWrite, & amp; DwRetwrite, TRUE) } WaiForSingleObject not also is not very good, can you tell me, what is the difference between these two functions, And the main thread, I should create several threads, function is used to do? CodePudding user response:
WaiForSingleObject only waiting for the event, the GetOverlappedResult also returns the number writing values Asynchronous mode of OVERLAPPED hEvent to create Serial Communications in Win32 BOOL WriteABuffer (char * lpBuf, dwords dwToWrite) { OVERLAPPED osWrite={0}; DWORD dwWritten; DWORD dwRes; BOOL forward; //Create this write operation 's OVERLAPPED structure' s hEvent. OsWrite. HEvent=CreateEvent (NULL, TRUE, FALSE, NULL); If (osWrite hEvent==NULL) //the error creating overlapped event handle Return FALSE; //Issue write. if (! WriteFile (hComm lpBuf, dwToWrite, & amp; DwWritten, & amp; OsWrite)) { If (GetLastError ()!={ERROR_IO_PENDING) //WriteFile failed, but isn 't of Report the error and abort. The fRes=FALSE; } The else //Write is pending. DwRes=WaitForSingleObject (osWrite hEvent, INFINITE). The switch (dwRes) { //OVERLAPPED structure 's event has had signaled. Case WAIT_OBJECT_0: if (! The GetOverlappedResult (hComm, & amp; OsWrite, & amp; DwWritten, FALSE)) The fRes=FALSE; The else //Write operation completed successfully. The fRes=TRUE; break; Default: //An error has occurred in WaitForSingleObject. //This usually are a problem with the //OVERLAPPED structure 's event handle. The fRes=FALSE; break; } } } The else //WriteFile completed immediately. The fRes=TRUE; The CloseHandle (osWrite. HEvent); Return the fRes. } CodePudding user response:
reference 4 floor zgl7903 response: WaiForSingleObject only waiting for the event, the GetOverlappedResult also returns the number writing values Asynchronous mode of OVERLAPPED hEvent to create Serial Communications in Win32 BOOL WriteABuffer (char * lpBuf, dwords dwToWrite) { OVERLAPPED osWrite={0}; DWORD dwWritten; DWORD dwRes; BOOL forward; //Create this write operation 's OVERLAPPED structure' s hEvent. OsWrite. HEvent=CreateEvent (NULL, TRUE, FALSE, NULL); If (osWrite hEvent==NULL) //the error creating overlapped event handle Return FALSE; //Issue write. if (! WriteFile (hComm lpBuf, dwToWrite, & amp; DwWritten, & amp; OsWrite)) { If (GetLastError ()!={ERROR_IO_PENDING) //WriteFile failed, but isn 't of Report the error and abort. The fRes=FALSE; } The else //Write is pending. nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull