<?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 ansprechen]]></title><description><![CDATA[<p>Hallo</p>
<p>Mein Ziel ist es über den Com1 -port abzufragen ob ein Schalter geschlossen oder offen ist. Meine Überlegung: Über RTS sende ich ständig ein TRUE. RTS mit CTS über den Schalter kurzschließen und wenn CTS TRUE ist weiß ich das der Schalter geschlossen wurde.</p>
<p>Ich habe es geschafft den RTS zu setzen kann aber nicht den CTS abfragen.</p>
<p>Das ist mein (zusammengesuchter) <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /> Code:</p>
<p>cport.h</p>
<pre><code>#define WRONG_PORT 100
#define ON true
#define OFF false

class ComPort
{
public:
int SetOPEN (int PORT);
int SetCLOSE (void);
int SetDTR (bool mode);
int SetRTS (bool mode);
int SetTXD (bool mode);
bool GetCTS ();
bool GetDSR ();
void SetAll (bool mode);
};
</code></pre>
<p>main.cpp</p>
<pre><code>#include &quot;CPORT.H&quot;
#include &lt;stdio.h&gt;
#include &lt;conio.h&gt;
#include &lt;windows.h&gt;
#include &lt;atlbase.h&gt;

HANDLE hCom;
DWORD COMStatus;

LPWSTR foo(const char* s) {
	USES_CONVERSION;
	LPWSTR p = A2W(s); // ANSI to WIDE
	return p;
}

int ComPort::SetOPEN(int PORT)
{
if(PORT&lt;=0 || PORT&gt;4)
{
return WRONG_PORT;
}

LPCSTR ansistr = &quot;COM1&quot;; 
LPWSTR neuerStr = foo(ansistr);

hCom = CreateFile(neuerStr,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
0,
NULL);

return 0;
}

int ComPort::SetCLOSE()
{
CloseHandle(hCom);
return 0;
}

int ComPort::SetDTR(bool mode)
{
if(mode==ON)
{
EscapeCommFunction(hCom, SETDTR); // setzen
}
else
{
EscapeCommFunction(hCom, CLRDTR); // Loeschen
}
return 0;
}

int ComPort::SetRTS(bool mode)
{
if(mode==ON)
{
EscapeCommFunction(hCom, SETRTS); // setzen
}
else
{
EscapeCommFunction(hCom, CLRRTS); // Loeschen
}
return 0;
}

int ComPort::SetTXD(bool mode)
{
if(mode==ON)
{
EscapeCommFunction(hCom, SETBREAK); // setzen
}
else
{
EscapeCommFunction(hCom, CLRBREAK); // Loeschen
}
return 0;
}

bool ComPort::GetCTS()
{
GetCommModemStatus(hCom, &amp;COMStatus);
if(COMStatus &amp; MS_CTS_ON)
{
return ON;
}
return OFF;
}

bool ComPort::GetDSR()
{
GetCommModemStatus(hCom, &amp;COMStatus);
if(COMStatus &amp; MS_DSR_ON)
{
return ON;
}
return OFF;
}
void ComPort::SetAll (bool mode)
{
SetRTS(mode);
SetTXD(mode);
SetDTR(mode);
}

void main(void){
	BOOL Loop = TRUE;
	ComPort comp;

	while(Loop){

		if( _kbhit() ){
			Loop = FALSE;
		}
			if (comp.SetOPEN(1)!=100){			
				comp.SetRTS(true);	
				if (comp.GetCTS()== true){
					printf (&quot;schalter geschlossen\n&quot;);
				}
			}else{
				printf(&quot;Wrong comport\n&quot;);
		}
	}	
	comp.SetCLOSE();
}
</code></pre>
<p>Gruß Eisenschlumpf</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/205040/com-port-ansprechen</link><generator>RSS for Node</generator><lastBuildDate>Sat, 04 Apr 2026 06:21:00 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/205040.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 09 Feb 2008 17:44:26 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to COM-port ansprechen on Sat, 09 Feb 2008 17:44:26 GMT]]></title><description><![CDATA[<p>Hallo</p>
<p>Mein Ziel ist es über den Com1 -port abzufragen ob ein Schalter geschlossen oder offen ist. Meine Überlegung: Über RTS sende ich ständig ein TRUE. RTS mit CTS über den Schalter kurzschließen und wenn CTS TRUE ist weiß ich das der Schalter geschlossen wurde.</p>
<p>Ich habe es geschafft den RTS zu setzen kann aber nicht den CTS abfragen.</p>
<p>Das ist mein (zusammengesuchter) <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /> Code:</p>
<p>cport.h</p>
<pre><code>#define WRONG_PORT 100
#define ON true
#define OFF false

class ComPort
{
public:
int SetOPEN (int PORT);
int SetCLOSE (void);
int SetDTR (bool mode);
int SetRTS (bool mode);
int SetTXD (bool mode);
bool GetCTS ();
bool GetDSR ();
void SetAll (bool mode);
};
</code></pre>
<p>main.cpp</p>
<pre><code>#include &quot;CPORT.H&quot;
#include &lt;stdio.h&gt;
#include &lt;conio.h&gt;
#include &lt;windows.h&gt;
#include &lt;atlbase.h&gt;

HANDLE hCom;
DWORD COMStatus;

LPWSTR foo(const char* s) {
	USES_CONVERSION;
	LPWSTR p = A2W(s); // ANSI to WIDE
	return p;
}

int ComPort::SetOPEN(int PORT)
{
if(PORT&lt;=0 || PORT&gt;4)
{
return WRONG_PORT;
}

LPCSTR ansistr = &quot;COM1&quot;; 
LPWSTR neuerStr = foo(ansistr);

hCom = CreateFile(neuerStr,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
0,
NULL);

return 0;
}

int ComPort::SetCLOSE()
{
CloseHandle(hCom);
return 0;
}

int ComPort::SetDTR(bool mode)
{
if(mode==ON)
{
EscapeCommFunction(hCom, SETDTR); // setzen
}
else
{
EscapeCommFunction(hCom, CLRDTR); // Loeschen
}
return 0;
}

int ComPort::SetRTS(bool mode)
{
if(mode==ON)
{
EscapeCommFunction(hCom, SETRTS); // setzen
}
else
{
EscapeCommFunction(hCom, CLRRTS); // Loeschen
}
return 0;
}

int ComPort::SetTXD(bool mode)
{
if(mode==ON)
{
EscapeCommFunction(hCom, SETBREAK); // setzen
}
else
{
EscapeCommFunction(hCom, CLRBREAK); // Loeschen
}
return 0;
}

bool ComPort::GetCTS()
{
GetCommModemStatus(hCom, &amp;COMStatus);
if(COMStatus &amp; MS_CTS_ON)
{
return ON;
}
return OFF;
}

bool ComPort::GetDSR()
{
GetCommModemStatus(hCom, &amp;COMStatus);
if(COMStatus &amp; MS_DSR_ON)
{
return ON;
}
return OFF;
}
void ComPort::SetAll (bool mode)
{
SetRTS(mode);
SetTXD(mode);
SetDTR(mode);
}

void main(void){
	BOOL Loop = TRUE;
	ComPort comp;

	while(Loop){

		if( _kbhit() ){
			Loop = FALSE;
		}
			if (comp.SetOPEN(1)!=100){			
				comp.SetRTS(true);	
				if (comp.GetCTS()== true){
					printf (&quot;schalter geschlossen\n&quot;);
				}
			}else{
				printf(&quot;Wrong comport\n&quot;);
		}
	}	
	comp.SetCLOSE();
}
</code></pre>
<p>Gruß Eisenschlumpf</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1452653</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1452653</guid><dc:creator><![CDATA[eisenschlumpf]]></dc:creator><pubDate>Sat, 09 Feb 2008 17:44:26 GMT</pubDate></item><item><title><![CDATA[Reply to COM-port ansprechen on Sun, 10 Feb 2008 17:38:02 GMT]]></title><description><![CDATA[<p>Crosspost: <a href="http://www.c-plusplus.net/forum/viewtopic-var-t-is-205091.html" rel="nofollow">http://www.c-plusplus.net/forum/viewtopic-var-t-is-205091.html</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1453190</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1453190</guid><dc:creator><![CDATA[simon.gysi]]></dc:creator><pubDate>Sun, 10 Feb 2008 17:38:02 GMT</pubDate></item></channel></rss>