5/29/2013

Serial Port (RS232C) WaitCommEvent Timeout Method.


linux - use select time out check.

win32 - use overlapped method. 

Open Serial Port Option by Overlapped.

HANDLE SPortHandle=
CreateFile (.. .. FILE_FLAG_OVERLAPPED | FILE_ATTRIBUTE_NORMAL,NULL);

//
// check serial port data
//
int isindataserialport(Cport , msec )
{
    int ret=1; // true , false;
    DWORD fdwCommMask;
    SetCommMask (Cport,/*EV_CTS | EV_DSR| */ EV_RXCHAR);
    #ifdef _WIN32_WCE
        WaitCommEvent (Cport, &fdwCommMask, 0);
    #else
        OVERLAPPED OverLapped;
        memset ((char *)&OverLapped, 0, sizeof(OVERLAPPED));
        OverLapped.hEvent = CreateEvent (NULL, TRUE, TRUE, 0);
        if (!WaitCommEvent (Cport, &fdwCommMask, &OverLapped))
       {
            if (GetLastError() == ERROR_IO_PENDING)
            {
                if(WaitForSingleObject (OverLapped.hEvent,(DWORD) msec) != WAIT_OBJECT_0)
                {
                     ret=0;
                }
            }
        }
        CloseHandle(OverLapped.hEvent);
    #endif

    if((fdwCommMask & EV_RXCHAR) != EV_RXCHAR)
   {
        ret=0;
    }
    return ret;
}



댓글 없음: