本文介绍了在ASP.NET MVC中的串行端口通信中获得随机标记ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在尝试在asp.net中提取正确的RFID TAG ID,但我得到了随机标签ID。 我期待这个数据标签ID在我的ajax请求中返回 但是我有一个随机ID标签而不是 我尝试过: 这是我的代码 < pre>使用System; 使用System.Collections.Generic; 使用System.Diagnostics; 使用System.IO.Ports; 使用System.Linq; 使用System.Threading; 使用System.Web; 使用System.Web.Mvc; public delegate void displayToView(String tagID); 名称空间read_rfid_example.Controllers { public class HomeController:Controller { SerialPort mySerialPort = new SerialPort(COM3); // GET:Home public ActionResult Index() { return View(); } List< string> mlist = new List< string>(); private void DataReceivedHandler( object sender, SerialDataReceivedEventArgs e) { SerialPort sp =(SerialPort)sender; string indata = sp.ReadExisting(); displayToView display = new displayToView(pageReload); display.Invoke(indata); } string result =; [HttpGet] public JsonResult start() { 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); 尝试 { mySerialPort.Open(); } catch { } while(mySerialPort.IsOpen) { Thread.Sleep (100); } 返回Json(结果,JsonRequestBehavior.AllowGet); } string [] str = new string [10]; int count = 0; public void pageReload(String text) { mlist.Add(text); count ++; str [mlist.Count] = mlist [mlist.Count - 1]; if(count == 5) { result = string.Join(,str); mySerialPort.Close(); } } } } 如何返回正确的TAG ID?请帮助解决方案 这不是随机的! 你开始阅读以前读数中剩余的缓冲区。行开头的随机读数是前一个标记的结尾。 您需要了解串口很慢,您可能需要获取数据才能达到计算机。 您需要在代码中添加对接收数据的理解,以检测标记的开头或结尾。 I am trying to extract the correct RFID TAG ID in asp.net but I got the random tag id instead.I'm expecting this data tag ID to be returned in my ajax requestbut I've got a random ID Tag insteadWhat I have tried:Here is my code<pre>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;public delegate void displayToView(String tagID);namespace read_rfid_example.Controllers{ public class HomeController : Controller { SerialPort mySerialPort = new SerialPort("COM3"); // GET: Home public ActionResult Index() { return View(); } List<string> mlist = new List<string>(); private void DataReceivedHandler( object sender, SerialDataReceivedEventArgs e) { SerialPort sp = (SerialPort)sender; string indata = sp.ReadExisting(); displayToView display = new displayToView(pageReload); display.Invoke(indata); } string result = ""; [HttpGet] public JsonResult start() { 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); try { mySerialPort.Open(); } catch { } while(mySerialPort.IsOpen) { Thread.Sleep(100); } return Json(result, JsonRequestBehavior.AllowGet); } string[] str = new string[10]; int count = 0; public void pageReload(String text) { mlist.Add(text); count++; str[mlist.Count] = mlist[mlist.Count - 1]; if (count == 5) { result = string.Join("", str); mySerialPort.Close(); } } }}How to return the correct TAG ID? Please help 解决方案 This is not random !You start reading with what remains in buffer from previous reading. The random reading at beginning of line is the end of previous tag.You need to understand that a serial port is slow, and you may need to way for data to reach the computer.You need to add an understanding of received data in your code to detect either a start or an end of tag. 这篇关于在ASP.NET MVC中的串行端口通信中获得随机标记ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-21 04:54