Guide to Developing Software Solutions with RFID Technology

Posted on Posted in Stories, Technology Center

rfid chipRadio-frequency identification (RFID) has become an essential part of our life.

It has made our lives easier and faster as it is now used for thousands of applications such as:

  • tracking the progress of items through the assembly line during production
  • collecting tolls without stopping
  • gaining entrance to buildings
  • tracking attendance and timekeeping for workers and students
  • easy monitoring and management of inventories, and many more.

In developing systems that make use of RFID technology, we have to consider which frequency is right for your application. RFID tags and readers must have the same frequency in order for them to communicate. There are different frequencies a RFID system can use, the most common are:

  • 125-134 kHz (Low Frequency)
  • 13.56 MHz (High Frequency)
  • 433 & 860-960 MHz (Ultrahigh Frequency)

Radio waves have different behavior at various frequency. Low frequency tags are ideal for reading objects with high water content but it only has a read range of 10 cm. Its applications include animal identification, factory data collection or simple access control systems. High frequency tags work in ranges between 10 cm up to 1 m. HF RFID are often used in contactless payment systems or public transport tickets. Meanwhile, ultrahigh frequency offers a much better read range(1m-100m) and can read more tags per second. Because of this, they are more suited for reading many items at once. Its most common applications include warehouse logistics and highway tolls.

We can classify RFID tags into two different groups: active and passive. Active RFID tags are battery powered and transmits its own signal all the time. Passive RFID tags has no internal power source. They are powered by electromagnetic energy transmitted from the RFID reader.

We have a ZK-RFID 101, which is an Ultrahigh frequency RFID integrated reader, and UHF RFID tags. We’ll use these devices to create a simple desktop application that can detect a tag and show its EPC value. Electronic Product Code (EPC) is a universal identifier that gives a unique identity to a specific object. Encoded on RFID tags, they’re used to identify or track all kinds of objects such as trade items, fixed assets, documents and reusable transport items

 

Setup

The ZK-RFID 101 reader supports serial communication through RS232, RS485 and Wiegand interface. To enable communication between your laptop/PC and the reader, you have to use a RS232-USB Converter. This converter comes with a CD driver provided by the manufacturer, we have to install it first. If you don’t have a CD driver, you can download it here.

Step 1. Driver Installation

  1. Insert the CD Driver of the converter to the CD-ROM.
  2. Connect the converter to a spare USB port on your laptop/PC.
  3. Open the contents of the CD Driver and go to …\CH340. Run HL-340.exe
  4. Select CH341SER.INF and click on install.
  5. After installation, go to Device Manager and expand “Ports (COM & LPT)”. You should see this if your installation was successful:

port

Step 2. Protocol parameters implementation

Now, the reader would need the following protocol parameters: 57600 bits per second, 8 data bits, 1 start bit, 1 stop bit, and no parity bit. To implement this to your com port:

  1. go to Device Manager and expand “Ports (COM & LPT)”
  2. right-click on “USB-SERIAL CH340(COM_)”
  3. click on Properties
  4. select Port Settings tab and change the settings according to the protocol parameters that the reader need.comprop

Note: You may want to lower the buffer settings to avoid connection problems. To do this, click on Advanced. Tick the “Use FIFO Buffers” checkbox and set the receive and transmit buffer settings to lowest. Click on OK.

adv

You can now test the reader using the demonstration software provided by the device manufacturer. Download this software from their website along with the dll files needed for software development.

Step 3. Desktop application development

Software Tools required:

Microsoft Visual Studio Community 2015

To develop a simple desktop application using C#:

  1. Go to the files that you downloaded from the device manufacturer’s website.
  2. Copy the “Basic.dll” and “UHFReader18CSharp.dll” to the “…bin\Debug” folder of your C# project.
  3. Then, on your solution explorer, right-click on your project file and click on add>reference.
  4. Browse for “UHFReader18CSharp.dll” then click on OK.
  5. Finally, add this in the library imports part of your code:
using ReaderB;

You can now use the functions of the UHFReader18CSharp dynamic-link library. Here are some of the functions included in this library that you are most likely to use:

  • AutoOpenComPort() – used to detect an unoccupied communication port and attached with a reader. The function will establish a connection between this port and the reader. The protocol parameters are 57600 bps, 8 data bits, 1 start bit, 1 stop bit, no parity bit. If a connection is established, the function will open the communication port and return a valid handle. Otherwise the function will return an error code with an invalid handle(value as -1).
  • OpenComPort() – used to establish a connection between the reader and a specified communication port. The protocol parameters are 57600 bps, 8 data bits, 1 start bit, 1 stop bit, no parity bit.
  • CloseComPort() – used to disconnect the reader and release the corresponding communication port resources. In some development environment, the communication port resources must be released first before exiting. If these are not released, the operation system will become unstable.
  • CloseSpecComPort() – used to disconnect the reader and release its corresponding resources.
  • Inventory_G2 () – used to detect tags in the inductive area and get their EPC values.

