<?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[(mal wieder)com port + WriteFile]]></title><description><![CDATA[<p>Ich weiss, das Thema ist mittlerweile ausgelutscht, aber ich bekomme es immernoch nicht hin.</p>
<p>Das Problem ist, dass ich immer &quot;FEHLER #06&quot; bekomme ( =&gt; WriteFile gibt FALSE zurück )<br />
Kann das daran liegen, dass kein gerät am com port angeschlossen ist, oder wie oder was???</p>
<p>hier mal mein quelltext:</p>
<pre><code class="language-cpp">#include &lt;windows.h&gt;
#include &lt;iostream.h&gt;

WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
  HANDLE hCom = CreateFile(&quot;COM1&quot;, GENERIC_WRITE|GENERIC_READ, 0, NULL,
              OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL);
  if(!SetupComm(hCom, 512, 512))
  {
    MessageBoxA(NULL, &quot;FEHLER #01&quot;, &quot;comPort&quot;, NULL);
    return 0;
  }

  DCB dcb;
  ZeroMemory(&amp;dcb, sizeof(DCB));

  char buffer[100];
  strcpy(buffer,&quot;baud=1200 parity=N data=8 stop=1&quot;);
  if (BuildCommDCB((char*)&amp;buffer, &amp;dcb))
  {
    if (dcb.BaudRate != CBR_1200)
    {
      MessageBoxA(NULL, &quot;FEHLER #02&quot;, &quot;comPort&quot;, NULL);
      return 0;
    }
  }
  else
  {
    MessageBoxA(NULL, &quot;FEHLER #03&quot;, &quot;comPort&quot;, NULL);   
    return 0;
  }

  if(GetCommState(hCom, &amp;dcb))
  {
   dcb.BaudRate = CBR_19200;
   dcb.ByteSize = 8;
   dcb.Parity = 0;
   dcb.StopBits = 0;
   SetCommState(hCom, &amp;dcb);
  }
  else
  {
    MessageBoxA(NULL, &quot;FEHLER #04&quot;, &quot;comPort&quot;, NULL);
    return 0;
  }

  if (hCom == INVALID_HANDLE_VALUE)
  {
    MessageBoxA(NULL, &quot;FEHLER #05&quot;, &quot;comPort&quot;, NULL);
    return 0;
  }

  DWORD temp, numbytes_ok;
  COMSTAT ComState;
  OVERLAPPED Overlap;
  char buf_out[1] = &quot;a&quot;;

  ClearCommError(hCom, &amp;temp, &amp;ComState);
  ZeroMemory(&amp;Overlap, sizeof(OVERLAPPED));
  if(!temp)
  {
   if(!WriteFile(hCom, &amp;buf_out, 1, &amp;numbytes_ok, &amp;Overlap))
   {
    MessageBoxA(NULL, &quot;FEHLER #06&quot;, &quot;comPort&quot;, NULL);
    return 0;
   }
  }

  DWORD error;
  error = GetLastError();
  if(error)
   MessageBoxA(NULL, (char *)error, &quot;comPort&quot;, NULL);

  CloseHandle(hCom);

  MessageBoxA(NULL, &quot;Alles im grünen Bereich!&quot;, &quot;comPort&quot;, NULL);

  return 0;
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/114026/mal-wieder-com-port-writefile</link><generator>RSS for Node</generator><lastBuildDate>Wed, 01 Jul 2026 18:56:53 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/114026.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 29 Jun 2005 12:46:36 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to (mal wieder)com port + WriteFile on Wed, 29 Jun 2005 12:46:36 GMT]]></title><description><![CDATA[<p>Ich weiss, das Thema ist mittlerweile ausgelutscht, aber ich bekomme es immernoch nicht hin.</p>
<p>Das Problem ist, dass ich immer &quot;FEHLER #06&quot; bekomme ( =&gt; WriteFile gibt FALSE zurück )<br />
Kann das daran liegen, dass kein gerät am com port angeschlossen ist, oder wie oder was???</p>
<p>hier mal mein quelltext:</p>
<pre><code class="language-cpp">#include &lt;windows.h&gt;
#include &lt;iostream.h&gt;

WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
  HANDLE hCom = CreateFile(&quot;COM1&quot;, GENERIC_WRITE|GENERIC_READ, 0, NULL,
              OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL);
  if(!SetupComm(hCom, 512, 512))
  {
    MessageBoxA(NULL, &quot;FEHLER #01&quot;, &quot;comPort&quot;, NULL);
    return 0;
  }

  DCB dcb;
  ZeroMemory(&amp;dcb, sizeof(DCB));

  char buffer[100];
  strcpy(buffer,&quot;baud=1200 parity=N data=8 stop=1&quot;);
  if (BuildCommDCB((char*)&amp;buffer, &amp;dcb))
  {
    if (dcb.BaudRate != CBR_1200)
    {
      MessageBoxA(NULL, &quot;FEHLER #02&quot;, &quot;comPort&quot;, NULL);
      return 0;
    }
  }
  else
  {
    MessageBoxA(NULL, &quot;FEHLER #03&quot;, &quot;comPort&quot;, NULL);   
    return 0;
  }

  if(GetCommState(hCom, &amp;dcb))
  {
   dcb.BaudRate = CBR_19200;
   dcb.ByteSize = 8;
   dcb.Parity = 0;
   dcb.StopBits = 0;
   SetCommState(hCom, &amp;dcb);
  }
  else
  {
    MessageBoxA(NULL, &quot;FEHLER #04&quot;, &quot;comPort&quot;, NULL);
    return 0;
  }

  if (hCom == INVALID_HANDLE_VALUE)
  {
    MessageBoxA(NULL, &quot;FEHLER #05&quot;, &quot;comPort&quot;, NULL);
    return 0;
  }

  DWORD temp, numbytes_ok;
  COMSTAT ComState;
  OVERLAPPED Overlap;
  char buf_out[1] = &quot;a&quot;;

  ClearCommError(hCom, &amp;temp, &amp;ComState);
  ZeroMemory(&amp;Overlap, sizeof(OVERLAPPED));
  if(!temp)
  {
   if(!WriteFile(hCom, &amp;buf_out, 1, &amp;numbytes_ok, &amp;Overlap))
   {
    MessageBoxA(NULL, &quot;FEHLER #06&quot;, &quot;comPort&quot;, NULL);
    return 0;
   }
  }

  DWORD error;
  error = GetLastError();
  if(error)
   MessageBoxA(NULL, (char *)error, &quot;comPort&quot;, NULL);

  CloseHandle(hCom);

  MessageBoxA(NULL, &quot;Alles im grünen Bereich!&quot;, &quot;comPort&quot;, NULL);

  return 0;
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/819770</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/819770</guid><dc:creator><![CDATA[DerTypMitDerFrage]]></dc:creator><pubDate>Wed, 29 Jun 2005 12:46:36 GMT</pubDate></item><item><title><![CDATA[Reply to (mal wieder)com port + WriteFile on Wed, 29 Jun 2005 15:55:53 GMT]]></title><description><![CDATA[<p>Was meint GetLastError() denn, nachdem WriteFile() fehlschlägt ?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/819934</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/819934</guid><dc:creator><![CDATA[geeky]]></dc:creator><pubDate>Wed, 29 Jun 2005 15:55:53 GMT</pubDate></item><item><title><![CDATA[Reply to (mal wieder)com port + WriteFile on Wed, 29 Jun 2005 18:38:31 GMT]]></title><description><![CDATA[<p>komisch....<br />
wenn ich es laufen lassen will, dann hängt es -&gt; ich kann nicht herausfinden, was mit lastError sagen will.<br />
das programm stürtzt nur ab, wenn die zeile mit WritFile drin ist.<br />
wenn ich das rauskommentiere, läuft es.<br />
(nur ich kann halt nichts machen...)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/820036</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/820036</guid><dc:creator><![CDATA[DerTypMitDerFrage]]></dc:creator><pubDate>Wed, 29 Jun 2005 18:38:31 GMT</pubDate></item><item><title><![CDATA[Reply to (mal wieder)com port + WriteFile on Thu, 30 Jun 2005 05:06:08 GMT]]></title><description><![CDATA[<p>Den Fehler kann man sich anzeigen lassen (MSDN):</p>
<pre><code class="language-cpp">LPVOID lpMsgBuf;
  FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | 
                   FORMAT_MESSAGE_FROM_SYSTEM | 
                   FORMAT_MESSAGE_IGNORE_INSERTS,
                 NULL,
                 GetLastError(),
                 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
                 (LPTSTR) &amp;lpMsgBuf,
                 0,
                 NULL);
  // Display the string.
  MessageBox (NULL, (LPCTSTR)lpMsgBuf, TEXT(&quot;Error WriteFile&quot;), 
              MB_OK | MB_ICONINFORMATION);
  // Free the buffer.
  LocalFree(lpMsgBuf);
</code></pre>
<p>Auch solltest Du im Fehlerfalle nicht return 0; ohne CloseHandle (hCom); machen.<br />
Wozu brauchst Du OVERLAPPED Overlap; ?<br />
Setze es auf NULL, wenn Du es nicht benutzt. Bist Du sicher, was Du mit der Abfrage if (!tmp) bezweckst? MSDN: &quot;This parameter can be <strong>one</strong> or more of the following values. &quot;</p>
<p>Blackbird</p>
]]></description><link>https://www.c-plusplus.net/forum/post/820259</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/820259</guid><dc:creator><![CDATA[Blackbird]]></dc:creator><pubDate>Thu, 30 Jun 2005 05:06:08 GMT</pubDate></item><item><title><![CDATA[Reply to (mal wieder)com port + WriteFile on Thu, 30 Jun 2005 11:42:32 GMT]]></title><description><![CDATA[<p>Was mir gerade so auffällt:</p>
<pre><code>char buf_out[1] = &quot;a&quot;;
// ...
if(!WriteFile(hCom, &amp;buf_out, 1, &amp;numbytes_ok, &amp;Overlap)) &lt;= Ist das &quot;&amp;&quot; vor buf_out da nicht zuviel ?
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/820534</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/820534</guid><dc:creator><![CDATA[geeky]]></dc:creator><pubDate>Thu, 30 Jun 2005 11:42:32 GMT</pubDate></item></channel></rss>