Click here to Skip to main content
15,889,034 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Posted
Updated 22-Apr-19 3:20am
Comments
Dave Kreskowiak 21-Apr-19 22:07pm    
You do realize that your code will only ever work if the RFID reader is attached directly to the server, right?

If you deploy this into production, this code will not work at all if you connect the RFID reader to a workstation.

ASP.NET code runs ENTIRELY on the server side, never the client side.

Typically, something like this means you didn't get the communication parameters correct when setting up the serial port.
 
Share this answer
 
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.
 
Share this answer
 
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900