<?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[Serielle Schnittstelle mit 3 Drähten,   ReadFile()]]></title><description><![CDATA[<p>Hallo,<br />
also ich hab mir ne Verbindung zwischen 2 PCs mit 3 Drähten über die Rs232-Schnittstelle gebaut.</p>
<p>Ich hab mir dann auch n VB programm heruntergeladen. Mit dem Programm kann ich dann &quot;chatten&quot;.</p>
<p>Nun hab ich versucht das gleiche in C zu machen. Wenn ich auf dem PC1 des VB-prog hab und auf PC2 mein eigenes, dann kann ich von PC2 auf PC1 auch chararrays senden, aber nicht empfangen.<br />
D.h. mit dem c prog kann ich nur senden, ich will aber auch empfangen.</p>
<p>hierm mein code:</p>
<pre><code class="language-cpp">#include &lt;stdio.h&gt;
#include &lt;windows.h&gt;
#include &lt;process.h&gt;

#define MAX 256

unsigned __stdcall SecondThreadFunc(void* pArguments);

HANDLE hCom;

int main()
{
  // CREATE THREAD
  HANDLE hThread;
  unsigned threadID;

  COMMTIMEOUTS CommTimeouts;

  DWORD wieviel;//dwEvtMask;
  DCB dcb;
  DWORD dw = GetLastError();
  char buf[MAX];
  int boll = 0;
  int rc = 1;

  hThread = (HANDLE)_beginthreadex( NULL, 0, &amp;SecondThreadFunc, NULL, 0, &amp;threadID );

  hCom = CreateFile(&quot;COM2&quot;,GENERIC_READ|GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);  //anstatt NULL : FILE_FLAG_OVERLAPPED
  if(hCom == INVALID_HANDLE_VALUE)
  {
        printf(&quot;COM Port ist besetzt&quot;);
        getchar();
        return 1;
  }
  else
        printf(&quot;COM Port OK\n&quot;);

  boll = SetupComm(hCom, MAX, MAX); // set buffer sizes
  if(boll!=0)
       printf(&quot;1 alles ok\n&quot;);
  else
       printf(&quot;1 nö Fehler\n&quot;);

  getchar();

    ///////////////////////////////////////////////////////

// Initialize the DCBlength member.
dcb.DCBlength = sizeof (DCB);

// Get the default port setting information.
GetCommState (hCom, &amp;dcb);

// Change the DCB structure settings.
dcb.BaudRate = 19200;              // Current baud
//dcb.fBinary = TRUE;               // Binary mode; no EOF check
dcb.fParity = TRUE;               // Enable parity checking
dcb.fOutxCtsFlow = FALSE;         // No CTS output flow control
dcb.fOutxDsrFlow = FALSE;         // No DSR output flow control
//dcb.fDtrControl = DTR_CONTROL_ENABLE;
                                      // DTR flow control type
//dcb.fDsrSensitivity = FALSE;      // DSR sensitivity
//dcb.fTXContinueOnXoff = TRUE;     // XOFF continues Tx
dcb.fOutX = FALSE;                // No XON/XOFF out flow control
dcb.fInX = FALSE;                 // No XON/XOFF in flow control
dcb.fErrorChar = FALSE;           // Disable error replacement
dcb.fNull = FALSE;                // Disable null stripping
//dcb.fRtsControl = RTS_CONTROL_ENABLE;
                                      // RTS flow control
dcb.fAbortOnError = FALSE;        // Do not abort reads/writes on
                                      // error
dcb.ByteSize = 8;                 // Number of bits/byte, 4-8
dcb.Parity = NOPARITY;            // 0-4=no,odd,even,mark,space
dcb.StopBits = ONESTOPBIT;        // 0,1,2 = 1, 1.5, 2

// Configure the port according to the specifications of the DCB
// structure.
if (!SetCommState (hCom, &amp;dcb))
{
  // Could not configure the serial port.
  printf(&quot;%d\n&quot;,GetLastError());

  return FALSE;
}
else
printf(&quot;Jo alles klar\n&quot;);

//////////////////////////////////////////////////////////////////////////

// Retrieve the timeout parameters for all read and write operations
// on the port. 

GetCommTimeouts (hCom, &amp;CommTimeouts);

// Change the COMMTIMEOUTS structure settings.
CommTimeouts.ReadIntervalTimeout = MAXDWORD;
CommTimeouts.ReadTotalTimeoutMultiplier = 0;
CommTimeouts.ReadTotalTimeoutConstant = 0;
CommTimeouts.WriteTotalTimeoutMultiplier = 10;  
CommTimeouts.WriteTotalTimeoutConstant = 1000;    

// Set the timeout parameters for all read and write operations
// on the port. 
if (!SetCommTimeouts (hCom, &amp;CommTimeouts))
{
  // Could not set the timeout parameters.
printf(&quot;error:%d\n&quot;,GetLastError ());
  return FALSE;
}
else
   printf(&quot;timeouts ok\n&quot;);

memset(buf,0,MAX);

   while(rc)
   {
        memset(buf,0,MAX);

        gets(buf);
        if(buf[0]=='?')
           exit(1);

        rc = WriteFile(hCom,&amp;buf,strlen(buf),&amp;wieviel,NULL);
        printf(&quot;rc:%i|buf:%s&quot;,rc,buf);
    }

   printf(&quot;Fehler:%i|%d\n&quot;,rc,GetLastError());

    getchar();
    CloseHandle(hCom);
    CloseHandle(hThread);
    return 0;

}

unsigned __stdcall SecondThreadFunc( void* pArguments )
{

  char buf[MAX];
  DWORD wieviel;
  int rc=1;

  while(rc)
  {
      memset(buf,0,MAX);

     rc = ReadFile(hCom,&amp;buf,MAX/*strlen(buf)*/,&amp;wieviel,NULL);
          printf(&quot;____rc:%i|-- %s____&quot;,rc,buf);

  }

  printf(&quot;Fehler:%i\n&quot;,rc);

  _endthreadex(0);
  return 0;

}
</code></pre>
<p>wurde teilweise einfach aus der msdn kopiert.</p>
<p>Sieht hier jemand n Fehler?</p>
<p>Gruß<br />
Scrat1</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/95986/serielle-schnittstelle-mit-3-drähten-readfile</link><generator>RSS for Node</generator><lastBuildDate>Sun, 26 Apr 2026 18:29:19 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/95986.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 26 Dec 2004 16:41:09 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Serielle Schnittstelle mit 3 Drähten,   ReadFile() on Sun, 26 Dec 2004 16:42:19 GMT]]></title><description><![CDATA[<p>Hallo,<br />
also ich hab mir ne Verbindung zwischen 2 PCs mit 3 Drähten über die Rs232-Schnittstelle gebaut.</p>
<p>Ich hab mir dann auch n VB programm heruntergeladen. Mit dem Programm kann ich dann &quot;chatten&quot;.</p>
<p>Nun hab ich versucht das gleiche in C zu machen. Wenn ich auf dem PC1 des VB-prog hab und auf PC2 mein eigenes, dann kann ich von PC2 auf PC1 auch chararrays senden, aber nicht empfangen.<br />
D.h. mit dem c prog kann ich nur senden, ich will aber auch empfangen.</p>
<p>hierm mein code:</p>
<pre><code class="language-cpp">#include &lt;stdio.h&gt;
#include &lt;windows.h&gt;
#include &lt;process.h&gt;

#define MAX 256

unsigned __stdcall SecondThreadFunc(void* pArguments);

HANDLE hCom;

int main()
{
  // CREATE THREAD
  HANDLE hThread;
  unsigned threadID;

  COMMTIMEOUTS CommTimeouts;

  DWORD wieviel;//dwEvtMask;
  DCB dcb;
  DWORD dw = GetLastError();
  char buf[MAX];
  int boll = 0;
  int rc = 1;

  hThread = (HANDLE)_beginthreadex( NULL, 0, &amp;SecondThreadFunc, NULL, 0, &amp;threadID );

  hCom = CreateFile(&quot;COM2&quot;,GENERIC_READ|GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);  //anstatt NULL : FILE_FLAG_OVERLAPPED
  if(hCom == INVALID_HANDLE_VALUE)
  {
        printf(&quot;COM Port ist besetzt&quot;);
        getchar();
        return 1;
  }
  else
        printf(&quot;COM Port OK\n&quot;);

  boll = SetupComm(hCom, MAX, MAX); // set buffer sizes
  if(boll!=0)
       printf(&quot;1 alles ok\n&quot;);
  else
       printf(&quot;1 nö Fehler\n&quot;);

  getchar();

    ///////////////////////////////////////////////////////

// Initialize the DCBlength member.
dcb.DCBlength = sizeof (DCB);

// Get the default port setting information.
GetCommState (hCom, &amp;dcb);

// Change the DCB structure settings.
dcb.BaudRate = 19200;              // Current baud
//dcb.fBinary = TRUE;               // Binary mode; no EOF check
dcb.fParity = TRUE;               // Enable parity checking
dcb.fOutxCtsFlow = FALSE;         // No CTS output flow control
dcb.fOutxDsrFlow = FALSE;         // No DSR output flow control
//dcb.fDtrControl = DTR_CONTROL_ENABLE;
                                      // DTR flow control type
//dcb.fDsrSensitivity = FALSE;      // DSR sensitivity
//dcb.fTXContinueOnXoff = TRUE;     // XOFF continues Tx
dcb.fOutX = FALSE;                // No XON/XOFF out flow control
dcb.fInX = FALSE;                 // No XON/XOFF in flow control
dcb.fErrorChar = FALSE;           // Disable error replacement
dcb.fNull = FALSE;                // Disable null stripping
//dcb.fRtsControl = RTS_CONTROL_ENABLE;
                                      // RTS flow control
dcb.fAbortOnError = FALSE;        // Do not abort reads/writes on
                                      // error
dcb.ByteSize = 8;                 // Number of bits/byte, 4-8
dcb.Parity = NOPARITY;            // 0-4=no,odd,even,mark,space
dcb.StopBits = ONESTOPBIT;        // 0,1,2 = 1, 1.5, 2

// Configure the port according to the specifications of the DCB
// structure.
if (!SetCommState (hCom, &amp;dcb))
{
  // Could not configure the serial port.
  printf(&quot;%d\n&quot;,GetLastError());

  return FALSE;
}
else
printf(&quot;Jo alles klar\n&quot;);

//////////////////////////////////////////////////////////////////////////

// Retrieve the timeout parameters for all read and write operations
// on the port. 

GetCommTimeouts (hCom, &amp;CommTimeouts);

// Change the COMMTIMEOUTS structure settings.
CommTimeouts.ReadIntervalTimeout = MAXDWORD;
CommTimeouts.ReadTotalTimeoutMultiplier = 0;
CommTimeouts.ReadTotalTimeoutConstant = 0;
CommTimeouts.WriteTotalTimeoutMultiplier = 10;  
CommTimeouts.WriteTotalTimeoutConstant = 1000;    

// Set the timeout parameters for all read and write operations
// on the port. 
if (!SetCommTimeouts (hCom, &amp;CommTimeouts))
{
  // Could not set the timeout parameters.
printf(&quot;error:%d\n&quot;,GetLastError ());
  return FALSE;
}
else
   printf(&quot;timeouts ok\n&quot;);

memset(buf,0,MAX);

   while(rc)
   {
        memset(buf,0,MAX);

        gets(buf);
        if(buf[0]=='?')
           exit(1);

        rc = WriteFile(hCom,&amp;buf,strlen(buf),&amp;wieviel,NULL);
        printf(&quot;rc:%i|buf:%s&quot;,rc,buf);
    }

   printf(&quot;Fehler:%i|%d\n&quot;,rc,GetLastError());

    getchar();
    CloseHandle(hCom);
    CloseHandle(hThread);
    return 0;

}

unsigned __stdcall SecondThreadFunc( void* pArguments )
{

  char buf[MAX];
  DWORD wieviel;
  int rc=1;

  while(rc)
  {
      memset(buf,0,MAX);

     rc = ReadFile(hCom,&amp;buf,MAX/*strlen(buf)*/,&amp;wieviel,NULL);
          printf(&quot;____rc:%i|-- %s____&quot;,rc,buf);

  }

  printf(&quot;Fehler:%i\n&quot;,rc);

  _endthreadex(0);
  return 0;

}
</code></pre>
<p>wurde teilweise einfach aus der msdn kopiert.</p>
<p>Sieht hier jemand n Fehler?</p>
<p>Gruß<br />
Scrat1</p>
]]></description><link>https://www.c-plusplus.net/forum/post/681182</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/681182</guid><dc:creator><![CDATA[Scrat1]]></dc:creator><pubDate>Sun, 26 Dec 2004 16:42:19 GMT</pubDate></item><item><title><![CDATA[Reply to Serielle Schnittstelle mit 3 Drähten,   ReadFile() on Sun, 26 Dec 2004 17:27:23 GMT]]></title><description><![CDATA[<p>Der zweite Thread läuft sofort nach dem _beginthreadex los. Zu dem Zeitpunkt hast du hCom aber noch gar nicht initialisiert.</p>
<p>Außerdem solltest du im zweiten Thread irgendeine Abbruchbedingung in die while-Schleife einfügen und am Ende nicht _endthreadex aufrufen, sondern einfach mit return zurückkehren.</p>
<p>Allerdings: Poste doch bitte nur die relevanten Code-Teile (welche das sind, kannst du z.B. mit dem Debugger ermitteln). Dann bekommst du viel eher Hilfe.</p>
<p>p.s.: Deine Fehlerbehandlung sieht etwas seltsam aus. Wenn du einen Fehler feststellst, solltest du das Programm kontrolliert beenden lassen oder den Fehler korrigieren. Du gibst aber nur eine Fehlermeldung aus und machst mit den ungültigen Variablen weiter.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/681201</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/681201</guid><dc:creator><![CDATA[Christoph]]></dc:creator><pubDate>Sun, 26 Dec 2004 17:27:23 GMT</pubDate></item><item><title><![CDATA[Reply to Serielle Schnittstelle mit 3 Drähten,   ReadFile() on Mon, 27 Dec 2004 23:29:54 GMT]]></title><description><![CDATA[<p>Danke für die Hinweise.</p>
<p>Hab den Threadfehlerbehoben, dannach lief alles.</p>
<p>Hier der komplette Code(funktioniert wunderbar):</p>
<pre><code class="language-cpp">#include &lt;stdio.h&gt;
#include &lt;windows.h&gt;
#include &lt;process.h&gt;

#define MAX 256

unsigned __stdcall SecondThreadFunc(void* pArguments);
HANDLE hCom;

int main()
{
  // CREATE THREAD
  HANDLE hThread;
  unsigned threadID;

  COMMTIMEOUTS CommTimeouts;
  DWORD wieviel;//dwEvtMask;
  DCB dcb;
  char buf[MAX];
  int boll = 0;
  int rc = 1;

// Initialisiere serielle Schnittstelle
  hCom = CreateFile( &quot;COM2&quot;,
                   GENERIC_READ|GENERIC_WRITE,
                   0,
                   0,
                   OPEN_EXISTING,
                   FILE_FLAG_OVERLAPPED,
                   0);

  if(hCom == INVALID_HANDLE_VALUE)
  {
    printf(&quot;COM Port ist besetzt&quot;);
    getchar();
    return 1;
  }
  else
        printf(&quot;COM Port     O.k.\n&quot;);

// konfiguriere serielle Schnittstelle
  boll = SetupComm(hCom, MAX, MAX); // set buffer sizes
  if(boll!=0)
    printf(&quot;buffer sizes O.k.\n&quot;);
  else
  {
    printf(&quot;buffer sizes Fehler:|%d\n&quot;,GetLastError());
    getchar();
    return FALSE;
  }

// weitere Einstellungen
  dcb.DCBlength = sizeof (DCB);

  GetCommState (hCom, &amp;dcb);

  dcb.BaudRate = 19200;                //CBR_9600;  //CBR_4800;   //und mehr;  einfach mal 9600 oder 19200
  dcb.Parity = NOPARITY;               //EVENPARITY;//ODDPARITY;
  dcb.StopBits = ONESTOPBIT;          //ONESTOPBIT;//TWOSTOPBITS;

  if (!SetCommState (hCom, &amp;dcb))
  {
    printf(&quot;Eintragung der Einstellungen Fehlgeschlagen:|%d\n&quot;,GetLastError());
    getchar();
    return FALSE;
  }
  else
    printf(&quot;Eintragung   O.k.\n&quot;);

// Timeouts der Schnittstelle konfigurieren
  GetCommTimeouts (hCom, &amp;CommTimeouts);

  CommTimeouts.ReadIntervalTimeout = MAXDWORD;
  CommTimeouts.ReadTotalTimeoutMultiplier = 0;  //von msdn 0
  CommTimeouts.ReadTotalTimeoutConstant = 0; //von msdn 0     // von mir 100
  CommTimeouts.WriteTotalTimeoutMultiplier = 10;  //von msdn 10
  CommTimeouts.WriteTotalTimeoutConstant = 1000;    //von msdn 1000

  if (!SetCommTimeouts (hCom, &amp;CommTimeouts))
  {
    printf(&quot;Eintragung der Timeouts fehlgeschlagen:|%d\n&quot;,GetLastError ());
    getchar();
    return FALSE;
  }
  else
    printf(&quot;Timeouts     O.k.\n&quot;);

// Starten des Threads
  hThread = (HANDLE)_beginthreadex( NULL,
                                  0,
                                  &amp;SecondThreadFunc,
                                  NULL,
                                  0,
                                  &amp;threadID );

  if(hThread ==  -1 || hThread==0)
  {
    printf(&quot;Starten des Threads fehlgeschlagen:|%d\n&quot;,GetLastError());
    getchar();
    return FALSE;
  }
  else
     printf(&quot;Thread Start O.k.\n&quot;);

  while(rc)
  {
    memset(buf,0,MAX);
    gets(buf);

    if(buf[0]=='?')
      exit(1);

    rc = WriteFile(hCom,&amp;buf,strlen(buf),&amp;wieviel,NULL);
  }

  printf(&quot;Fehler beim Schreiben:|%d\n&quot;,rc,GetLastError());

  getchar();
  CloseHandle(hCom);
  CloseHandle(hThread);
  _endthreadex(0);
  return 0;
}

unsigned __stdcall SecondThreadFunc( void* pArguments )
{

  char buf[MAX];
  DWORD wieviel;
  int rc=1;

  while(rc)
  {
    memset(buf,0,MAX);
    rc = ReadFile(hCom,&amp;buf,MAX,&amp;wieviel,NULL);

    if(wieviel&gt;0)
      printf(&quot;%s_\n&quot;,buf);
  }

  printf(&quot;Fehler beim Empfangen:|%d\n&quot;,GetLastError());

  //_endthreadex(0);
  return 0;
}
</code></pre>
<p>Sieht hoffentlich besser aus <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>
<p>Gruß<br />
Scrat1</p>
]]></description><link>https://www.c-plusplus.net/forum/post/681613</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/681613</guid><dc:creator><![CDATA[Scrat1]]></dc:creator><pubDate>Mon, 27 Dec 2004 23:29:54 GMT</pubDate></item></channel></rss>