Introduction: ACR122U NFC C# Project
The aim of this project is to provide all the means to create an application using an ACR122U NFC reader. The application will only use the NFC tag ID. With a compatible tag, it is also possible to write data.
Step 1: Getting Things Ready
For this project, Visual Studio is being used. Mind you, this is the full environment, and not the Visual Studio Code environment. Get your copy at the Microsoft site (the free community edition will do).
This is a hefty install, taking up several gigabytes on your hard disk. You don't need to install all the available options. For the purpose of this tutorial, you will only need the .Net Desktop environment.
Step 2: The Hardware
Obviously, you'll need a card reader. The ACR122U is a cheap reader, which you can pickup at different websites. Just make sure you have the right one for the kind of tags you will be using.
There are two kinds of NFC tags available. The most common ones will use 13.56 MHz frequency band.
Installing the hardware on a Windows 10 computer is very straightforward. Normally, you don't need any drivers, because Windows recognizes the hardware as a smart card reader.
Step 3: Making a New Project
In Visual Studio you create a new project. We'll dive directly in the most difficult one, namely a Windows project. We'll get to the difficulties right away, but first you'll need to bare with me for a second.
To get this project working, I took advantage of an existing package. Within Visual Studio, there is a tool called NuGet, which gives you access to packages created by other users. In the video, you can watch the creation of the project and the adding of the package
Step 4: Adding Some Code
The package we added was provided by a company called SydeSoft. They are also kind enough to publish some sample code. Unfortunately, this code is in German, which is not the most accessible language for most users on the internet. In this tutorial, you will find a translated version of this code.
However, you should be aware this code is specific for a console application. If you paste it into a console project, you can directly see the NFC reader in action. It makes a good exercise and is useful if you want to test if the reader is working.
Since our object is to create a Windows application, we will adapt the code for this purpose.
We'll take the first line of code private static ACR122U acr122u = new ACR122U( );and paste it at the right place in our code. You can see it at line 13 in the picture above. You'll notice some red waves under the words ACR122U, indicating an error. This can quickly be solved by right clicking on the word, then selecting Quick actions and refactoring and then selecting Using SydeSoft.NfcDevice;
Now your error should have disappeared.
Step 5: Adding More Code
Based on the code from SydeSoft, we make adjustments to our code.
The first 3 lines of code that originally were in the code of Main (typical for a console application) are put within the constructor of the Form1.
The rest of the code is copied below that, as shown in the picture.
Having done this, you can test drive your application. Nothing will be shown in the window yet, but you can see the result of your actions in the console of the development environment. Use the CTRL+ALT+O or view->output menu to see the console. Depending on the type of tag you are using, you may get an error while writing, if your tag is readonly.
Step 6: Communicating With the Window in Your Application
Now we get to a little nasty part. The library we are using works by the benefits of threads. Simply explained, a thread is a kind of an independent piece of program, monitored or controlled by the main program in this case.
Unfortunately (it took me some time to figure it out), the data managed by one thread can not directly be accessed by another thread. So, I use a little workaround.
We change the line of code where the object acr122u is created. In our original code, it is an object created on the class ACR122U (be aware of the casing). We will make our own class, derived from this class, in which we will add a property in which we will store the data. For purposes of demonstration, the ID will be used.
The modifications to be made, are:
- Change the definition of the acr122u variable to private static MyACR122U acr122u = new MyACR122U();
- Right click on the word MyACR122U and select Quick actions and refactorings, then Generate class 'MyACR122U'
- Scroll to the bottom of the source code and modify the code as in the picture above
- Do some extra work on the code in the Acr122u_CardInserted method
Next, we modify the design of our window.
Step 7: Putting the Information on the Window
Add a TextBox and a Timer control to the window. Again, for demonstration purposes, we don't rename them.
Make sure the Timer is enabled (change the property Enabled to True)
Doubleclick on the Timer object and add the following code:
textBox1.Text = acr122u.ReadId;
Download the full Visual Studio project from my website.