| 1 | --COMN INIT--
 | 
| 2 |     memset( &sOverlRd, 0, sizeof(OVERLAPPED) );
 | 
| 3 |     sOverlRd.hEvent = CreateEvent( NULL, FALSE, FALSE, NULL );
 | 
| 4 |     memset( &sOverlWr, 0, sizeof(OVERLAPPED) );
 | 
| 5 |     //If the 2nd parameter is TRUE, the function creates a manual-reset event object, 
 | 
| 6 |   //which requires the use of the ResetEvent function to set the event state to nonsignaled. 
 | 
| 7 |   //If this parameter is FALSE, the function creates an auto-reset event object, 
 | 
| 8 |   //and system automatically resets the event state to nonsignaled after a single waiting thread has been released. 
 | 
| 9 |   sOverlWr.hEvent = CreateEvent( NULL, TRUE, FALSE, NULL ); 
 | 
| 10 |     memset( &sOverlSt, 0, sizeof(OVERLAPPED) );
 | 
| 11 |     sOverlSt.hEvent = CreateEvent( NULL, FALSE, FALSE, NULL );
 | 
| 12 | 
 | 
| 13 | hCOM = CreateFile( pname, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL );
 | 
| 14 | 
 | 
| 15 | SetCommMask( hCOM, EV_RXFLAG );
 | 
| 16 | 
 | 
| 17 | --WRITE--
 | 
| 18 |     rc = WriteFile( hCOM, buff, (unsigned int) tx_length, &dwBytesWritten, &sOverlWr );
 | 
| 19 |   if (!rc)
 | 
| 20 |   {
 | 
| 21 |     rc = WaitForSingleObject( sOverlWr.hEvent, 1000 );  
 | 
| 22 |     ResetEvent( sOverlWr.hEvent );
 | 
| 23 |     m_lastERROR = GetLastError();
 | 
| 24 |     if (m_lastERROR == ERROR_IO_PENDING)
 | 
| 25 |     {
 | 
| 26 |       if (!GetOverlappedResult( hCOM,  &sOverlWr, &dwBytesWritten,TRUE)){
 | 
| 27 |         rc = dwBytesWritten;
 | 
| 28 |         m_lastERROR = GetLastError();
 | 
| 29 |       }
 | 
| 30 |     }
 | 
| 31 |   }
 | 
| 32 | 
 | 
| 33 | --READ--
 | 
| 34 |   if (!ClearCommError( hCOM, NULL, &m_st_comstat ))// bei ersten String gibt es die richtige anzahl an empfangenen Zeichen an. Beim zweiten mal 0.
 | 
| 35 |   {
 | 
| 36 |     m_lastERROR = GetLastError();
 | 
| 37 |   }
 | 
| 38 |   if (!m_st_comstat.cbInQue)
 | 
| 39 |   {
 | 
| 40 |       ret = WaitCommEvent( hCOM, &rc, &sOverlSt ); //wird diereckt verlasssen mit TRUE
 | 
| 41 |       if (!ret)
 | 
| 42 |       {
 | 
| 43 |         m_lastERROR = GetLastError();
 | 
| 44 |       }
 | 
| 45 |         dwRet = WaitForSingleObject( sOverlSt.hEvent, timeout*9 );//wird diereckt verlasssen mit TRUE
 | 
| 46 |       switch(dwRet)
 | 
| 47 |       {
 | 
| 48 |         // Read completed.
 | 
| 49 |       case WAIT_OBJECT_0:
 | 
| 50 |         if (!GetOverlappedResult(hCOM, &sOverlRd, &dwBytesRead, FALSE))
 | 
| 51 |           return FALSE;
 | 
| 52 |           // Error in communications; report it.
 | 
| 53 |         else
 | 
| 54 |         // Read completed successfully.
 | 
| 55 |         ClearCommError( hCOM, NULL, &m_st_comstat );//meldet immer noch 0 Zeichen empfangen.
 | 
| 56 |         ReadFile( hCOM, &buff[0], m_st_comstat.cbInQue, &dwBytesRead, &sOverlRd );
 | 
| 57 | ....
 | 
| 58 | }
 | 
| 59 | //da zeichen vorhanden/empfangen lese 
 | 
| 60 | if ( !ReadFile( hCOM, &buff[0], m_st_comstat.cbInQue, &dwBytesRead, &sOverlRd ) )
 | 
| 61 |         {
 | 
| 62 |       m_lastERROR = GetLastError();
 | 
| 63 | 
 | 
| 64 |       if (m_lastERROR == ERROR_IO_PENDING)
 | 
| 65 |       {
 | 
| 66 | ...
 |