调用ReadFile()从STM32 USB端口读取数据时出现错误998
Hey there! Let's break down why you're hitting that ERROR_NOACCESS (998) error, and fix it step by step.
The Root Cause: Invalid Buffer Pointer
The biggest issue is in your main.c code—you've declared your receive buffer incorrectly:
// Wrong: This is an array of 31 char pointers, not a character buffer char* receivedData[31];
When you call readSerialPort(*receivedData, 31), you're passing receivedData[0]—an uninitialized pointer pointing to random memory. ReadFile tries to write data to this invalid address, which triggers the ERROR_NOACCESS (invalid memory access) error immediately.
Fix the Buffer Declaration
Change your buffer to a proper character array in main.c:
// Correct: A buffer that holds 31 characters (plus null terminator if needed) char receivedData[31] = {0}; // Initialize to zero to avoid garbage values
And update the function call to pass the buffer directly (array names decay to valid pointers automatically):
if (readSerialPort(receivedData, 31)) {
Secondary Issue: Uninitialized toRead Variable
In your readSerialPort function, if status.cbInQue is 0, the toRead variable is never assigned a value. This means ReadFile might try to read a random number of bytes, leading to undefined behavior.
Fix the Uninitialized Variable
Initialize toRead to 0 at the start of the function, and only attempt to read if there's data available:
uint8_t readSerialPort(char* buffer, unsigned int buf_size) { DWORD bytesRead = 0; unsigned int toRead = 0; // Initialize to 0 ClearCommError(handler, &errors, &status); if (status.cbInQue > 0) { // Use ternary operator for cleaner logic toRead = (status.cbInQue > buf_size) ? buf_size : status.cbInQue; } // Only call ReadFile if we have bytes to read if (toRead > 0 && ReadFile(handler, buffer, toRead, &bytesRead, NULL)) { return bytesRead; } return 0; }
Bonus: Type Mismatch in Return Value
Your readSerialPort function returns a uint8_t, but bytesRead is a DWORD. If you ever read more than 255 bytes, the return value will be truncated. For your current buffer size (31), this isn't a problem, but it's good practice to fix the return type to match:
// Change return type to DWORD DWORD readSerialPort(char* buffer, unsigned int buf_size) { // ... existing code ... return bytesRead; }
Then update the check in main.c:
DWORD bytesRead = readSerialPort(receivedData, 31); if (bytesRead > 0) { printf("%d - %.*s\n", id, (int)bytesRead, receivedData); id++; } else { printf("error %u\n", GetLastError()); }
Final Checks
- Double-check that your STM32 is sending data with the correct baud rate, parity, stop bits, and flow control—match these settings in your
DCBconfiguration (you currently don't set them, which uses defaults; make sure they align with your STM32 code). - Ensure your buffer is large enough to hold the incoming data (your 31-byte buffer should handle the 4-byte test data easily).
After making these changes, your ReadFile call should successfully write data to your buffer without triggering the access error.
内容的提问来源于stack exchange,提问作者Fluxable

