GithubHelp home page GithubHelp logo

nfc-reader's Introduction

NfcReader

A simple library that provides to use rfid card readers.

Usage

Basic usage of the library are provided. I recommend you using events which are very helpful.

Connection

//Initializing
NFCReader NFC = new NFCReader();

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

//Disconnecting
NFC.Disconnect(); // public void Disconnect()
//Inserted Event 
NFC.CardInserted += new NFCReader.CardEventHandler(...Some function);

//Ejected Event
NFC.CardEjected += new NFCReader.CardEventHandler(... Some function);

//Enabling Event Watching
NFC.Watch(); //public void Watch()

Read, Write Authorize

//Authorizing(which is done automatically by the read and write functions)
NFC.AuthBlock("2"); // private bool AuthBlock(String block)

//Reading
NFC.ReadBlock("2"); //public byte[] ReadBlock(String Block)

//Writing   
NFC.WriteBlock("Some string data that will not exceed block limit", "2"); //public bool WriteBlock(String Text, String Block)

ReaderList, CardUID

//Card UID
NFC.GetCardUID();

//Available Readers 
NFC.GetReadersList(); //public List<string> GetReadersList()

Example Inserted and Ejected Event Usage

public void Card_Inserted()
{
  try
  {
    if (NFC.Connect())
    {
        //Do stuff like NFC.GetCardUID(); ...
    }
    else
    {
        //Give error message about connection...
    }
  }
  catch (Exception ex)
  {
    this.SetStatusText("Hata: Bir Sorun Oluştu Tekrar Deneyiniz",false);
  }
}
public void Card_Ejected()
{
   //Do stuff...
   NFC.Disconnect();
}

nfc-reader's People

Contributors

fspezi avatar yoduh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

nfc-reader's Issues

WriteBlock return false.

Tell me please. The function NFC.WriteBlock(“17 different characters”, “2”) always returns false. With a successful record, it should return true, right?

How to get the ATR?

Sir, I want to ask you about the ATR
My device is Identive CLOUD 47x0 F
I am trying to figure out your code
From my understanding,my step
1.ScardEstablishContext to create service
2.ScardListReaders get the readers
3.ScardConnect connect the card
and then i can't understand how to read the card content
I search a lot of informations but not much help
Someone said I must get ATR that my reader can know which card I use and i can do the read and write
Can you told me about your step and explain ?

AuthBlock Returns code 22

I'm using an ACR1220 and running VS 2019 in Admin mode on Windows 10.
When attempting to call the AuthBlock method I always get code 22 returned.

I can get the UUID ok.
I've tried this with multiple Mifare classic 1k cards but they all return the same code. The reader works find with the Mifare app on the same PC. I've tried it on another PC and get the same error.

Do you have any idea what error code 22 means?

Also I'm happy to help contribute to the code base.

Worker never stops.

Hi!

Thank you for your code it is very usefull.

The worker never returns from its thread. Here is a fix:

` BackgroundWorker worker = sender as BackgroundWorker;

    while (!e.Cancel)
    {
        int nErrCode = Card.SCardGetStatusChange(hContext, 1000, ref states[0], 1);

        if(worker.CancellationPending)
        {
            e.Cancel = true;
            return;
        }

        if (nErrCode == Card.SCARD_E_SERVICE_STOPPED)
        {
            DeviceDisconnected();
            e.Cancel = true;
            return;
        }

        //Check if the state changed from the last time.
        if ((this.states[0].RdrEventState & 2) == 2)`

