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

我只是一个虾纸丫 提交于 2019-12-01 09:30:41

问题


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;
timeouts.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();
}

回答1:


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!




回答2:


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();
}


来源:https://stackoverflow.com/questions/5685090/readfile-says-it-failed-but-the-error-code-is-error-success

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!