Hallo Freunde,
Habe hier ein Problem mit der seriellen Kommunikation.
Ich habe mir ne Seriellen Schnittstelle geoeffnet und kann irgendwie keine Daten lesen.
Zur Kontrolle gibt mein Sender immer folgendes aus: 1 CR LF (31h 13h 10h).
Bekommen tu ich aber nur Unsinn.
Ich initialisiere so:
HANDLE hCom=INVALID\_HANDLE\_VALUE; //handle of active connection
COMMTIMEOUTS timeouts = {1000,1000,1000,1000,1000}; //set Timeouts, 1000ms
int OpenPort(char \*pcCommPort) //bsp: "COM1"
{
DCB dcb;
BOOL fSuccess;
hCom = CreateFile( pcCommPort,
GENERIC\_READ | GENERIC\_WRITE,
0, // comm devices must be opened w/exclusive-access
NULL, // no security attributes
OPEN\_EXISTING, // comm devices must use OPEN\_EXISTING
0, // not overlapped I/O
NULL // hTemplate must be NULL for comm devices
);
if (hCom == INVALID\_HANDLE\_VALUE) {
// Handle the error.
return (1);
}
fSuccess = GetCommState(hCom, &dcb);
if (!fSuccess) {
// Handle the error.
return (2);
}
//COM-Port configuration
dcb.fBinary = TRUE;
dcb.Parity = FALSE; // no parity
dcb.fOutxCtsFlow = FALSE;
dcb.fOutxDsrFlow = FALSE;
dcb.fDtrControl = DTR\_CONTROL\_DISABLE;
dcb.fDsrSensitivity = FALSE;
dcb.fTXContinueOnXoff = FALSE;
dcb.BaudRate = 4800; // baud=4800 bps
dcb.ByteSize = 8; //8 data bits
dcb.fOutX = FALSE;
dcb.fInX = FALSE;
dcb.fErrorChar = FALSE;
dcb.fNull = FALSE;
dcb.fRtsControl = RTS\_CONTROL\_DISABLE;
dcb.fAbortOnError = FALSE;
dcb.StopBits = ONESTOPBIT; //1 stop bit
fSuccess = SetCommState(hCom, &dcb);
if (!fSuccess)
{
// Handle the error.
return (3);
}
fSuccess = SetCommTimeouts(hCom,&timeouts); //set timeouts
if (!fSuccess)
{
// Handle the error.
return (4);
}
//fSuccess = SetupComm(hCom,256,10); //set buffers; input=256 bytes, output=10 bytes
if (!fSuccess)
{
// Handle the error.
return (5);
}
return (0);
}
So, jetzt versuche ich die Daten zu lesen.
void ReadByte (void)
{
DWORD dwBytesRead;
char\* dwBytes="";
ReadFile(hCom, &dwBytes, 1, &dwBytesRead, NULL);
Sleep(10);
if(MessageBox(NULL,&dwBytes,"again",1)==IDOK) ReadByte();
}
Aber da kommt nur Muell. sowas wie 10B oder 0B. Ich meine, selbst wenn ich glauben wuerde, es waeren Binaerzahlen, 10B ist nicht 1 sondern 3.
Wenn der Sender nichts sendet kommt gleich nur Kram raus. Ist echt komisch.
Weiss jemand Rat?
der Guenther