开发者

ReadFile() says it failed, but the error code is ERROR_SUCCESS

开发者 https://www.devze.com 2023-02-26 20:48 出处:网络
I\'m using ReadFile() on Windows to read data from a serial port. This code was working fine at one point in time, but it\'s now failing and I\'m trying to track down the source of the problem, so I d

I'm using ReadFile() on Windows to read data from a serial port. This code was working fine at one point in time, but it's now failing and I'm trying to track down the source of the problem, so I doubt it's a problem with the serial configuration or timeouts, since none of that has changed.

ReadFile() returns false, indicating that an error occurred. However, when I immediately check the value of GetLastError(), it returns 0, which is ERROR_SUCCESS. The number of bytes read is 0, so I'm inclined to think that indeed something has gone wrong, but that error code is utterly useless.

Any ideas? Thanks.

EDIT: Here are some relevant code snippets:

#define GPS_COM_PORT L"COM3"

// for reference, the device communicates at 115200 baud,
// no parity, 1 stop bit, no flow control

// open gps com port
hGpsUart = CreateFile(GPS_COM_PORT, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if (hGpsUart == INVALID_HANDLE_VALUE)
{
    if (GetLastError() == ERROR_FILE_NOT_FOUND)
    {
        msg.setText("GPS COM port does not exist!");
        msg.exec();
        QApplication::quit();
    }

    msg.setText("Error occurred while trying to open GPS COM port!");
    msg.exec();
    QApplication::quit();
}

// set gps com port settings
dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
if (!GetCommState(hGpsUart, &dcbSerialParams))
{
    msg.setText("Could not get GPS COM port settings!");
    msg.exec();
    QApplication::quit();
}
dcbSerialParams.BaudRate = CBR_115200;
dcbSerialParams.ByteSize = 8;
dcbSerialParams.StopBits = ONESTOPBIT;
dcbSerialParams.Parity = NOPARITY;
if (!SetCommState(hGpsUart, &dcbSerialParams))
{
    msg.setText("Could not set GPS COM port settings!");
    msg.exec();
    QApplication::quit();
}

// set gps com port timeouts
timeouts.ReadIntervalTimeout = MAXDWORD;
timeouts.ReadTotalTimeoutConstant = 0;
timeouts.ReadTotalTimeoutMultiplier = 0;
t开发者_开发百科imeouts.WriteTotalTimeoutConstant = 50;
timeouts.WriteTotalTimeoutMultiplier = 10;
if (!SetCommTimeouts(hGpsUart, &timeouts))
{
    msg.setText("Could not set GPS COM port timeouts!");
    msg.exec();
    QApplication::quit();
}

// ... later in the code ...

char buf[161] = {0};
DWORD bytes_read = 0;

// This returns false...
if (!ReadFile(hGpsUart, buf, 160, &bytes_read, NULL)) 
{
    // Yet in here, GetLastError() returns ERROR_SUCCESS (0)
    QMessageBox msg;
    msg.setText("Error reading from GPS UART!");
    msg.exec();
}


I think the key to your observations is the phrase in your source that says "Yet in here, GetLastError() returns ERROR_SUCCESS (0)"

The call to GetLastError has to be the very next Win32 call made after the (presumably) failing call. As an experiment, try putting an explicit call to GetLastError() within your failure handler, but just before the message box call. I suspect you'll see the true failure code.

Good luck!


The constructor of QMessageBox may be doing something that clears `GetLastError'. Try this:

if (!ReadFile(hGpsUart, buf, 160, &bytes_read, NULL)) 
{
    int LastError = GetLastError() ;
    QMessageBox msg;
    msg.setText(QString("Error %1 reading from GPS UART!").arg(LastError));
    msg.exec();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号