ReadFile()说它失败了,但是错误代码是ERROR_SUCCESS
ReadFile() says it failed, but the error code is ERROR_SUCCESS
我在Windows上使用ReadFile()
从串行端口读取数据。这段代码在某个时间点工作得很好,但现在失败了,我正试图追踪问题的根源,所以我怀疑这是串行配置或超时的问题,因为这些都没有改变。
ReadFile()
返回false,表示发生错误。然而,当我立即检查GetLastError()
的值时,它返回0,即ERROR_SUCCESS
。读取的字节数是0,所以我倾向于认为确实有出错了,但是那个错误代码是完全没用的。
任何想法?谢谢。
编辑:这里是一些相关的代码片段:
#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()返回ERROR_SUCCESS(0)"
对GetLastError的调用必须是(可能)调用失败后的下一个Win32调用。作为一个实验,尝试在失败处理程序中显式地调用GetLastError(),但是就在消息框调用之前。我想你会看到真正的失败代码。
祝你好运!
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();
}
相关文章:
- 错误处理.将系统错误代码映射到泛型
- C++错误:"error: int aaa::bbb is protected within this context"
- 为什么我在使用void函数时得到错误代码C2276
- 尝试链接我的着色器时,我收到错误代码"error c5145 must write to gl_position"
- 逻辑运算符上出现错误代码 a')'
- 'val' Arduino 错误代码之前的预期'('
- 我在贪吃蛇游戏中收到了错误代码 -1073741571
- 根据 GetLastError 直接写入磁盘会导致错误代码 5
- 当我选择大于 720 的矩阵大小时,程序退出并显示错误代码.可能是什么原因?
- 创建进程 API 失败,在窗口 122 上出现错误代码 10
- 使用另一个函数调用一个函数(都在类中)时出现问题.没有错误代码C++
- 当命令失败时,gzip 会在 C++ 中返回错误代码吗?
- 从 C++ 调用 cURL 命令会返回意外的错误代码,如 1792 和 6656
- WSALookupServiceBegin() 上的错误代码 10022(无效的 arugment)
- C++ Schannel POST 400 错误代码错误请求
- openCL-创建子缓冲区返回错误代码13
- 更新了Runge-Kutta(RK4)C++错误代码中的二阶DE
- iOS didFailToConnect外围设备:错误代码= 0 "Unknown error."
- c++/error::exc_bad_access错误代码=1
- 程序中"error: stray '302'"编译错误 代码::块