简体   繁体   English

在Java中使用smartcardio在Raspberry Pi上使用多个NFC ACR122U设备

[英]Multiple NFC ACR122U devices on a Raspberry Pi using smartcardio in Java

I have 3 ACR122U NFC readers connected to a Raspberry Pi. 我有3个ACR122U NFC阅读器连接到Raspberry Pi。 I have a Java program that uses javax.smartcardio to interface with the readers. 我有一个使用javax.smartcardio与读者进行交互的Java程序。 My program creates threads for each reader found: 我的程序为找到的每个读者创建线程:

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();
}

Each thread class runs an AccessTerminal class which does basically the following: 每个线程类都运行一个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);

The above code runs perfectly if only 1 ACR122U is connected. 如果仅连接1个ACR122U,则上面的代码可以完美运行。 It ALSO runs perfectly when I have 3 ACR122U connected to a WINDOWS machine. 当我将3个ACR122U连接到WINDOWS机器时,它也可以完美运行。 However if I have two or more connected to a raspberry pi, The one reader does nothing at first but if I first scan the other reader then it gets halfway to "Card found" then freezes, then I can swipe the other reader and it comes up with with a NoCardPresent Exception. 但是,如果我有两个或多个连接到树莓派,则一个读取器起初不执行任何操作,但是如果我先扫描另一个读取器,则它到达“找到卡”的一半,然后冻结,然后我可以刷另一个读取器来与NoCardPresent异常。 Bellow is the errors. 波纹管是错误。

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

If I do exactly the same program with the same setup on a windows machine I get the following output: 如果我在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

In both cases I swiped the same card at both readers. 在这两种情况下,我在两个读卡器上刷了同一张卡。 Don't worry about the Card is invalid or valid, that is an irrelevant check in my code. 不必担心该卡无效或有效,这与我的代码无关。

If someone has a solution for me I would be very great full. 如果有人对我有解决方案,我将非常满意。 I have a feeling it has to do with linux not differentiating between the two usb devices. 我有一种感觉,它与linux没有区分两个USB设备有关。

Thanks for reading! 谢谢阅读!

It seems as though the Raspberry Pi OS didn't handle the USB drivers too well using separate threads. 看起来Raspberry Pi OS使用单独的线程不能很好地处理USB驱动程序。 Instead of starting a new Java thread to handle each device, I just made a single thread loop through all the devices when polling for a card. 我没有启动一个新的Java线程来处理每个设备,而是在轮询卡时在所有设备上进行了单线程循环。

I found more issues like the one above using the smartcard.io java package. 我使用smartcard.io java包发现了更多类似上面的问题。 In the end I switched to C++ libnfc library to handle my devices. 最后,我切换到C ++ libnfc库来处理我的设备。

libnfc turned out to be a lot faster and more stable. 事实证明,libnfc更快,更稳定。 Especially when dealing with multiple devices on one system. 特别是在一个系统上处理多个设备时。

Hope this helps someone ;) 希望这可以帮助某人;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM