0

I have managed to connect to the RFID USB reader using the code below... However, whenever my tag is placed near the reader. It does not run the function and produce an output of "Reading Data...". Anyone can provide me a functioning RFID coding? Below is my codes written.

        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.Handshake = Handshake.None;
                    iSerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
                    iSerialPort.Open();
                }
                catch (System.Exception ex)
                {
                    strException = ex.Message;
                    return -1;
                }



                return 0;
            }

            private static void DataReceivedHandler(object sender,SerialDataReceivedEventArgs e)
            {
                SerialPort sp = (SerialPort)sender;
                string indata = sp.ReadExisting();
                Console.WriteLine("Data Received:");
                Console.Write(indata);
            }
        }
    }
Ping
  • 103
  • 2
  • 3
  • 17
  • When your `try...catch` block is not commented out, did you get any exception? – shree.pat18 Jun 03 '14 at 08:46
  • How do you know the event handler is not called? Because you don't get the output on the console or because you set a breakpoint? The event handler is called in a different thread than the main thread - that could be a problem with the console output. Set a breakpoint. – Thorsten Dittmar Jun 03 '14 at 08:47
  • I have edited the code and the console now show Reader connected COM17@9600. Any better way of proving good connection with the devices?? – Ping Jun 03 '14 at 13:10
  • Make this work with a terminal emulator first, something like Hyperterminal or Putty. This helps you eliminate simple mistakes like getting the port settings wrong, having a problem with the device driver and forgetting to give the RFID reader a command. – Hans Passant Jun 03 '14 at 13:39

0 Answers0