Forse ci siamo.
int Efis(void){
HANDLE hMCP;
OVERLAPPED osReader = { 0 };
BOOL fWaitingOnRead = FALSE;
hMCP = CreateFile(L"COM1",GENERIC_READ | GENERIC_WRITE,0,0, OPEN_EXISTING,FILE_FLAG_OVERLAPPED, 0);
if (hMCP == INVALID_HANDLE_VALUE){
stampa("MCP non collegato");
}
DCB prop;
GetCommState(hMCP, &prop);
prop.BaudRate = CBR_9600;
prop.Parity = NOPARITY;
prop.ByteSize = 8;
prop.StopBits = ONESTOPBIT;
if (!SetCommState(hMCP, &prop))
hMCP = INVALID_HANDLE_VALUE;
osReader.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
do{
if (osReader.hEvent == NULL){
stampa("Errore creazione i/o evento");
}
if (!fWaitingOnRead){
do{
CkR = ReadFile(hMCP, ch, 1, &CS, &osReader);
if (CS == 0){
ind = 2;
}
if (!CkR){
if (GetLastError() != ERROR_IO_PENDING){
stampa("Errore di comunicazione");
ind = 2;
}
else
fWaitingOnRead = TRUE;
}
else{
hdg[ind] = ch[0];
ind++;
}
} while (ind < 2);
if (hdg[0] != '\0'){
stampa(hdg);
hdg[0] = '\0';
}
ind = 0;
ch[0] = '\0';
}
#define READ_TIMEOUT 500 // milliseconds
DWORD dwRes;
if (fWaitingOnRead)
{
dwRes = WaitForSingleObject(osReader.hEvent, READ_TIMEOUT);
switch (dwRes)
{
//lettura completata
case WAIT_OBJECT_0:
if (!GetOverlappedResult(hMCP, &osReader, &CS, FALSE))
stampa("Errore di comunicazione");
else{
//Read completed successfully.
hdg[ind] = ch[0];
ind++;
}
fWaitingOnRead = FALSE;
break;
case WAIT_TIMEOUT:
break;
default:
break;
}
}
Sleep(10);
} while (1 < 2);
return 1;
}
Con questo loop riesco a leggere correttamente i valori sulla seriale in modo asincrono. Prossimo tentativo, leggere contemporaneamente un'altra porta.