I'm trying to read a com port. I can create it, change the dcb, set the timeouts and see the input buffer using ClearCommError. But I can not read it. When the program hits the ReadFile() statement it never comes back. Here is some of the code:

char CMyClass::EIA232_Open(HANDLE* h232, char* cPortNum, long baudrate)
    DCB dcb;
    COMMTIMEOUTS commtimeouts;
    char EIA232_Port[64];
    HANDLE hEIA232;
    DWORD dwFlags;

    hEIA232 = *h232;


    // Open the port
    hEIA232 = CreateFile(EIA232_Port, 
                         GENERIC_READ | GENERIC_WRITE, 

    // Set the comm port buffers
    SetupComm(hEIA232, 256, 256);

    // Set the commtimeouts
    commtimeouts.ReadIntervalTimeout = 0;
    commtimeouts.ReadTotalTimeoutConstant = 0;
    commtimeouts.ReadTotalTimeoutMultiplier = 10;
    commtimeouts.WriteTotalTimeoutConstant = 0;
    commtimeouts.WriteTotalTimeoutMultiplier = 10;
    SetCommTimeouts(hEIA232, &commtimeouts);

    // Set the dcb
    dcb.DCBlength = sizeof(DCB);
    fSuccess = GetCommState(hEIA232, &dcb);

    dcb.BaudRate = baudrate;          // Baud rate for the CAN converter
    dcb.ByteSize = 8;                      // 8 bits / char
    dcb.StopBits = ONESTOPBIT;      // One stop bit
    dcb.Parity = NOPARITY;              // No parity
    dcb.fParity = false;                     // No parity functions
    dcb.DCBlength = sizeof(DCB);    // Need to set this
    dcb.fBinary = true;                    // This must be true; Microsoft does not support non-binary mode transfers
    dcb.fOutX = false;                      // XON/XOFF disabled for transmit
    dcb.fInX = false;                        // XON/XOFF disabled for receive
    dcb.fOutxCtsFlow = false;           // CTS disabled
    dcb.fOutxDsrFlow = false;          // DSR disabled
    dcb.fParity = false;                    // No parity
    dcb.fDsrSensitivity = false;        // DSR disabled
    dcb.XonLim = 100;                    // This shouldn't matter since no flow control is used
    dcb.XoffLim = 100;                    // This shouldn't matter since no flow control is used
    dcb.fDtrControl = DTR_CONTROL_ENABLE;  // Enables DTR line when opened, and stays on
    dcb.fRtsControl = RTS_CONTROL_ENABLE;  // Enables RTS line when opened, and stays on
    dcb.fInX = false;                       // Disable receive XON/XOFF control
    dcb.fOutX = false;                     // Disable transmit XON/XOFF control
    dcb.fNull = false;                      // Do not discard null characters
    dcb.fAbortOnError = false;        // Do not abort on an error
    SetCommState(hEIA232, &dcb)

    // Set the comm mask
    SetCommMask(hEIA232, EV_RXCHAR)


char MyClass::EIA232_Read(HANDLE h232, char* cData)
    int count;
    char buf[512];
    DWORD bytesread;
    COMSTAT comstat;

    while(count < 100)  // We'll try 100 times to get all the data
        // Do we have data in the buffer
        if(!ClearCommError(h232, &dwCommErrors, &comstat))

        SecureZeroMemory(buf, sizeof(buf));
        if(comstat.cbInQue > 0)
            // Read the buffer
            print_status("Reading rcv buffer");

           // This next statement never returns.
            if(!ReadFile(h232, buf, sizeof(buf), &bytesread, NULL)) 



I would expect the ReadFile to return almost immediately. If I stop the code and look at the cbInQue value, it is 50. Then when I procede to the ReadFile() function, it hangs. If I let the code go at its normal pace, it still hangs.

Thanks for any help on this.

try flooding comstat with 0s before calling ClearCommError().

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.