title: C# serial port communication example
body: <p>I was wandering if it's possible to do (and it's well implemented) my next question:</p>
  
  <p>I want to talk to a PLC, via serial communication. Each time I send data to the PLC it will send me data back and we don't know how much time it will take him to send me back (Some millisconds but don't how much). </p>
  
  <p>I wanted to create a class for serial communication (between PC and the PLC) with <strong>separated</strong> methods:</p>
  
  <ol>
  <li>Open()--> Opens serial communication with PLC</li>
  <li>Talk2PLC()--> Sends data to PLC and waits to receive back data and read it</li>
  <li>Close()--> Close serial communication with PLC</li>
  </ol>
  
  <p>The way it will work will be: When project starts, we open serial communication with PLC. Then during the execution of the program we will only use Talk2PLC() to communicate with PLC, we will call this method several times. When data from PLC is received it will be processed in ProcessData(). When all the work is done then we will close communication with the PLC, we call Close().</p>
  
  <pre><code>   !!It's import methods are separated¡¡
  </code></pre>
  
  <h2>Questions:</h2>
  
  <ul>
  <li><p>Will it work fine the way I do even if "open" and "close" are separated?</p></li>
  <li><p>In Talk2PLC() will I be able to read data send back from PLC even if the answer is not instant? Do I need some kind of delay in case I need to wait? Will my event handler com_Datareceive() be able to read it? "ReadTimeout" and "WriteTimeout" should do the trick, i think.</p></li>
  <li>After I call Open() and then call Talk2PLC() for the first time, the next times I will call Talk2PLC() will I be able to send and receive data from PLC?</li>
  </ul>
  
  <p>a</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.IO.Ports;
  
  namespace Project
  {
      class Serial
      {
          // Serial port settings.
          SerialPort com = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);
  
          List&lt;byte&gt; PlcBuffer = new List&lt;byte&gt;();
  
          // Open serial communication with PLC.
          public void Open()
          {
              com.Open();          
          }
  
          /* Query PLC and get answer.
           * @cmd: command to be send.
           */
          public void Talk2PLC(byte[] cmd)
          {
  
              PlcBuffer.Clear();//Clear previous data before storing new one.
              // Set the read/write timeouts
              com.ReadTimeout = 1000;
              com.WriteTimeout = 1000;
  
              //Write. Send data to PLC
              com.Write(cmd, 0, cmd.Length);
              //Read. Wait for an answer from PLC.
              com.DataReceived += new SerialDataReceivedEventHandler(com_DataReceived);
          }
  
          // Gets all data send by the PLC.
          public void com_DataReceived(object sender, SerialDataReceivedEventArgs e)
          {
              // Buffer and process binary data
              while (com.BytesToRead &gt; 0)
                  PlcBuffer.Add((byte)com.ReadByte());
  
              ProcessData(PlcBuffer);   //Send data to be processed
          }//End com_DataReceived()
  
          // This will process data from PLC
          public void ProcessData(List&lt;byte&gt; buffer)
          {
              //Process data
          }
  
          // Close serial communication with PLC.
          public void Close()
          {
                  com.Close();
          }
  
      }
  }
  </code></pre>
  
  <p>Thanks to everyone who read all and tried to help :)</p>