title: is WriteFile working ? C++
body: <p>I have to send 6 byte to a device in serial port. The port is open but the data isnt sent. I use serial port monitor to know what happen with my code(C++ Win32, Visual Studio).   </p>
  
  <p>I am using CreateFile</p>
  
  <pre><code>hPort = CreateFile( TEXT("COM3"),               
                    GENERIC_READ | GENERIC_WRITE,
                    0,                            
                    NULL,                         
                    OPEN_EXISTING,                
                    0,                            
                    NULL );
  </code></pre>
  
  <p>My code to write is:</p>
  
  <pre><code>void uart::write(char data) {
  
  WriteFile(hPort,
      (LPCVOID)data,
      1,
      &amp;byteswritten,
      NULL);
  }
  </code></pre>
  
  <p>When i need execute a command in my device i call fuction sendCommand("000000010000000100000000000000000000000000000000");</p>
  
  <pre><code>void device::sendCommand(std::string command) {
  
  int   size = command.length();
  char* string = (char*)command.c_str();
  std::vector&lt;char&gt; data(command.begin(), command.end()) ;
  
  std::cout &lt;&lt;"[Uart.write]&gt;&gt;\n";                                             
  int j=0;                                                                        
  for (int k = 0; k &lt;= size - 1; k++) {
      Uart.write(data[k]);
      std::cout &lt;&lt; data[k];                                                       
      j++;
      if (j % 8 == 0 &amp;&amp; j!=0) { std::cout &lt;&lt; "__byte " &lt;&lt; j / 8 &lt;&lt; "send___\n";
  }
  </code></pre>
  
  <p>I use the next code like as model to write the above code win32 c++.</p>
  
  <pre><code>       #using &lt;System.dll&gt;
      using namespace System;
      using namespace System::IO::Ports;
      using namespace System::Threading;
  
      void SendCommand(SerialPort^ port, Byte unit, Byte command, Int32 data);
      int _tmain(int argc, _TCHAR* argv[])
  {
      SerialPort^ port;
      Byte unit;
      Byte command;
      Int32 data;
  
      // Set up serial port
      port = gcnew SerialPort();
      port-&gt;PortName = "COM8";
      port-&gt;BaudRate = 9600;
      port-&gt;DataBits = 8;
      port-&gt;Parity   = Parity::None;
      port-&gt;StopBits = StopBits::One;
      port-&gt;Handshake = Handshake::None;
  
      // Open port
      port-&gt;Open();
  
      // Home device 1
      SendCommand(port, 1, CMD_HOME, 0);
      WaitForReply(port, 1, CMD_HOME, unit, command, data);
  // Close port
      port-&gt;Close();
  
      return 0;
  }
      void SendCommand(SerialPort^ port, Byte unit, Byte command, Int32 data)
      {
          array&lt;Byte&gt;^ packet = gcnew array&lt;Byte&gt;(6);
      gcnew array&lt;Byte&gt;
          packet[0] = unit;
          packet[1] = command;
  
          packet[2] = data &amp; 0xFF;
          packet[3] = (data &gt;&gt;  8) &amp; 0xFF;
          packet[4] = (data &gt;&gt; 16) &amp; 0xFF;
          packet[5] = (data &gt;&gt; 24) &amp; 0xFF;
  
          port-&gt;Write(packet, 0, 6);
      }
  </code></pre>
  
  <p>I would like find something like <code>array&lt;Byte&gt;^ packet = gcnew array&lt;Byte&gt;(6);
          gcnew array&lt;Byte&gt;
              packet[0] = unit; ..........</code> in C++. I think my problem is that.</p>