title: C# Write byte to serial port like failed
body: <p>Why the byte output from another program is like nothing happening when I use it on own program? The data is from sample program:-</p>
  <p><a href="https://i.stack.imgur.com/7YkyV.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7YkyV.png" alt="enter image description here" /></a></p>
  <p>When I use the <code>A5 00 01 02 13 02 01 55</code> in my program, nothing happen while the sample program can trigger the light on device.</p>
  <p>My code:-</p>
  <pre><code>using System;
  using System.Text;
  using ComTypes = System.Runtime.InteropServices.ComTypes;
  using System.Runtime.InteropServices;
  using System.Collections.Generic;
  using System.IO.Ports;
  using COMLib;
  using System.Linq;
  
  namespace PortLPT
  {
      class Program
      {
          static SerialPort twrport = new SerialPort();
          static string InputData = String.Empty;
          static string comname;
          [STAThread]
          static void Main(string[] args)
          {
              COMLibrary comport = new COMLibrary();
              var comdevice = comport.GetAllCOMPorts();
              foreach(var devinfo in comdevice)
              {
                  Console.WriteLine($&quot;Port Number: {devinfo.name}.\nPort Description: {devinfo.decsription}.\n{new string('=',&quot;Port Description&quot;.Length + devinfo.decsription.Length + 3)}&quot;);
                  if (devinfo.decsription.Contains(&quot;MOXA&quot;))
                      comname = devinfo.name;
              }
  
              twrport.PortName = comname;
              twrport.BaudRate = 9600;
              twrport.DataBits = 8;
              twrport.StopBits = StopBits.One;
              twrport.Parity = Parity.None;
              twrport.Open();
              twrport.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
              twrport.RtsEnable = true;
              twrport.DtrEnable = true;   
  
  
              if (!(twrport.IsOpen))
              {
                  try
                  {
                      twrport.Open();
                      Console.WriteLine(&quot;Serial port connection successfully open.&quot;);
                  }
                  catch (UnauthorizedAccessException ex)
                  {
                      Console.WriteLine($&quot;Exception on opening serial port connection. Ex-{ex.Message}&quot;);
                  }
              }
              else
              {
                  Console.WriteLine(&quot;Serial port connection successfully open.&quot;);
              }
  
              try
              {
                  string str = &quot;A5 00 01 02 13 02 01 55&quot;;
                  byte[] bytes = str.Split(' ').Select(s =&gt; Convert.ToByte(s, 16)).ToArray();
  
                  byte[] buffer = new byte[8] { 0xA5, 0x00, 0x01, 0x02, 0x13, 0x02, 0x01, 0x55 };
                  if (!(twrport.IsOpen))
                      twrport.Open();
                  twrport.Write(bytes, 0, bytes.Length);
                  twrport.Close();
                  Console.WriteLine($&quot;Write to port: {PrintBytes(bytes)}&quot;);
              }
              catch(Exception ex)
              { Console.WriteLine($&quot;Error write to port. Ex-{ex.Message.ToString()}&quot;); twrport.Close(); }
              
  
  
              Console.ReadLine();
          }
          static void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
          {
              // Show all the incoming data in the port's buffer
              Console.WriteLine(twrport.ReadExisting());
          }
          static string PrintBytes(byte[] byteArray)
          {
              var sb = new StringBuilder(&quot;{&quot;);
              for (var i = 0; i &lt; byteArray.Length; i++)
              {
                  var b = byteArray[i];
                  sb.Append(b);
                  if (i &lt; byteArray.Length - 1)
                  {
                      sb.Append(&quot;, &quot;);
                  }
              }
              sb.Append(&quot;}&quot;);
              return sb.ToString();
          }
      }
  }
  </code></pre>
  <p>I have try two kind of byte style which is variable <code>bytes[]</code> and <code>buffer[]</code>. Both not success. How do I make this write to serial port accordingly?</p>
  <p>I'm not so expert on this. I have try some ade write to serial port link from search engine result and still failed to trigger the device</p>
  <h1>Updated</h1>
  <p><a href="https://i.stack.imgur.com/Zd4zk.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Zd4zk.png" alt="enter image description here" /></a></p>