I'm trying to send data from a STM32 Discovery Board to a host PC via USB using my own application. I managed to send data to the STM. I used WriteFile for that. For reading data I am calling the function readSerialPort()
which calls the function ReadFile()
.
In main.c readSerialPort()
is called in the while loop. Whenever I send some testdata (4 random bytes) to the PC I can see status.cbInQue
get bigger with 4, but ReadFile does not return TRUE, as to show it has finished putting the data in the buffer pointer leading to receivedData()
.
When I do GetLastErrror()
, it returns error code 998. Which means ERROR_NOACCESS, usually this means that the allocated array is not the same as buf_size
, but it is. So what causes this error and makes receiving data from the STM via USB not possible?
(I'm pretty new to asking a question on Stackoverflow so let me know if I need to improve in the way I'm asking this question)
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's first comment solved my problem:
char* receivedData[31];
is an array of uninitialized pointers. Did you meanchar receivedData[31];
? And then call:if (readSerialPort(receivedData, 31)) {