title: How do I send bytes to a serial device in C#?
body: <p>I have a device that uses serial (via a USB adaptor) to interface with my PC. I'm having real difficulty getting it to play nicely in C#. I know that it works properly because the vendor-supplied software behaves as expected. I also know that I am able to receive data using my code thanks to a test mode which repeatedly sends "OK".</p>
  
  <p>Here's my code:</p>
  
  <pre><code>    private SerialPort port;
  
      public SerialConnection()
      {
          this.port = new SerialPort("COM3", 38400, Parity.None, 8, StopBits.One);
          this.port.WriteTimeout = 2000; port.ReadTimeout = 2000;
  
          this.port.Open();
  
          this.port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
      }
  
      public void SendCommand(byte[] command)
      {
          this.port.Write(command,0,command.Length);
          string chars = "";
          foreach (byte charbyte in command) chars += (char)charbyte;
          Console.WriteLine(" -&gt; " + chars);
      }
  
      void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
      {
          string data = this.port.ReadLine();
          Console.WriteLine(" &lt;- " + data);
      }
  </code></pre>
  
  <p>At the moment I'm using another test mode which is supposed to echo back whatever it receives. Accordingly, I'm calling SendCommand with the following bytes:</p>
  
  <pre><code>byte[] {
      0x50,
      0x69,
      0x6E,
      0x67
  }
  </code></pre>
  
  <p>But nothing ever seems to get sent back.</p>
  
  <p>I've no idea what to try next. Anyone got any suggestions?</p>
  
  <hr>
  
  <p>For a subsidiary question I've posted some PortMon logs. I think they might be useful here too, so here they are:</p>
  
  <ol>
  <li><a href="http://tdwright.co.uk/minibird_filtered.log" rel="nofollow">Vendors software</a> - with all the <code>IOCTL_SERIAL_GET_COMMSTATUS</code> entries filtered out</li>
  <li><a href="http://tdwright.co.uk/myapp.log" rel="nofollow">My toe-dip attempt</a></li>
  </ol>