Hallo Mxt,
den Code hatte ich von folgender Seite: http://xanthium.in/Serial-Port-Progr...sing-Win32-API
Ich habe deine Funktion "SensorInterface::Receive()" einfach mal per Copy&Paste bei mir eingefügt und es funktioniert so wie ich mir es erhofft hatte.
Die Daten kommen mit 5 Hertz an. Wenn ich polle mit 10 Hertz kommen die Daten so an wie ich sie gern hätte.
Mit dem WaitCommEvent ganau so. Was sind hier die Vorteile?
Das mit den TimeOuts habe ich noch nicht ganz verstanden. Habe sie einfach mal auskommentiert und das Ergebnis war jeweils das gleiche.
Anbei mal mein aktueller Code:
Kann ich das so stehen lassen oder habt ihr noch Tipps zur optimierung?Code:#include "stdafx.h" #include "CommSeriell.h" CommSeriell::CommSeriell(int a) { m_run.store(true); m_thread = std::thread(std::bind(&CommSeriell::theThread, this)); } CommSeriell::~CommSeriell() { m_run.store(false); if (m_thread.joinable()) { m_thread.join(); } } BOOL CommSeriell::initComPort() { printf("\n\n +==========================================+"); printf("\n | Serial Port Reception (Win32 API) |"); printf("\n +==========================================+\n"); /*---------------------------------- Opening the Serial Port -------------------------------------------*/ 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(); { readbuf = Receive(); std::cout << "\nPrint input\n"; std::cout << readbuf << std::endl; } error++; //nur 10 mal ausführen } while (error <10); CloseHandle(hComm);//Closing the Serial Port return Status; } BOOL CommSeriell::readNMEA(){ return Status; } void CommSeriell::theThread() { initComPort(); setRecieveMask(); recieveComPort(); printf("\n +==========================================+\n"); }; std::string CommSeriell::Receive() { const int nReadSize = 256; std::vector<char> Buffer(nReadSize); unsigned long nDataRead; if (!ReadFile(hComm, &Buffer[0], nReadSize, &nDataRead, NULL)) { std::cout<<"General Read Error"; } return std::string(Buffer.begin(), Buffer.begin() + nDataRead); } //DWORD CommSeriell::InputSize() //{ // DWORD PortErrors; // COMSTAT PortStatus; // // if (ClearCommError(hComm, &PortErrors, &PortStatus)) // { // return PortStatus.cbInQue;; //} //else // return 0; //}
Danke für die schnelle Hilfe
Florian







Zitieren

Lesezeichen