title: How to send command to RS232 serial port?
body: <p>I recently got a sas expander card.</p>
  <p>The one who gave card to me said:</p>
  <p>It has a firmware on the chip and it can show sensor's temperature.</p>
  <p>He wants me to develop a <code>C#</code> <code>Console app</code> to execute the firmware.</p>
  <p>I didn't know the firmware source code looks like.</p>
  <p>But it could be executed by <code>PuTTy</code> and it's connection was via <code>RS232</code> Serial Port.</p>
  <p><code>PuTTy</code> connection setting:</p>
  <p><a href="https://i.stack.imgur.com/4Y0Q1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4Y0Q1.png" alt="enter image description here" /></a></p>
  <p><a href="https://i.stack.imgur.com/2JE11.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/2JE11.png" alt="enter image description here" /></a></p>
  <p>After I click <code>Open</code>,press <code>Enter</code> and type command <code>sys</code>:</p>
  <p><a href="https://i.stack.imgur.com/vJ4rd.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vJ4rd.png" alt="enter image description here" /></a>
  <a href="https://i.stack.imgur.com/QVXpc.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QVXpc.png" alt="enter image description here" /></a>
  <a href="https://i.stack.imgur.com/oDiei.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/oDiei.png" alt="enter image description here" /></a></p>
  <p>What I try in my C# code:</p>
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.IO.Ports;
  using System.Threading;
  
  
  namespace SerialPortExample
  {
      class SerialPortProgram
      {
          // Create the serial port with basic settings
          [STAThread]
          static void Main()
          {
             
             
              SerialPort mySerialPort = new SerialPort(&quot;COM5&quot;);
  
              mySerialPort.BaudRate = 115200;
              mySerialPort.Parity = Parity.None;
              mySerialPort.StopBits = StopBits.One;
              mySerialPort.DataBits = 8;
              mySerialPort.Handshake = Handshake.None;
              mySerialPort.RtsEnable = true;
              mySerialPort.DtrEnable = true;
              mySerialPort.ReadTimeout = 2000;
              mySerialPort.WriteTimeout = 1000;
              mySerialPort.Open();
              if(mySerialPort.IsOpen)
              { 
                  string str= &quot;Enter&quot;;
                  mySerialPort.Write(str);
   
              }
              mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
              
              Console.ReadLine();
              
          }
          private static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
          {
              SerialPort sp = (SerialPort)sender;
              string indata = sp.ReadExisting();
              Console.WriteLine(&quot;Data Received:&quot;);
              Console.Write(indata);
          }
  
  
      }
  }
  </code></pre>
  <p>What my code execute:</p>
  <p><a href="https://i.stack.imgur.com/cTcK2.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/cTcK2.png" alt="enter image description here" /></a></p>
  <p>What is the problem with my code?</p>
  <p>How can I execute firmware via <code>RS232</code> and interact like <code>PuTTy</code> have done?</p>
  <p>My Console app is using <code>.Net Framework 4.7.2</code>.</p>