title: How I can write data from a COM port on a text box?, c# mvc 4, razor
body: <p>Hello I'm new to mvc 4 and c#</p>
  
  <p>I need read a data from com port and write it automatically in a textbox; i am using c# and mvc 4 razor.</p>
  
  <p>I have the data from com port with the next code, but i don´t know how read and write this data in the textbox.</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Threading.Tasks;
  using System.IO.Ports;
  
  namespace Altair.Models.Business
  {
      public class ComPort
      {
          private string indata = "";
          public ComPort()
          {
              SerialPort mySerialPort = new SerialPort("COM6");
  
              mySerialPort.BaudRate = 9600;
              mySerialPort.Parity = Parity.None;
              mySerialPort.StopBits = StopBits.One;
              mySerialPort.DataBits = 8;
              mySerialPort.Handshake = Handshake.None;
  
              mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
  
              mySerialPort.Open();
  
              mySerialPort.Close();
          }
  
          public string getIndata(){
              return this.indata;
          }
  
          private void DataReceivedHandler(
                         object sender,
                         SerialDataReceivedEventArgs e)
          {
              SerialPort sp = (SerialPort)sender;
              indata = sp.ReadExisting();           
  
          }
      }
  }
  </code></pre>
  
  <p>And the cshtml code is:</p>
  
  <pre><code>@model Altair.Models.IU.FacturaVentaIU
  
  @{
      ViewBag.Title = "Altair";
  }
  
  &lt;h2&gt;BienVenido&lt;/h2&gt;
  
  @if (ViewBag.registroFallido != null)
  {
      @ViewBag.registroFallido
  }
  &lt;h3&gt;Por favor registre la tarjeta&lt;/h3&gt;
  @using (Html.BeginForm("registroCodigoTargeta", "Registro", FormMethod.Post))
  {
      @Html.AntiForgeryToken() //Pra prevenir ataques CSRF
      @Html.ValidationSummary(true)
  
      &lt;table&gt;
          &lt;tr&gt;
              &lt;td&gt;@Html.LabelFor(a =&gt; a.codigoTarjeta)&lt;/td&gt;
              &lt;td&gt;@Html.TextBoxFor(a =&gt; a.codigoTarjeta)&lt;/td&gt;
              &lt;td&gt;@Html.ValidationMessageFor(a =&gt; a.codigoTarjeta)&lt;/td&gt;
          &lt;/tr&gt;
  
          &lt;tr&gt;
              &lt;td&gt;&lt;input type="submit" value="Generar Factura" /&gt;&lt;/td&gt;
              &lt;td&gt;&lt;input type="reset" value="Limpiar" /&gt;&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/table&gt;
  }
  </code></pre>
  
  <p>Many thanks for your help.</p>