简体   繁体   English

如何在asp.net MVC(串口通讯)中显示从控制器查看的数据

[英]How to display the data to view from controller in asp.net MVC (Serial Communication)

I am trying to build a login system using Arduino and RFID reader in asp.net MVC c#. 我正在尝试使用asp.net MVC c#中的Arduino和RFID阅读器构建登录系统。

My objective is to update my view from the controller every time the client taps his RFID tag to RFID reader. 我的目标是每次客户将RFID标签贴在RFID阅读器上时,都会从控制器更新我的视图。 Whenever the client taps his RFID tag to RFID reader, I want to update the view to display the information of the client. 每当客户将RFID标签点击到RFID阅读器时,我想更新视图以显示客户端的信息。

For now, I am able to read the tag ID from Arduino using the code below but I don't have an idea on how to display my data to view. 现在,我可以使用下面的代码从Arduino读取标签ID,但我不知道如何显示我要查看的数据。 I did searching for this problem in google but I didn't get the answer for asp.net. 我确实在谷歌搜索这个问题,但我没有得到asp.net的答案。

Controller 调节器

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO.Ports;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.Mvc;

namespace read_rfid_example.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {

            SerialPort mySerialPort = new SerialPort("COM3");

            mySerialPort.BaudRate = 9600;
            mySerialPort.Parity = Parity.None;
            mySerialPort.StopBits = StopBits.One;
            mySerialPort.DataBits = 8;
            mySerialPort.Handshake = Handshake.None;
            mySerialPort.RtsEnable = true;

            mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);

            if(!mySerialPort.IsOpen)
                 mySerialPort.Open();


            return View();
        }

        private static void DataReceivedHandler(
                         object sender,
                         SerialDataReceivedEventArgs e)
        {
            SerialPort sp = (SerialPort)sender;
            string indata = sp.ReadExisting();
            Console.WriteLine("Data Received:");
            Console.Write(indata);

            Debug.WriteLine(indata); //  TAG ID: 03 0e 03 06 (output example and want to pass this data to view)
        }
      }
    }

Simple View 简单的看法

@{
    ViewBag.Title = "Index";
}

<h3>@ViewBag.TagId</h3>

My problem and my question are how can I update my tag in view every time the RFID reader detects the RFID tag? 我的问题和我的问题是,每当RFID阅读器检测到RFID标签时,我如何在视图中更新我的标签? I need help with this problem. 我需要帮助解决这个问题。 Someone can give me a proper way to make this like real-time? 有人能给我一个正确的方法让它像实时一样吗?

if you want see some maybe could be like 如果你想看到一些可能会像

private static void DataReceivedHandler(
                     object sender,
                     SerialDataReceivedEventArgs e)
    {
        SerialPort sp = (SerialPort)sender;
        string indata = sp.ReadExisting();
        Console.WriteLine("Data Received:");
        Console.Write(indata);
        Session["RfidCurrent"] = indata;
        Debug.WriteLine(indata); //  TAG ID: 03 0e 03 06 (output example and want to pass this data to view)
    }


    [HttpGet] 
    public ActionResult Index()
    {
        ViewBag.TagId = Session["RfidCurrent"]
        return View();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM