title: C++ Reading from a serial port with CreateFile
body: <p>I am writing a C++ program to read from a serial port (in my case COM6). To open the COM-Port. I always found this code in the internet:</p>
  
  <pre><code>HANDLE serialHandle;
  
  serialHandle = CreateFile(L"COM6", GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  </code></pre>
  
  <p>My Problem is that I get the following errors:</p>
  
  <ul>
  <li>'HANDLE': identifier not found</li>
  <li>'CreateFile': identifier not found</li>
  <li>'GENERIC_READ': undeclared identifier</li>
  <li>'INVALID_HANDLE_VALUE': undeclared identifier</li>
  <li>...</li>
  </ul>
  
  <p>My code:</p>
  
  <pre><code>#include &lt;windows.h&gt;
  #include "stdafx.h"
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;tchar.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;strsafe.h&gt;
  using namespace std;
  
  
  int main()
  {
      /*int comPortNmr = 6, speed = 115200;
      cout &lt;&lt; "Serial Line: &gt; ";
      cin &gt;&gt; comPortNmr;
      cout &lt;&lt; endl;
      cout &lt;&lt; "Speed: &gt; ";
      cin &gt;&gt; speed;
      cout &lt;&lt; endl; */
  
      HANDLE serialHandle;
      // Open serial port
      serialHandle = CreateFile(L"COM6", GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  
      if (serialHandle == INVALID_HANDLE_VALUE)
      {
          cout &lt;&lt; "Error." &lt;&lt; endl;
      }
      else
      {
          cout &lt;&lt; "Opend." &lt;&lt; endl;
      }
  
      return 0;
  }
  </code></pre>
  
  <p>What am I doing wrong?</p>