有人知道如何监控智能卡的存在并读取卡的UID值吗?
是的,我在网上尝试了很多例子,例如:
但是不知道如何做到。我可以检测到卡的存在并单独获取UID,但不知道如何将它们合并到我的应用程序中:(。
请帮帮我。
我解决了问题,想和有兴趣的人分享。
我使用了winscard.dll函数来访问卡片数据并与PC通信。
请注意,我使用了Mifare 1K卡作为NFC标签和读卡器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;
}
如果有人感兴趣,我已经在这里编写了一份逐步指南以实现此操作。
享受吧!!!