【问题标题】:ReadFile() says it failed, but the error code is ERROR_SUCCESSReadFile() 表示失败,但错误代码为 ERROR_SUCCESS
【发布时间】:2011-04-16 17:21:49
【问题描述】:

我在 Windows 上使用ReadFile() 从串行端口读取数据。这段代码在某个时间点运行良好,但现在失败了,我正在尝试追查问题的根源,所以我怀疑这是串行配置或超时的问题,因为这些都没有改变。

ReadFile() 返回 false,表示发生错误。但是,当我立即检查GetLastError() 的值时,它返回0,即ERROR_SUCCESS。读取的字节数为 0,所以我倾向于认为确实有一些问题 出了问题,但那个错误代码完全没用。

有什么想法吗?谢谢。

编辑:这里是一些相关的代码sn-ps:

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

【问题讨论】:

  • 你能发布一些你的代码吗?
  • 已发布。感谢您的关注!
  • 我想GetLastError() 是在Readfile() 之后立即调用的,对吧?没有其他语句可以改变两者之间的错误值?

标签: c++ windows serial-port readfile getlasterror


【解决方案1】:

我认为您观察的关键是您的源代码中的短语“但在这里,GetLastError() 返回 ERROR_SUCCESS (0)”

对 GetLastError 的调用必须是在(可能)失败调用之后进行的下一个 Win32 调用。作为一个实验,尝试在失败处理程序中显式调用 GetLastError(),但就在消息框调用之前。我怀疑你会看到真正的失败代码。

祝你好运!

【讨论】:

  • 宾果游戏,这正是问题所在。 QMessageBox 正在干扰它。我现在可以看到正确的错误代码了,谢谢!
【解决方案2】:

QMessageBox 的构造函数可能正在做一些清除 `GetLastError' 的事情。试试这个:

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

【讨论】:

    猜你喜欢
    • 2012-04-20
    • 1970-01-01
    • 1970-01-01
    • 2012-03-15
    • 2012-01-22
    • 2015-10-16
    • 2020-07-12
    • 2016-01-28
    相关资源
    最近更新 更多