none
Serial Port Interrupt in Windows

    Question

  •  I am making a C program that communicates with a serial port. I want the reading & writing operations to take place independently. Is it possible for an interrupt to be issued by windows when it receives some input on the serial port? I believe it is possible using overlapped I/O by having separate threads for reading & writing. however, since this is the first time I have ever heard of multi-threaded programming, I am extremely confused. Please help me
    Tuesday, June 17, 2008 6:16 AM

All replies

  • I'm unsure what you mean by "possible for an interrupt to be issued by Windows". Under the hood, the serial port will raise an interrupt. Windows will then translate this into a response to WaitCommEvent.
     
    WaitCommEvent takes an event mask and an OVERLAPPED structure. You can -- as far as I recall: it's been a while since I did any serial programming in Windows -- associate different events with different OVERLAPPED structures (i.e. different event handles).

    Alternatively, I think that you can issue WaitCommEvent from different threads with different event masks...
    Tuesday, June 17, 2008 1:45 PM
  • I am waiting infinitely for an input to be received on the serial port. At the same time, I want the user to be able to write some commands during this infinite wait. If I receive some input, I want to be notified of it & process it. Otherwise, the control should remain with the user. I am presently using the following code snippet(Will I have to issue read & write in separate threads to achieve my objective?):

    BOOL WriteBuffer(HANDLE hComm,char * lpBuf)  
    {  
       OVERLAPPED osWrite = {0};  
       DWORD dwWritten;  
       DWORD dwToWrite=strlen(lpBuf);  
       BOOL fRes;  
     
       // Create OVERLAPPED structure hEvent.  
       osWrite.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);  
     
       // Issue write.  
       if (!WriteFile(hComm, lpBuf, dwToWrite, &dwWritten, &osWrite))  
       {  
          if (GetLastError() != ERROR_IO_PENDING)  
          {  
             // WriteFile failed, but it isn't delayed. Report error and abort.  
             printf("\nWriteFile failed with error %d.\n",GetLastError());  
             fRes = FALSE;  
          }  
          else  
          {  
             // Write is pending.  
             if (!GetOverlappedResult(hComm, &osWrite, &dwWritten, TRUE))  
                fRes = FALSE;  
             else  
             {   // Write operation completed successfully.  
                printf("\nWrite operation completed successfully.\n");  
                fRes = TRUE;  
             }  
          }  
       }  
       else  
       {  
          // WriteFile completed immediately.  
          printf("\nWriteFile completed immediately.\n");  
          fRes = TRUE;  
       }  
     
       CloseHandle(osWrite.hEvent);  
       return fRes;  
    }  
     
    int ReadBuffer(HANDLE HCom)  
    {  
        OVERLAPPED osRead = {0};  
        DWORD dwRead;  
        BOOL fRead;  
     
        // Create OVERLAPPED structure hEvent.  
        osRead.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);  
     
        DWORD dwCommEvent;  
     
        for ( ; ; )  
        {  
            WaitCommEvent(HCom, &dwCommEvent, &osRead);  
            if ( WaitForSingleObject(osRead.hEvent,INFINITE) == WAIT_OBJECT_0)  
            {  
                char szBuf[100];  
                do  
                {  
                    memset(szBuf,0,sizeof(szBuf));  
                    ReadFile( HCom,szBuf,sizeof(szBuf),&dwRead,&osRead);    //Read buffer  
                    if(dwRead!=0)  
                    {  
                        printf("%s\n",szBuf);  
                    }  
                }while (dwRead > 0 );  
            }  
            else  
            {  
                printf("Waiting for single object failed with error no %d\n",GetLastError());  
            }  
        }  
        CloseHandle(osRead.hEvent);  
    Wednesday, June 18, 2008 4:51 AM