<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[COM Port - Empfangene Bytes auslesen?]]></title><description><![CDATA[<p>Hallo miteinander!</p>
<p>Ich versuche schon seit xx Stunden über den folgenden Code in einer while schleife zu pollen ob Bytes empfangen worden sind. Wenn dies Anzahl der empfangenen Bytes gleich der Länge ist die ich als Antwort erwarte soll diese Antwort ausgewertet werden. Über das Terminalprogramm kann man sehen, dass definitiv Werte gesendet werden. (auch in der richtigen Länge)</p>
<pre><code class="language-cpp">HANDLE hRS232;
DCB	RS232Setting;
COMMTIMEOUTS	RS232Timeout;
boolean RS232IsOpened = false;
char key;

.. MAIN ...
while(true){

	while(_kbhit()){
		key = _getch();

			if(key == 'e'){
			...sende Ende Command ....
			}
		}
	Sleep(100);
	if(GetNumOfBytesInBuffer() == 4){
		cout&lt;&lt;&quot;4 byte im Empfangsbuffer&quot;&lt;&lt;endl;
	}

}
.... MAIN ENDE ...

int GetNumOfBytesInBuffer(void)
{
COMSTAT		sComStat;
DWORD		dwErrorFlags;

	if(!RS232IsOpened)				{ return -1; }

	//dwErrorFlags=0;

	if(!ClearCommError(hRS232, &amp;dwErrorFlags, &amp;sComStat)){

		return 0;

	}

return ((int) sComStat.cbInQue);

int RS232Open(unsigned int PortNr,int BaudRate, int NumOfStopBits, int NumOfDataBits)
{
	wchar_t RS232DeviceName[10];

	/* Some rudimentary Error Checks*/
	if(PortNr &gt; UPPER_PORTNR_BOUND)		{ return -1; }
	if(RS232IsOpened)			{ return -1; }
	if(NumOfDataBits &gt; 8)			{ return -1; }

	/* lets copy the desired PortNr to the identifier string*/
	swprintf (RS232DeviceName,TEXT(&quot;\\\\.\\COM%i&quot;), PortNr);
	hRS232 = CreateFile(RS232DeviceName,GENERIC_READ | GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
	if(hRS232 == INVALID_HANDLE_VALUE)	{ 
		switch (GetLastError())	{
			case ERROR_FILE_NOT_FOUND:	
printf(&quot;The specified COM Port does not exist!&quot;);
				break;
			case ERROR_ACCESS_DENIED:	
printf(&quot;The specified COM Port is already in use!&quot;);
				break;				
		} 
		return -1; 
	}

	else{

		SetupComm(hRS232, 1024, 1024);

		if(!GetCommState (hRS232, &amp;RS232Setting)){
			printf(&quot;%i&quot;, GetLastError());
			return -1;
		}

		/* Initialise the Array RS232Setting with 0 */
		RS232Setting.DCBlength = sizeof(DCB);
		RS232Setting.BaudRate = BaudRate;
		RS232Setting.Parity = NOPARITY;
		RS232Setting.StopBits = ONESTOPBIT;	
		RS232Setting.ByteSize = 8;

		/* Flags */ 
		RS232Setting.fBinary = TRUE;
		RS232Setting.fOutxCtsFlow = FALSE;
		RS232Setting.fOutxDsrFlow = FALSE;
		RS232Setting.fDtrControl = FALSE;
		RS232Setting.fRtsControl = RTS_CONTROL_DISABLE;
		RS232Setting.fDsrSensitivity = FALSE;
		RS232Setting.fOutX = FALSE;
		RS232Setting.fInX = FALSE;

		/* Now configure the Device according to the DCB Memset */
		if(SetCommState(hRS232, &amp;RS232Setting) == 0){
			printf(&quot;%i&quot;,GetLastError());
			CloseHandle(hRS232);
			return -1;
		}

		/*Set Comm Port Timeout's */
		RS232Timeout.ReadIntervalTimeout		=55;
		RS232Timeout.ReadTotalTimeoutConstant		=150;
		RS232Timeout.ReadTotalTimeoutMultiplier		=100;
		RS232Timeout.WriteTotalTimeoutConstant		=75; 
		RS232Timeout.WriteTotalTimeoutMultiplier	=10; 

		if(!SetCommTimeouts(hRS232, &amp;RS232Timeout)){
			CloseHandle(hRS232);
			return -1;
		}

		PurgeComm(hRS232,PURGE_RXABORT|PURGE_RXCLEAR);

		RS232IsOpened = true;
		return 0;
	}
}
</code></pre>
<p>Kann hier jemand erkennen wo das Problem liegt? Da ich mit der seriellen Schnitstelle unter Win noch fast keine Erfahrung habe bin ich mir auch nicht sicher ob das überhaupt der richtige Weg um zu pollen.</p>
<p>Was funktioniert ist wenn ich etwas auf RS232 schreibe und dann sofort mit READ lese kann ich die Antwortnachricht problemlos lesen.</p>
<p>Was aber überhaupt nicht funktioniert ist dieses pollen um zu prüfen ob bytes empfangen wurde.</p>
<p>Verwende Win XP und MS Visual C++ 2005 Express</p>
<p>Würde mich über jede Hilfe / jeden Tipp sehr freuen<br />
lg<br />
rain</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/166157/com-port-empfangene-bytes-auslesen</link><generator>RSS for Node</generator><lastBuildDate>Sat, 02 May 2026 03:31:03 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/166157.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 26 Nov 2006 19:04:24 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to COM Port - Empfangene Bytes auslesen? on Sun, 26 Nov 2006 19:04:24 GMT]]></title><description><![CDATA[<p>Hallo miteinander!</p>
<p>Ich versuche schon seit xx Stunden über den folgenden Code in einer while schleife zu pollen ob Bytes empfangen worden sind. Wenn dies Anzahl der empfangenen Bytes gleich der Länge ist die ich als Antwort erwarte soll diese Antwort ausgewertet werden. Über das Terminalprogramm kann man sehen, dass definitiv Werte gesendet werden. (auch in der richtigen Länge)</p>
<pre><code class="language-cpp">HANDLE hRS232;
DCB	RS232Setting;
COMMTIMEOUTS	RS232Timeout;
boolean RS232IsOpened = false;
char key;

.. MAIN ...
while(true){

	while(_kbhit()){
		key = _getch();

			if(key == 'e'){
			...sende Ende Command ....
			}
		}
	Sleep(100);
	if(GetNumOfBytesInBuffer() == 4){
		cout&lt;&lt;&quot;4 byte im Empfangsbuffer&quot;&lt;&lt;endl;
	}

}
.... MAIN ENDE ...

int GetNumOfBytesInBuffer(void)
{
COMSTAT		sComStat;
DWORD		dwErrorFlags;

	if(!RS232IsOpened)				{ return -1; }

	//dwErrorFlags=0;

	if(!ClearCommError(hRS232, &amp;dwErrorFlags, &amp;sComStat)){

		return 0;

	}

return ((int) sComStat.cbInQue);

int RS232Open(unsigned int PortNr,int BaudRate, int NumOfStopBits, int NumOfDataBits)
{
	wchar_t RS232DeviceName[10];

	/* Some rudimentary Error Checks*/
	if(PortNr &gt; UPPER_PORTNR_BOUND)		{ return -1; }
	if(RS232IsOpened)			{ return -1; }
	if(NumOfDataBits &gt; 8)			{ return -1; }

	/* lets copy the desired PortNr to the identifier string*/
	swprintf (RS232DeviceName,TEXT(&quot;\\\\.\\COM%i&quot;), PortNr);
	hRS232 = CreateFile(RS232DeviceName,GENERIC_READ | GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
	if(hRS232 == INVALID_HANDLE_VALUE)	{ 
		switch (GetLastError())	{
			case ERROR_FILE_NOT_FOUND:	
printf(&quot;The specified COM Port does not exist!&quot;);
				break;
			case ERROR_ACCESS_DENIED:	
printf(&quot;The specified COM Port is already in use!&quot;);
				break;				
		} 
		return -1; 
	}

	else{

		SetupComm(hRS232, 1024, 1024);

		if(!GetCommState (hRS232, &amp;RS232Setting)){
			printf(&quot;%i&quot;, GetLastError());
			return -1;
		}

		/* Initialise the Array RS232Setting with 0 */
		RS232Setting.DCBlength = sizeof(DCB);
		RS232Setting.BaudRate = BaudRate;
		RS232Setting.Parity = NOPARITY;
		RS232Setting.StopBits = ONESTOPBIT;	
		RS232Setting.ByteSize = 8;

		/* Flags */ 
		RS232Setting.fBinary = TRUE;
		RS232Setting.fOutxCtsFlow = FALSE;
		RS232Setting.fOutxDsrFlow = FALSE;
		RS232Setting.fDtrControl = FALSE;
		RS232Setting.fRtsControl = RTS_CONTROL_DISABLE;
		RS232Setting.fDsrSensitivity = FALSE;
		RS232Setting.fOutX = FALSE;
		RS232Setting.fInX = FALSE;

		/* Now configure the Device according to the DCB Memset */
		if(SetCommState(hRS232, &amp;RS232Setting) == 0){
			printf(&quot;%i&quot;,GetLastError());
			CloseHandle(hRS232);
			return -1;
		}

		/*Set Comm Port Timeout's */
		RS232Timeout.ReadIntervalTimeout		=55;
		RS232Timeout.ReadTotalTimeoutConstant		=150;
		RS232Timeout.ReadTotalTimeoutMultiplier		=100;
		RS232Timeout.WriteTotalTimeoutConstant		=75; 
		RS232Timeout.WriteTotalTimeoutMultiplier	=10; 

		if(!SetCommTimeouts(hRS232, &amp;RS232Timeout)){
			CloseHandle(hRS232);
			return -1;
		}

		PurgeComm(hRS232,PURGE_RXABORT|PURGE_RXCLEAR);

		RS232IsOpened = true;
		return 0;
	}
}
</code></pre>
<p>Kann hier jemand erkennen wo das Problem liegt? Da ich mit der seriellen Schnitstelle unter Win noch fast keine Erfahrung habe bin ich mir auch nicht sicher ob das überhaupt der richtige Weg um zu pollen.</p>
<p>Was funktioniert ist wenn ich etwas auf RS232 schreibe und dann sofort mit READ lese kann ich die Antwortnachricht problemlos lesen.</p>
<p>Was aber überhaupt nicht funktioniert ist dieses pollen um zu prüfen ob bytes empfangen wurde.</p>
<p>Verwende Win XP und MS Visual C++ 2005 Express</p>
<p>Würde mich über jede Hilfe / jeden Tipp sehr freuen<br />
lg<br />
rain</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1181865</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1181865</guid><dc:creator><![CDATA[rain]]></dc:creator><pubDate>Sun, 26 Nov 2006 19:04:24 GMT</pubDate></item><item><title><![CDATA[Reply to COM Port - Empfangene Bytes auslesen? on Sun, 26 Nov 2006 19:08:46 GMT]]></title><description><![CDATA[<p>Nimm doch eine fertige Klasse und erfinde nicht das Rad neu. Serielle Kommunikation ist *nicht* trivial. Auch wenn es sich so anhört...<br />
<a href="http://www.codeproject.com/system/serial.asp" rel="nofollow">http://www.codeproject.com/system/serial.asp</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1181867</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1181867</guid><dc:creator><![CDATA[helper class]]></dc:creator><pubDate>Sun, 26 Nov 2006 19:08:46 GMT</pubDate></item><item><title><![CDATA[Reply to COM Port - Empfangene Bytes auslesen? on Sun, 26 Nov 2006 23:51:51 GMT]]></title><description><![CDATA[<p>Verwende doch einfach SetCommTimeouts und ReadFile - mit SetCommTimeouts stellst du entweder 0 oder halt deine 100ms als fixes Timeout ein, und mit ReadFile liest du eben alles was verfügbar ist in einen Puffer den du selbst verwaltest. Wenn du dann deine 4 Byte zusammengetragen hast brichst du einfach ab. Sollte ganz einfach sein <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1181984</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1181984</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Sun, 26 Nov 2006 23:51:51 GMT</pubDate></item></channel></rss>