ReadFile() 尝试从 USB 端口读取数据时出现错误 998
ReadFile() gives error 998 when trying to read data from USB port
我正在尝试使用我自己的应用程序通过 USB 将数据从 STM32 探索板发送到主机 PC。我设法将数据发送到 STM。我为此使用了 WriteFile。为了读取数据,我调用函数 readSerialPort()
,它调用函数 ReadFile()
.
In main.c readSerialPort()
在 while 循环中被调用。每当我向 PC 发送一些测试数据(4 个随机字节)时,我可以看到 status.cbInQue
变大了 4,但 ReadFile 没有 return TRUE,表明它已完成将数据放入缓冲区指针导致 receivedData()
.
当我做GetLastErrror()
时,它return的错误代码是998。这意味着ERROR_NOACCESS,通常这意味着分配的数组与 buf_size
不同,但它是。那么是什么原因导致此错误并导致无法通过 USB 从 STM 接收数据?
(我对在 Whosebug 上提问很陌生,所以如果我需要改进我提出这个问题的方式,请告诉我)
main.c
char* receivedData[31];
uint32_t id = 0;
while(1) {
if (readSerialPort(*receivedData, 31)) {
printf("%d - %s\n", id, receivedData);
id++;
} else {
printf("error %u\n", GetLastError());
}
comm.c
#include "comm.h"
HANDLE handler;
uint8_t connected;
COMSTAT status;
DWORD errors;
int connectSerial(char* portName) {
connected = 0;
handler = CreateFileA(portName,
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);
if (handler == INVALID_HANDLE_VALUE) {
if (GetLastError() == ERROR_FILE_NOT_FOUND) {
printf("ERROR: Handle was not attached. Reason: %s not available\n", portName);
} else {
printf("ERROR!!!");
}
} else {
DCB dcbSerialParameters = { 0 };
if (!GetCommState(handler, &dcbSerialParameters)) {
printf("failed to get current serial parameters");
} else {
connected = 1;
PurgeComm(handler, PURGE_RXCLEAR | PURGE_TXCLEAR);
// Sleep(2000);
}
}
}
void disconnectSerial() {
if (connected) {
connected = 0;
CloseHandle(handler);
}
}
uint8_t readSerialPort(char* buffer, unsigned int buf_size) {
DWORD bytesRead;
unsigned int toRead;
ClearCommError(handler, &errors, &status);
if (status.cbInQue > 0) {
if (status.cbInQue > buf_size)
toRead = buf_size;
else
toRead = status.cbInQue;
}
if (ReadFile(handler, buffer, toRead, &bytesRead, NULL))
return bytesRead;
return 0;
}
uint8_t writeSerialPort(char* buffer, unsigned int buf_size) {
DWORD bytesSend;
if (!WriteFile(handler, (void*)buffer, buf_size, &bytesSend, 0)) {
ClearCommError(handler, &errors, &status);
return 0;
}
else return 1;
}
Johnny 的第一条评论解决了我的问题:
char* receivedData[31];
is an array of uninitialized pointers. Did you
mean char receivedData[31];
? And then call:
if (readSerialPort(receivedData, 31)) {
我正在尝试使用我自己的应用程序通过 USB 将数据从 STM32 探索板发送到主机 PC。我设法将数据发送到 STM。我为此使用了 WriteFile。为了读取数据,我调用函数 readSerialPort()
,它调用函数 ReadFile()
.
In main.c readSerialPort()
在 while 循环中被调用。每当我向 PC 发送一些测试数据(4 个随机字节)时,我可以看到 status.cbInQue
变大了 4,但 ReadFile 没有 return TRUE,表明它已完成将数据放入缓冲区指针导致 receivedData()
.
当我做GetLastErrror()
时,它return的错误代码是998。这意味着ERROR_NOACCESS,通常这意味着分配的数组与 buf_size
不同,但它是。那么是什么原因导致此错误并导致无法通过 USB 从 STM 接收数据?
(我对在 Whosebug 上提问很陌生,所以如果我需要改进我提出这个问题的方式,请告诉我)
main.c
char* receivedData[31];
uint32_t id = 0;
while(1) {
if (readSerialPort(*receivedData, 31)) {
printf("%d - %s\n", id, receivedData);
id++;
} else {
printf("error %u\n", GetLastError());
}
comm.c
#include "comm.h"
HANDLE handler;
uint8_t connected;
COMSTAT status;
DWORD errors;
int connectSerial(char* portName) {
connected = 0;
handler = CreateFileA(portName,
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);
if (handler == INVALID_HANDLE_VALUE) {
if (GetLastError() == ERROR_FILE_NOT_FOUND) {
printf("ERROR: Handle was not attached. Reason: %s not available\n", portName);
} else {
printf("ERROR!!!");
}
} else {
DCB dcbSerialParameters = { 0 };
if (!GetCommState(handler, &dcbSerialParameters)) {
printf("failed to get current serial parameters");
} else {
connected = 1;
PurgeComm(handler, PURGE_RXCLEAR | PURGE_TXCLEAR);
// Sleep(2000);
}
}
}
void disconnectSerial() {
if (connected) {
connected = 0;
CloseHandle(handler);
}
}
uint8_t readSerialPort(char* buffer, unsigned int buf_size) {
DWORD bytesRead;
unsigned int toRead;
ClearCommError(handler, &errors, &status);
if (status.cbInQue > 0) {
if (status.cbInQue > buf_size)
toRead = buf_size;
else
toRead = status.cbInQue;
}
if (ReadFile(handler, buffer, toRead, &bytesRead, NULL))
return bytesRead;
return 0;
}
uint8_t writeSerialPort(char* buffer, unsigned int buf_size) {
DWORD bytesSend;
if (!WriteFile(handler, (void*)buffer, buf_size, &bytesSend, 0)) {
ClearCommError(handler, &errors, &status);
return 0;
}
else return 1;
}
Johnny 的第一条评论解决了我的问题:
char* receivedData[31];
is an array of uninitialized pointers. Did you meanchar receivedData[31];
? And then call:if (readSerialPort(receivedData, 31)) {