我有 3 个 ACR122U NFC 阅读器连接到 Raspberry Pi。我有一个使用 javax.smartcardio 与读者交互的 Java 程序。我的程序为找到的每个读者创建线程:
List<CardTerminal> terminals = TerminalFactory.getDefault().terminals().list();
int terminalCount = terminals.size();
System.out.println("Detected " + String.valueOf(terminalCount) + " terminal/s");
for(int i = 0; i < terminalCount; i++)
{
System.out.println("Initiating thread :" + String.valueOf(i));
new Thread(new AccessTerminal(i,terminals.get(i))).start();
}
每个线程类都运行一个 AccessTerminal 类,该类基本上执行以下操作:
System.out.println("Thread started... Waiting for card...");
cardTerminal.waitForCardPresent(0);
System.out.println("Card found");
card = cardTerminal.connect("*");
cardChannel = card.getBasicChannel();
if(authenticate())
{
int UID = getUID();
System.out.println("User identified as :" + String.valueOf(UID));
}
cardTerminal.waitForCardAbsent(0);
如果只连接了 1 个 ACR122U,上面的代码就可以完美运行。当我将 3 个 ACR122U 连接到 WINDOWS 机器时,它也可以完美运行。但是,如果我有两个或更多连接到树莓派,一个阅读器一开始什么也不做,但如果我先扫描另一个阅读器,然后它会到“找到卡”的一半然后冻结,然后我可以刷另一个阅读器它来了出现 NoCardPresent 异常。贝娄是错误。
Starting system...
Detected 2 terminal/s
Initiating thread :0
Initiating thread :1
Thread started... Waiting for card...
Thread started... Waiting for card...
Card found
Card found
Card not valid
Nov 27, 2013 1:02:01 PM livaccess.AccessTerminal run
SEVERE: null
javax.smartcardio.CardNotPresentException: No card present
at sun.security.smartcardio.TerminalImpl.connect(TerminalImpl.java:82)
at livaccess.AccessTerminal.run(AccessTerminal.java:41)
at java.lang.Thread.run(Thread.java:722)
Caused by: sun.security.smartcardio.PCSCException: SCARD_W_REMOVED_CARD
at sun.security.smartcardio.PCSC.SCardStatus(Native Method)
at sun.security.smartcardio.CardImpl.<init>(CardImpl.java:85)
at sun.security.smartcardio.TerminalImpl.connect(TerminalImpl.java:78)
... 2 more
如果我在 Windows 机器上使用相同的设置执行完全相同的程序,我会得到以下输出:
Starting system...
Detected 2 terminal/s
Initiating thread :0
Initiating thread :1
Thread started... Waiting for card...
Thread started... Waiting for card...
Card found
Card is valid
User identified as :1534
Thread started... Waiting for card...
Card found
Card is valid
User identified as :1534
在这两种情况下,我都在两个读卡器上刷了同一张卡。不用担心卡无效或有效,这是我代码中的一个无关检查。
如果有人对我有解决方案,我会非常满意。我觉得这与 linux 没有区分两个 USB 设备有关。
谢谢阅读!