For more information on the functions in this library, please refer to the user’s guide provided by the manufacturer.

I have created this simple app that detects tags and shows their EPC values to provide more insight.

 

sample

using System;
using System.Text;
using System.Windows.Forms;
using ReaderB;

namespace SampleRfidApp
{
	public partial class Form1 : Form
	{
		private int port, comPortIndex, openComIndex;
		private byte baudRate;
        		private byte comAddress = 0xff;
        		private bool comOpen;
        		private int cmdReturn = 30;
        		private bool isInventoryScan = false;
        		private int openedPort;

        public Form1()
		{
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
		{
            port = 0;
            int openResult;

            openResult = 30;
            Cursor = Cursors.WaitCursor;

			try
			{
				comAddress = Convert.ToByte("FF", 16);
				baudRate = Convert.ToByte(5);
				openResult = StaticClassReaderB.AutoOpenComPort(ref port, ref comAddress, baudRate, ref comPortIndex);//automatically detects a com port and connects it with the reader
				openComIndex = comPortIndex;

				if (openResult == 0)
				{
					comOpen = true;
					openedPort = port;

					if ((cmdReturn == 0x35 | cmdReturn == 0x30))
					{
						StaticClassReaderB.CloseSpecComPort(comPortIndex);//disconnects the reader from designated com port when communication error occurs
						comOpen = false;
					}
				}
			}
			finally
			{
				Cursor = Cursors.Default;
			}

            if ((openComIndex != -1) & (openResult != 0X35) & (openResult != 0X30))
			{
                comOpen = true;
            }

            if ((openComIndex == -1) && (openResult == 0x30))
			{
                MessageBox.Show("Serial Communication Error", "Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            timer1.Enabled = !timer1.Enabled;
        }

        private void timer1_Tick(object sender, EventArgs e)
		{
            if (isInventoryScan)
                return;
            inventory();
        }

        private string ByteArrayToHexString(byte[] data)
		{
            StringBuilder sb = new StringBuilder(data.Length * 3);

            foreach (byte b in data)
                sb.Append(Convert.ToString(b, 16).PadLeft(2, '0'));
            return sb.ToString().ToUpper();
        }

        private void inventory()
		{
            byte AdrTID = 0;
            byte LenTID = 0;
            byte TIDFlag = 0;
            byte[] EPC = new byte[5000];
            int CardNum = 0;
            int Totallen = 0;
            isInventoryScan = true;
            bool isOnTextbox;
            int EPClen, m, CardIndex;
            string sEPC, temps;

            cmdReturn = StaticClassReaderB.Inventory_G2(ref comAddress, AdrTID, LenTID, TIDFlag, EPC, ref Totallen, ref CardNum, comPortIndex);//detects tags and gets their EPC values

            if ((cmdReturn == 1) | (cmdReturn == 2) | (cmdReturn == 3) | (cmdReturn == 4) | (cmdReturn == 0xFB))
			{
                byte[] daw = new byte[Totallen];
                Array.Copy(EPC, daw, Totallen);
                temps = ByteArrayToHexString(daw);
                m = 0;

                if (CardNum == 0)
				{
                    isInventoryScan = false;
                        return;
                }

                for (CardIndex = 0; CardIndex < CardNum; CardIndex++)
				{
                    EPClen = daw[m];
                    sEPC = temps.Substring(m * 2 + 2, EPClen * 2);
                    m = m + EPClen + 1;

                    if (sEPC.Length != EPClen * 2)
					{
                        return;
                    }
                    isOnTextbox = false;

                    for (int i = 0; i < 1; i++)
					{
                        if (sEPC == textBox1.Text)
						{
							isOnTextbox = true;
                        }
                    }

                    if (!isOnTextbox)
					{
                        textBox1.Text = sEPC;//displays the tag's EPC value in a textbox
                    }
                }
			}
			
            isInventoryScan = false;
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
		{
            try
			{
				Cursor = Cursors.WaitCursor;
				cmdReturn =StaticClassReaderB.CloseSpecComPort(openedPort);//disconnects the reader from designated com port
                if (cmdReturn == 0)
				{
                    comOpen = false;
                }
				else
				{
                    MessageBox.Show("Serial Communication Error", "Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
            }
			finally
			{
				Cursor = Cursors.Default;
			}
        }
    }
}

 

 

2 thoughts on “Guide to Developing Software Solutions with RFID Technology

  1. I have this error :

    An unhandled exception of type ‘System.BadImageFormatException’ occurred in RFIDReaderUSB.exe

    Additional information: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)

    I dont know what’s wrong ..

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.