Friday 10 January 2014

Post 10: Setting up idTronic RFID-Reader

In this post I'll show you how to set up the idTronic RFID Reader for your own personal needs. Below you can see how this Reader looks like. It's connected via a USB-to-Mini-USB-Cable with my Notebook.

  

In the picture below you can see UHF-RFID Tags that I'm using for this post. But any other UHF-Tags (~868 MHz) will do the job as fine. 

IdTronic ships the RFID-Reader with an SDK and a Console Software in C#. But in this software you won't need half of the features there. Below you can see a screen shot of the console Software and the methods you can use.


Most of the times you'll only need these methods: connection to the reader, reading tag and deconnect from the Reader. That's it. But as you can see in the picture above there's a "plethora" of methods to be found. And filtering what you actually need can be quite time consuming. So I made this effort for you and filtered out what you need in order to have an application that uses the Reader for what it is supposed to do: Reading RFID-Tags.

The Software package delivers a C# console program.You get a lot of classes but you only need these (keep in mind to have the exact structure as you see in the picture below):




Below is the code for our GUI. As you can see we only have a method that get the ports, reads the Tags, writes the Tags in a Hash-set, so every Tag-ID will only appear once. And a deconnect method. Since we want the Reader to constantly read, we implemented a timer that calls the get-Method every 1000ms.

Form code
 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Timers;

namespace idTronic_GUI_Anwendung
{
    delegate void myTC_fertigCallback(List< String> l);

    public partial class Form1 : Form
    {
        CSrfeReaderInterface.device.IProtocolDeviceInterface dev = null;
        HashSet< String> hashRFID = new HashSet< string>();
        static System.Timers.Timer _timer;
        private BindingSource bindingSource1 = new BindingSource();
        BindingList< String> blist = new BindingList< string>();
        List< String> lRFID = new List< string>();
        string[] theSerialPortNames;
        Boolean connectionSuccessful = false;
        string port;
        public Form1()
        {   
            InitializeComponent();

            dataGridView1.AutoGenerateColumns = true;
            dataGridView1.DataSource = lRFID;
        }

        private void bConnect_Click(object sender, EventArgs e)
        {
            _timer = new System.Timers.Timer(1000);
            _timer.Elapsed += new ElapsedEventHandler(jetzt);
            _timer.Enabled = true;
        }

        private void jetzt(object sender, ElapsedEventArgs e)
        {           
            get();
        }

        private void get()
        {
            if (!connectionSuccessful)
            {
                while (true)
                {
                    for (int portIndex = 0; portIndex < theSerialPortNames.Count(); portIndex++)
                    {

                        // You have to set the port correctly in order to use the RFID-Reader
                        // You can look up the Port of the RFID-Reader by consulting 'Geräte Manager'/ 'Device Manager'
                        port = "12"; // in my case it was port 12. Yours can be different
                        try
                        {
                            // try to open the serial port
                            dev = new CSrfeReaderInterface.SerialDevice(theSerialPortNames[Convert.ToInt32(port)]);
                            if (!dev.Open())
                            {
                                Console.WriteLine("Could not open the COM-Port.");
                            }
                            else
                            {
                                Console.WriteLine("The COM-Port was open successfully");
                                connectionSuccessful = true;
                                return;
                            }
                        }
                        catch
                        {
                            Console.WriteLine("No Reader was found.");
                        }
                    }
                }
            }
            
            // create trace and turn off 
            CSrfeReaderInterface.Global.m_tracer = new CSrfeReaderInterface.ConsoleTrace();
            CSrfeReaderInterface.Global.m_tracer.TraceLevel = 0;

            // create new instance of the protocol handler with the selected serial device
            CSrfeReaderInterface.protocol.CSrfeProtocolHandler ph = new CSrfeReaderInterface.protocol.CSrfeProtocolHandler(dev);

            // print out menu and process what is selected
                // do a single inventory and print out the tag list with an index to select one tag
                List< byte[]> epcList;
                ph.doSingleInventory(out epcList);
                for (int i = 0; i < epcList.Count(); i++)
                {
                    hashRFID.Add(BitConverter.ToString(epcList[i]));
                }

                lRFID.Clear();
                foreach(string s in hashRFID)
                {                    
                    lRFID.Add(s);
                }

                foreach (string item in lRFID)
                {
                    Console.WriteLine(item);
                }
                Console.WriteLine("----------------------------");

                myTC_fertig(lRFID);
        }

        void myTC_fertig(List< string> l)
        {

            if (lb_rfidID.InvokeRequired == true)
            {
                myTC_fertigCallback callback = new myTC_fertigCallback(myTC_fertig);
                this.Invoke(callback, new object[] { l });
            }
            else
            {
                lb_rfidID.Items.Clear();
                foreach (string item in l)
                {
                    
                    lb_rfidID.Items.Add(item);
                }
                dataGridView1.DataSource = null;
                dataGridView1.DataSource = l.Select(x=>new {Value=x}).ToList();

                DataGridViewColumn column = dataGridView1.Columns[0];
                column.Width = 200;

                lPortIndex.Text = port;

            }
        }

        private void bDisconnect_Click(object sender, EventArgs e)
        {
            _timer.Enabled = false;
            dev.Close();
            Console.WriteLine("Connection closed successfully.");
        }

        private void bEmpty_Click(object sender, EventArgs e)
        {
            lb_rfidID.Items.Clear();
            hashRFID.Clear();
            lRFID.Clear();
        }

        private void bGetPort_Click(object sender, EventArgs e)
        {
            // get all available port names
            theSerialPortNames = System.IO.Ports.SerialPort.GetPortNames();

            foreach (string availablePorts in theSerialPortNames)
            {
                Console.WriteLine("savailablePorts: "+ availablePorts);
            }
            Console.WriteLine("--------------------------");

            List< string> l = new List< string>();
            for (int i=0; i< theSerialPortNames.Count(); i++)
            {
                l.Add(i.ToString());
            }

            myTC_fertig(l);
        }
    }
} 


Our GUI will look like this. Nothing fancy but it does exactly what we need: Reading the RFID-Tags.

Below I made a vid how to use this software: Firstly we have click on the "Get Port" in order to ....get the port. Secondly you click on the "Connect / Start" button. This will make our Software connect to the Reader and starts the timer, so it constantly reads the RFID-Tags in it's proximity. Thirdly, if you're done with ready, just deconnect by pressing the "Decconect / Stop" button. I also put an "Empty list" in the GUI, so you can delete the list. Because the IDs of the RFID-Tags won't delete themselves if you try to remove the Tags from the RFID-Reader. Consequently the IDs will remain in the list although the Tags aren't it the Reader's proximty. In order to refresh the list, just press the "Empty List".
 


A video of the GUI only can be found here: 



No comments:

Post a Comment

Tweet