h4kbas / nfc-reader

A simple library that provides to use rfid card readers.
MIT License
128 stars 44 forks source link

Problem with GetCardUID() #1

Closed arthurChennetier closed 5 years ago

arthurChennetier commented 5 years ago

Hey, first of all thanks you for your package, it will be really usefull for me.

I manage to make it work but I have only one problem wih getCardUID(). Actually it returns nothing when I use it. Perhaps I'm not using it like I should do ? Here is my code.

``

   public void auth()
    {
        NFC = new NFCReader();

        //Connecting
        NFC.Connect(); // public bool Connect()

        Console.WriteLine(NFC.GetReadersList().First());

        //Inserted Event 
        NFC.CardInserted += new NFCReader.CardEventHandler(this.onCardInserted);

        //Ejected Event
        NFC.CardEjected += new NFCReader.CardEventHandler(this.onCardEjected);

        NFC.Watch();

        while (true) ;

    }

    public void onCardInserted()
    {
        Console.WriteLine("Inserted");
        Console.WriteLine(NFC.GetCardUID()); // Here nothin is returned
    }
    public void onCardEjected()
    {
        Console.WriteLine("Ejected");
    }

I tried to understand the nature of the error by using :

Console.WriteLine(Card.GetScardErrMsg(status));

This returned me the string : "?"

h4kbas commented 5 years ago

Hi, you are welcome. I was planning to update the library but I didn't have much time.

I think it's because I didn't throw any errors for unsuccessful reading in that function. Instead, I put null string value. Did you try another card? You get ? because it's undefined error.

Please try another card and reply your findings. If there is a problem, I am gonna check it

arthurChennetier commented 5 years ago

Hi,

So I finally managed to make it work. This was due to a bad impementation from my side. Making a Connection before getting the UID fixed the problem .

 public void onCardInserted()
        {
            Console.WriteLine("Inserted");
            NFC.Connect();
            Console.WriteLine(NFC.GetCardUID());
        }

Sorry for the inconvenience.

h4kbas commented 5 years ago

I didn't provide and extensive guide and that's because these mistakes can occur. I will try to make it soon with some fixes and features. For now, I should give you an event usage example

public void Card_Inserted()
{
  try
  {
    if (NFC.Connect())
    {
        //Do stuff...
    }
    else
    {
        //Give error message about connection...
    }
  }
  catch (Exception ex)
  {
     //Give error message
  }
}
public void Card_Ejected()
{
   //Do stuff...
   NFC.Disconnect();
}