USB RFID reading tag using C# connected through serial port

社会主义新天地 提交于 2019-12-25 18:37:50

问题


I have manage to connect to my RFID devices through USB port with a console application using the code below

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Net;
    using System.Net.Sockets;
    using System.IO.Ports;
    using System.Threading;
    using System.Windows.Forms;
    using Gtk;

    namespace Testing1
    {
        public class Testing1
        {
            public static SerialPort iSerialPort = new SerialPort();

            static int Main() 
            {
                string strException = string.Empty;
            string strComPort = "COM17";
                int nBaudrate=Convert.ToInt32(9600);

                int nRet = OpenCom(strComPort, nBaudrate, out strException);
                if (nRet != 0)
                {
                    string strLog = "Connect reader failed, due to: " + strException; 
                    Console.WriteLine(strLog);
                    //return;
                }
                else
                {
                    string strLog = "Reader connected " + strComPort + "@" + nBaudrate.ToString();
                    Console.WriteLine(strLog);
                }

                Console.WriteLine("Press any key to exit.");
                Console.ReadKey();

                iSerialPort.Close();
                return 0;
            }

            public static int OpenCom(string strPort, int nBaudrate, out string strException)
            {

                strException = string.Empty;

                if (iSerialPort.IsOpen)
                {
                    iSerialPort.Close();
                }

                try
                {
                    iSerialPort.PortName = strPort;
                    iSerialPort.BaudRate = nBaudrate;
                    iSerialPort.ReadTimeout = 3000;
                    iSerialPort.DataBits = 8;
                    iSerialPort.Parity = Parity.None;
                    iSerialPort.StopBits = StopBits.One;
                    iSerialPort.Open();
                }
                catch (System.Exception ex)
                {
                    strException = ex.Message;
                    return -1;
                }



                return 0;
            }
        }
    }

However, now i would like to read data from a RFID tag. Is it possible through console application? Or do i have to have a normal GUI application? The application should allow multiple tag read every time a tag is within the reading range.


回答1:


Please see my answers below:

  1. It's possible to read data using a console application
  2. In order to read data you need to subscribe to DataReceived event like below:

        SerialPort mySerialPort = new SerialPort("COM1");
        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();
    

More information is available here:

http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.datareceived(v=vs.110).aspx




回答2:


You have to check in what COM does your computer recognice the reader. Start - Control Panel - Device Admin - PORTS (COM and LPT) and change it in the code of Toan Nguyen.



来源:https://stackoverflow.com/questions/23799474/usb-rfid-reading-tag-using-c-sharp-connected-through-serial-port

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!