Hallo,
ich versuche grade verzweifelt etwas vom COM Port zu lesen. Problem
dabei ist, dass das nicht blockend sein darf, sprich er soll einfach den
Kram den er im Puffer hat zurück geben und fertig. Momentan gibt die
ReadFile Funktion nen Fehler zurück, GetLastError() sagt dann jedoch
ERROR_IO_PENDING, also alles OK. Nur steht in der Variable wo die Anzahl
der gelesenen Bytes drin steht 0 drin.
Hier mein zusammengefasstes Programm:
1 | this->HCom=CreateFileA(Port.toStdString().c_str(), GENERIC_READ | GENERIC_WRITE , 0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
|
2 | if(this->HCom==INVALID_HANDLE_VALUE)
|
3 | return CREATEFILE;
|
4 | if(!SetupComm(this->HCom, 4096, 4096))
|
5 | return SETUPCOMM;
|
6 | if(!PurgeComm(this->HCom, PURGE_RXABORT | PURGE_RXCLEAR | PURGE_TXABORT | PURGE_TXCLEAR))
|
7 | return PURGECOMM;
|
8 | if(!GetCommState(this->HCom, &DCBstruct))
|
9 | return GETCOMMSTATE;
|
10 | QString CommDCBStr=Port+": baud="+Baud+" parity=N data=8 stop=1 to=off xon=off odsr=off octs=off dtr=off rts=off idsr=off";
|
11 | if(!BuildCommDCBA(CommDCBStr.toStdString().c_str(), &this->DCBstruct))
|
12 | return BUILDCOMMDCB;
|
13 | //this->DCBstruct.fDtrControl=0x02;
|
14 | if(!SetCommState(this->HCom, &this->DCBstruct))
|
15 | return SETCOMMSTATE;
|
Und dann noch das lesen:
1 | int ComPort::read(char *Data, int toRead)
|
2 | {
|
3 | DWORD Readed=0;
|
4 | overlappedRead.Offset=0;
|
5 | overlappedRead.OffsetHigh=0;
|
6 | overlappedRead.Pointer=NULL;
|
7 | overlappedRead.hEvent=0;
|
8 | ReadFile(this->HCom, Data, toRead, &Readed, &overlappedRead);
|
9 | if(GetLastError()==ERROR_IO_PENDING)
|
10 | {
|
11 | return Readed;
|
12 | }
|
13 | return -1;
|
14 | }
|