在ASP.NET MVC中的串行端口通信中获得随机标记ID [英] Got a random tag ID in serial port communication in ASP.NET MVC

查看:58
本文介绍了在ASP.NET MVC中的串行端口通信中获得随机标记ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在asp.net中提取正确的RFID TAG ID,但我得到了随机标签ID。



我期待这个数据标签ID在我的ajax请求中返回

期望:

标签UID:0B A5 56 D3





但是我有一个随机ID标签而不是

随机ID标签:

D3标签UID:0B A5 56 D

3标签UID:0B A5 56





我尝试过:



 这是我的代码 

< 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?请帮助

解决方案

通常,这样的事情意味着在设置串口时没有正确的通信参数。


< blockquote>

Quote:

期望:

标签UID:0B A5 56 D3 



但是我有一个随机ID标签而不是

随机ID标签:

 D3标签UID:0B A5 56 D 
3标签UID:0B A5 56



这不是随机的!

你开始阅读以前读数中剩余的缓冲区。行开头的随机读数是前一个标记的结尾。

您需要了解串口很慢,您可能需要获取数据才能达到计算机。

您需要在代码中添加对接收数据的理解,以检测标记的开头或结尾。


串口有一个属性BytesToRead,您需要与在任何给定时间实际读取的字节进行比较。如果你阅读较少,你需要再次阅读。



这就是异步串行处理令人困惑的原因。由于您需要数据,因此使用同步处理更简单:write;等待;读取BytesToRead> 0.



Re:ReadExisting()



Quote:

此方法以字符串形式返回SerialPort对象的流和内部缓冲区的内容。此方法不使用超时。请注意,此方法可以在内部缓冲区中留下尾随前导字节,这会使BytesToRead值大于零。


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 request

Expectation:

Tag UID: 0B A5 56 D3



but I've got a random ID Tag instead

Random ID Tag:

D3 Tag UID: 0B A5 56 D
3 Tag UID: 0B A5 56



What 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

解决方案

Typically, something like this means you didn't get the communication parameters correct when setting up the serial port.


Quote:

Expectation:

Tag UID: 0B A5 56 D3


but I've got a random ID Tag instead
Random ID Tag:

D3 Tag UID: 0B A5 56 D
3 Tag UID: 0B A5 56


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.


The serial port has a property "BytesToRead" that you need to compare with the bytes actual read at any given time. If you read less, you need to read again.

That's why "async" serial processing is confusing. Since you need the data, it's simpler to use synchronous processing: write; wait; read while BytesToRead > 0.

Re: ReadExisting()

Quote:

This method returns the contents of the stream and internal buffer of the SerialPort object as a string. This method does not use a time-out. Note that this method can leave trailing lead bytes in the internal buffer, which makes the BytesToRead value greater than zero.


这篇关于在ASP.NET MVC中的串行端口通信中获得随机标记ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