Smart card reader development with .Net technologies

心已入冬 提交于 2019-12-04 11:35:13

问题


Does anyone know how to monitor the presence of smart card and read the UID value of the card?

Yes I tried lot of examples in web like

A Smart Card Framework for .NET

pcsc-sharp

Monitoring a Smartcard Reader

But No idea how to do it. I can detect the presence of a card and can get the UID separately, but no idea how to combine them in my application:( .

Help me


回答1:


I figure it out. thought to share with anyone interested.

I used winscard.dll functions to access card data and communicate with PC.

Note that I used Mifare 1K cards as NFC tag and reader ACR 122u.

    private string getcardUID()//only for mifare 1k cards
    {
        string cardUID = "";
        byte[] receivedUID = new byte[256];
        Card.SCARD_IO_REQUEST request = new Card.SCARD_IO_REQUEST();
        request.dwProtocol = Card.SCARD_PROTOCOL_T1;
        request.cbPciLength = System.Runtime.InteropServices.Marshal.SizeOf(typeof(Card.SCARD_IO_REQUEST));
        byte[] sendBytes = new byte[] { 0xFF, 0xCA, 0x00, 0x00, 0x00 }; //get UID command      for Mifare cards
        int outBytes = receivedUID.Length;
        int status = Card.SCardTransmit(hCard, ref request, ref sendBytes[0], sendBytes.Length, ref request, ref receivedUID[0], ref outBytes);

        if (status != Card.SCARD_S_SUCCESS)
        {
            cardUID = "Error";
        }
        else
        {
            cardUID = BitConverter.ToString(receivedUID.Take(4).ToArray()).Replace("-", string.Empty).ToLower();
        }

        return cardUID;
    }

For anyone interested I've written step by step guide to achieve this in here.

Simple NFC reading system for windows

Enjoy!!!



来源:https://stackoverflow.com/questions/19863110/smart-card-reader-development-with-net-technologies

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!