如何在IC卡Sony RC-S380中写入和读取数据

时间:2017-12-07 05:22:01

标签: c# asp.net smartcard-reader pcsc

我使用PCSC-sharp库从felica卡(NFC)读取。 通过使用pcsc-sharp示例代码,现在只能显示卡ID。我想在卡上写下新的详细信息(比如员工代码或名称),并使用ht阅读相同内容。有没有可能的方法来执行此操作。 https://github.com/danm-de/pcsc-sharp/

private static string getData()
        {
            var context = _contextFactory.Establish(SCardScope.System);

            using (var rfidReader = new SCardReader(context))
            {
                var sc = rfidReader.Connect(readerName, SCardShareMode.Shared, SCardProtocol.Any);
                if (sc != SCardError.Success)
                {
                    Console.WriteLine("Card removed", readerName);
                    writeFile("Card removed");
                    return "";
                }
                var apdu = new CommandApdu(IsoCase.Case2Short, rfidReader.ActiveProtocol)
                {
                    CLA = 0xFF,
                    Instruction = InstructionCode.GetData,
                    P1 = 0x00,
                    P2 = 0x00,
                    Le = 0 // We don't know the ID tag size
                };

                sc = rfidReader.BeginTransaction();
                if (sc != SCardError.Success)
                {
                    Console.WriteLine("Could not begin transaction.");
                    Console.ReadKey();
                    return "";
                }

                Console.WriteLine("Retrieving the UID .... ");

                var receivePci = new SCardPCI(); // IO returned protocol control information.
                var sendPci = SCardPCI.GetPci(rfidReader.ActiveProtocol);

                var receiveBuffer = new byte[256];
                var command = apdu.ToArray();

                sc = rfidReader.Transmit(
                    sendPci, // Protocol Control Information (T0, T1 or Raw)
                    command, // command APDU
                    receivePci, // returning Protocol Control Information
                    ref receiveBuffer); // data buffer



                if (sc != SCardError.Success)
                {
                    Console.WriteLine("Error: " + SCardHelper.StringifyError(sc));
                }

                var responseApdu = new ResponseApdu(receiveBuffer, IsoCase.Case2Short, rfidReader.ActiveProtocol);
                Console.WriteLine("Uid: {0}", responseApdu.HasData ? BitConverter.ToString(responseApdu.GetData()) : "No uid received");
                Console.WriteLine("Uid: {0}", responseApdu.HasData ? responseApdu.GetData().ToString() : "No uid received");
                writeFile(BitConverter.ToString(responseApdu.GetData()));
                rfidReader.EndTransaction(SCardReaderDisposition.Leave);
                rfidReader.Disconnect(SCardReaderDisposition.Reset);
                return BitConverter.ToString(responseApdu.GetData());
            }
        }

0 个答案:

没有答案