1

I'm using a provided code here -> RFID RC522 Raspberry PI 2 Windows IOT

I'm printing the uid in a textbox but program stops in this step:

await mfrc.InitIO();

My code:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using System.Text;
using System.Net.Http;
using Windows.ApplicationModel.Background;
using Windows.System.Threading;
using Windows.Devices.Gpio;
using Mfrc522Lib;
using Windows.Devices.Enumeration;
using Windows.Devices.Spi;



namespace rfid
{ 
   public sealed partial class MainPage : Page
{

public MainPage()
{
    this.InitializeComponent();

}





public async void InitRC522Async()
{

    var mfrc = new Mfrc522();
    await mfrc.InitIO();

    while (true)
    {
        if (mfrc.IsTagPresent())
        {
            var uid = mfrc.ReadUid();
                textbox_1.Text = uid.ToString();

            mfrc.HaltTag();
        }
    }
}

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        InitRC522Async();
    }
}
}
Michael Xu
  • 4,382
  • 1
  • 8
  • 16

3 Answers3

0

You can use Device Portal to change the default controller driver as Inbox Driver.The Direct Memory Mapped Driver is for lightning providers. BTW,you need to restart your device after changing the default controller driver setting.

enter image description here

Michael Xu
  • 4,382
  • 1
  • 8
  • 16
  • After selecting the "Inbox Driver" option item, please click Update Drvier button, and then reboot your device.If it still does not work,please tell me what exception thrown. – Michael Xu Nov 28 '17 at 09:26
  • Windows is updating, meanwhile i have a class with all the "Library Mfrc522Lib.cs (all in one)" (content in the link) and the upper code in the main page. It is allright? – Joao Pereira Nov 28 '17 at 10:02
  • Hi Michael, i don't get a exception thrown, program just stops work, buttons textboxes don't respond, but app dosent close – Joao Pereira Nov 28 '17 at 13:55
  • Hi Joao Pereira, I have not encountered this issue when running the code.In fact, after ***InitIO***, there is a while loop operation, it will cause the buttons and textboxes don't respond.Please debug your application and check the point. – Michael Xu Nov 29 '17 at 01:56
0

I've been implementing the same reference, RFID RC522 Raspberry PI 2 Windows IOT for my RFID Attendance System and I've found that printing the detected UID into the xaml textbox freezes the program.

To fix this, you could use a task delay. This allows the textbox to update upon tag detection..

        var mfrc = new Mfrc522();
        await mfrc.InitIO();
        while(true)
        {
            if (mfrc.IsTagPresent())
            {
                var uid = mfrc.ReadUid().ToString();
                string uidString = uid;
                textbox1.Text = uidString;
                mfrc.HaltTag();

                await Task.Delay(TimeSpan.FromSeconds(1));
            }
        }
Zorell
  • 1
  • 2
0

I had a similar issue with the GUI Freezing, just place it through it's own thread and it'll work fine.

public MainPage()
    {
        this.InitializeComponent();

        Thread T = new Thread(testfunc);
        T.IsBackground = true;
        T.SetApartmentState(ApartmentState.STA);
        T.Start();


    }
    
    public async void testfunc()
    {
        await Task.Run(async () =>
        {
            await RFIDTask();
            
        });
    }
    
    public async Task RFIDTask()
    {
        var mfrc = new Mfrc522();
        await mfrc.InitIO();

        while (true)
        {

            while (true)
            {
                if (mfrc.IsTagPresent())
                {
                    string txt_Result = "";
                    var uid = mfrc.ReadUid();

                    foreach (byte byt in uid.FullUid)
                    {
                        txt_Result = txt_Result + byt.ToString("x2");

                    }
                    await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
                    {
                        tbTagId.Text = txt_Result;
                        
                    }); 
                    
                    await Task.Run(() => mfrc.HaltTag());
                    break;
                }

            }
            await Task.Delay(2000);

            mfrc.Reset();
            mfrc.HaltTag();