Hallo Peter(TOO),
sorry das habe ich gestern leider in meinem Frust vergessen.
Code:BOOL CommSeriell::initComPort() { printf("\n\n +==========================================+"); printf("\n | Serial Port Reception (Win32 API) |"); printf("\n +==========================================+\n"); /*---------------------------------- Opening the Serial Port -------------------------------------------*/ TCHAR *ComPortName = TEXT("COM5"); hComm = CreateFile(ComPortName, // Name of the Port to be Opened GENERIC_READ | GENERIC_WRITE, // Read/Write Access 0, // No Sharing, ports cant be shared NULL, // No Security OPEN_EXISTING, // Open existing port only 0, // Non Overlapped I/O NULL); // Null for Comm Devices if (hComm == INVALID_HANDLE_VALUE) std::wcout<<"\n Error! - Port "<< ComPortName <<" can't be opened\n"; else std::wcout <<"\n Port "<<ComPortName<<" Opened\n "; /*------------------------------- Setting the Parameters for the SerialPort ------------------------------*/ DCB dcbSerialParams = { 0 }; // Initializing DCB structure dcbSerialParams.DCBlength = sizeof(dcbSerialParams); Status = GetCommState(hComm, &dcbSerialParams); //retreives the current settings if (Status == FALSE){ printf("\n Error! in GetCommState()"); error = 1; } dcbSerialParams.BaudRate = CBR_19200; // Setting BaudRate = 9600 dcbSerialParams.ByteSize = 8; // Setting ByteSize = 8 dcbSerialParams.StopBits = ONESTOPBIT; // Setting StopBits = 1 dcbSerialParams.Parity = NOPARITY; // Setting Parity = None Status = SetCommState(hComm, &dcbSerialParams); //Configuring the port according to settings in DCB if (Status == FALSE) { printf("\n Error! in Setting DCB Structure"); error = 1; } else //If Successfull display the contents of the DCB Structure { printf("\n\n Setting DCB Structure Successfull\n"); printf("\n Baudrate = %d", dcbSerialParams.BaudRate); printf("\n ByteSize = %d", dcbSerialParams.ByteSize); printf("\n StopBits = %d", dcbSerialParams.StopBits); printf("\n Parity = %d", dcbSerialParams.Parity); } /*------------------------------------ Setting Timeouts --------------------------------------------------*/ COMMTIMEOUTS timeouts = { 0 }; timeouts.ReadIntervalTimeout = 50; timeouts.ReadTotalTimeoutConstant = 50; timeouts.ReadTotalTimeoutMultiplier = 10; timeouts.WriteTotalTimeoutConstant = 50; timeouts.WriteTotalTimeoutMultiplier = 10; if (SetCommTimeouts(hComm, &timeouts) == FALSE) printf("\n\n Error! in Setting Time Outs"); else printf("\n\n Setting Serial Port Timeouts Successfull"); return Status; } BOOL CommSeriell::setRecieveMask() { /*------------------------------------ Setting Receive Mask ----------------------------------------------*/ Status = SetCommMask(hComm, EV_RXCHAR); //Configure Windows to Monitor the serial device for Character Reception if (Status == FALSE) printf("\n\n Error! in Setting CommMask"); else printf("\n\n Setting CommMask successfull"); return Status; } BOOL CommSeriell::recieveComPort(){ /*------------------------------------ Setting WaitComm() Event ----------------------------------------*/ printf("\n\n Waiting for Data Reception"); do { Status = WaitCommEvent(hComm, &dwEventMask, NULL); //Wait for the character to be received /*-------------------------- Program will Wait here till a Character is received ------------------------*/ if (Status == FALSE) { printf("\n Error! in Setting WaitCommEvent()"); error = 10; } else //If WaitCommEvent()==True Read the RXed data using ReadFile(); { printf("\n\n Characters Received"); do { Status = ReadFile(hComm, &TempChar, sizeof(TempChar), &NoBytesRead, NULL); SerialBuffer[i] = TempChar; i++; } while (NoBytesRead > 0); /*------------Printing the RXed String to Console----------------------*/ printf("\n\n "); int j = 0; for (j = 0; j < i - 1; j++) // j < i-1 to remove the dupliated last character printf("%c", SerialBuffer[j]); //PurgeComm(hComm, PURGE_RXCLEAR); //lehren des Empfangsbuffer } error++; //nur 10 mal ausführen } while (error <10); return Status; } void CommSeriell::theThread() { initComPort(); //Initialisieren des ComPort setRecieveMask(); //Event setzen recieveComPort(); //In einer Dauerschleife auf Nachrichten warten CloseHandle(hComm);//Closing the Serial Port printf("\n +==========================================+\n"); };
Die Funktion "theThread" wird als eigener Thread gestartet, der die Funktionen nacheinander aufruft.
Der Inhalt der Funktion "recieveComPort" läuft eigentlich in einer Dauerschleife bis ein Fehler auftritt.
Dies ist aktuell zum Testen allerdings auf 10 durchläufe begrenzt.
MfG Florian







Zitieren

Lesezeichen