Hallo,

vielen Dank für die Hinweise und Erklärungen.

Ich habe jetzt versucht den Code etwas aufzuräumen:

Code:
//CommSeriell.cpp

#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()
{
	std::cout <<std::endl<<"Serial Port  Reception (Win32 API)"<< std::endl;

	/*---------------------------------- Opening the Serial Port -------------------------------------------*/
	
	ComPortName = TEXT("COM2");
	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){
		std::cout <<"\n    Error! in GetCommState()";
		error = 10;
	}
	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)
	{
		std::cout <<std::endl<<"\n    Error! in Setting DCB Structure"<<std::endl;
		error = 1;
	}
	else //If Successfull display the contents of the DCB Structure
	{
		std::cout << "\n\n    Setting DCB Structure Successfull" << std::endl
			<<"\n       Baudrate = " << dcbSerialParams.BaudRate
			<<"\n       ByteSize = %d" << dcbSerialParams.ByteSize
			<<"\n       StopBits = %d" << dcbSerialParams.StopBits
			<<"\n       Parity   = %d" << dcbSerialParams.Parity;
	}
	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)
		std::cout << "\n\n    Error! in Setting CommMask";
	else
		std::cout << "\n\n    Setting CommMask successfull";
	return Status;
}

BOOL CommSeriell::recieveComPort(){
	/*------------------------------------ Setting WaitComm() Event   ----------------------------------------*/

	std::cout <<"\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)
		{
			std::cout <<"\n    Error! in Setting WaitCommEvent()";
			error = 10;
		}
		else //If  WaitCommEvent()==True Read the RXed data using ReadFile();
		{
			readbuf = ReceiveData();
			std::cout <<"\n Print input\n";
			std::cout << readbuf << std::endl;
		}
		error++; //nur 10 mal ausführen
	} while (error <10);
	return Status;
}

std::string CommSeriell::ReceiveData()
{
	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);
}

void CommSeriell::theThread()
{
	initComPort();
	setRecieveMask();
	recieveComPort();

	CloseHandle(hComm);//Closing the Serial Port
	printf("\n +==========================================+\n");
}
Code:
// CommSeriell.h

#pragma once
class CommSeriell
{
public:
	CommSeriell(int a);
	~CommSeriell();

private:
	std::atomic<bool> m_run;
	std::thread m_thread;

	//Variablendeklarationen
	HANDLE hComm;				// Handle to the Serial port
	TCHAR  *ComPortName;		// Name of the Serial port(May Change) to be opened,
	BOOL  Status;				// Status of the various operations 
	DWORD dwEventMask;			// Event mask to trigger
	int error = 0;				// 
	std::string readbuf;		//

	//Funktiosdeklarationen
	void theThread();

	BOOL initComPort();
	BOOL setRecieveMask();
	BOOL recieveComPort();
	std::string ReceiveData();
};
Anbei auch die Variablen-Deklarationen.

Die von dir einen Thread vorher genannten Punkte habe ich hoffentlich soweit alle herausgenommen.

Anbei noch ein Bild wie die Daten jetzt ausgegeben werden.

MfG Florian

Klicke auf die Grafik für eine größere Ansicht

Name:	Screenshot_WaitCommEvent.jpg
Hits:	5
Größe:	63,9 KB
ID:	31549