Hello I'm new to mvc 4 and c#

I need read a data from com port and write it automatically in a textbox; i am using c# and mvc 4 razor.

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.

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();           

        }
    }
}

And the cshtml code is:

@model Altair.Models.IU.FacturaVentaIU

@{
    ViewBag.Title = "Altair";
}

<h2>BienVenido</h2>

@if (ViewBag.registroFallido != null)
{
    @ViewBag.registroFallido
}
<h3>Por favor registre la tarjeta</h3>
@using (Html.BeginForm("registroCodigoTargeta", "Registro", FormMethod.Post))
{
    @Html.AntiForgeryToken() //Pra prevenir ataques CSRF
    @Html.ValidationSummary(true)

    <table>
        <tr>
            <td>@Html.LabelFor(a => a.codigoTarjeta)</td>
            <td>@Html.TextBoxFor(a => a.codigoTarjeta)</td>
            <td>@Html.ValidationMessageFor(a => a.codigoTarjeta)</td>
        </tr>

        <tr>
            <td><input type="submit" value="Generar Factura" /></td>
            <td><input type="reset" value="Limpiar" /></td>
        </tr>
    </table>
}

Many thanks for your help.

Related posts

Recent Viewed