方法回复未插入的NFC阅读器 [英] Method replys unplugged NFC readers
问题描述
我目前使用pcsc-sharp从NFC标签读取ID.我得到了这个方法来列出所有可用的读者.但这回答了我曾经用过的所有读者.还列出了没有插入的旧阅读器.有人知道如何解决该问题吗?
I currently use pcsc-sharp to read ids from NFC tags. I got this methode to lists all available readers. But this replys me all readers I ever used. There are also old readers listed that aren´t plugged in. Does anyone know how to fix that?
public void SelectDevice()
{
List<string> availableReaders = this.ListReaders();
this.RdrState = new Card.SCARD_READERSTATE();
readername = availableReaders[0].ToString();
this.RdrState.RdrName = readername;
}
public List<string> ListReaders()
{
int ReaderCount = 0;
List<string> AvailableReaderList = new List<string>();
retCode = Card.SCardListReaders(hContext, null, null, ref ReaderCount);
if (retCode != Card.SCARD_S_SUCCESS)
{
MessageBox.Show(Card.GetScardErrMsg(retCode));
}
byte[] ReadersList = new byte[ReaderCount];
retCode = Card.SCardListReaders(hContext, null, ReadersList, ref ReaderCount);
if (retCode != Card.SCARD_S_SUCCESS)
{
MessageBox.Show(Card.GetScardErrMsg(retCode));
}
string rName = "";
int indx = 0;
if (ReaderCount > 0)
{
while (ReadersList[indx] != 0)
{
while (ReadersList[indx] != 0)
{
rName = rName + (char)ReadersList[indx];
indx = indx + 1;
}
AvailableReaderList.Add(rName);
rName = "";
indx = indx + 1;
}
}
return AvailableReaderList;
}
推荐答案
好的,我想我已经解决了这个问题.不知道这是否是最好的方法,但是它有效.我也不知道为什么ListReaders()-Method为我提供了我曾经使用过的所有阅读器,但是我找到了另一种获取当前插入的阅读器名称的方法.
Okay I think I solved the problem. Don´t know if this is the best way, but it works. I also don´t know why the ListReaders()-Method delivers me all readers I ever used, but I found another way to get the reader name of the reader that is currently plugged in.
public void SelectDevice()
{
string[] readerNames = new string[1];
using (var context = new SCardContext())
{
context.Establish(SCardScope.System);
readerNames = context.GetReaders();
}
readername = readerNames[0];
this.RdrState.RdrName = readername;
}
如果有更好的方法,或者有人知道为什么我会使用所有读者,请发表评论.
If there´s a better way, or someone knows why I get all readers ever used, please comment.
这篇关于方法回复未插入的NFC阅读器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!