Other fix:
`
public void Disconnect()
{
if (connActive)
{
retCode = Card.SCardDisconnect(hCard, Card.SCARD_UNPOWER_CARD);
if (retCode == 0)
{
_worker?.CancelAsync();
connActive = false;
}
}
//retCode = Card.SCardReleaseContext(hCard);
}

public void Watch()
{
this.RdrState = new Card.SCARD_READERSTATE();
readername = GetReadersList()[0];
this.RdrState.RdrName = readername;

    states = new Card.SCARD_READERSTATE[1];
    states[0] = new Card.SCARD_READERSTATE();
    states[0].RdrName = readername;
    states[0].UserData = 0;
    states[0].RdrCurrState = Card.SCARD_STATE_EMPTY;
    states[0].RdrEventState = 0;
    states[0].ATRLength = 0;
    states[0].ATRValue = null;
    _worker = new BackgroundWorker
    {
        WorkerSupportsCancellation = true
    };

    _worker.DoWork += WaitChangeStatus;
    _worker.RunWorkerCompleted += _worker_RunWorkerCompleted;
    _worker.RunWorkerAsync();
}

private void _worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    DisposeWorker();
}

public void Dispose()
{
    Disconnect();
    DisposeWorker();
}

private void DisposeWorker()
{
    System.Diagnostics.Debug.WriteLine("DisposeWorker");
    if (_worker != null)
    {
        if(_worker.IsBusy)
        {
            _worker.CancelAsync();
            return;
        }

        _worker.DoWork -= WaitChangeStatus;
        _worker.RunWorkerCompleted -= _worker_RunWorkerCompleted;
        _worker.Dispose();
        _worker = null;
    }
}

`

You need an implement:
public class NFCReader : IDisposable

Kind regards,
Karoly

Always unauthorized

Hi folks, I'm a complete newbie about NFC, so I'm at my first try. Please forgive my question if it is too dumb.
I got your useful lib from git and I try to use it. I can connect to my reader, handle the "card-in" event, but when I try to write a tag in block, I always get as return code "6" that means, as far as I understand, that I'm not allowed to write.
Could you please help understand why? Thank you.

Auth returning with error 6.

Hello!

Great bit of code, i'm hoping to make it work for me.

Looking around the issue section i managed to make my software find the nfc reader but now it wouldn't auth and therefore read from the blocks.

The auth method returns with an error code 6.

Can you help me?
What details do you need me to provide?

Thanks a lot.

More options for connect

Connect method only uses the first detected card reader but it should also be an option to use whichever reader client wants. Therefore, method should get a parameter optionally. By the way, this feature will enable us to use multiple readers in the same time :)

No Errors will Not Read or Right

Hi,
The library connects fine no issues.
But it will not read or write. When i read i get "0"

var t = NFC.ReadBlock("0");

i am using the ACR122

Thank You
Gary

How to convert UID to actual Id

My card's NFC Id is 1082.
When I scan it on https://whatwebcando.today/nfc.html on android phone it shows data = 10001082
When I use this library. NFC.GetCardUID returns 04eec4a2

I have checked chrome plugin (TagConneX) it passed 04EEC4A21C6880 to its web page.

How can I convert 04eec4a2 or 04EEC4A21C6880 to 10001082?

Problem During Insert/Eject Event

Can You any one tell me how to use Insert/Eject Event. Everything is fine to me. but i need automation through Insert/Eject Event.

StopWatch feature

Library does not have a stop listener method. It was not necessary at the first place but for other implementations, it seems like a requirement.

Error SCardListReaders

Card.SCardListReaders(this.hContext, null, null, ref pcchReaders); is returning a 6 for me and I am not sure how to move forward with throubleshooting.

I am in a .NET 4.x environment and I am not getting any compile errors. I have an ACR122 reader that is working with other software.

The code that is generating the error is below:
`
public NFCReader NFC;

void Start()
{
    NFCReader NFC = new NFCReader();

    NFC.CardInserted += new NFCReader.CardEventHandler(CardIn);

    NFC.CardEjected += new NFCReader.CardEventHandler(CardOut);

    NFC.Watch(); //public void Watch()
}

public void CardIn() {
    try
    {
        if (NFC.Connect())
        {
            Debug.Log( NFC.ReadBlock("0"));

        }
        else
        {
            //Give error message about connection...
        }
    }
    catch (Exception ex)
    {
        Debug.Log("Exception Should Not Have Ran: " + ex.Message);
    }
}

public void CardOut()
{
    NFC.Disconnect();
}`

Problem with GetCardUID()

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 : "?"

Url Ekleme

Merhaba Hüseyin, Makalen çok faydalı oldu. Bir şeyi çözemedim. İçine url nasıl kayıt ederim? Telefonlara okutucunda url açılmasını istiyorum. paylod nasıl hazırlamalıyım? İyi çalışmalar.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.