我有3個ACR122U NFC閱讀器連接到Raspberry Pi。我有一個Java程序,它使用javax.smartcardio與讀者進行交互。我的程序找到的每個讀取器創建線程:Raspberry Pi上的多個NFC ACR122U設備使用Java中的smartcardio
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機器時,它也可以完美運行。但是,如果我有兩個或更多的連接到覆盆子pi,一個讀者一開始什麼也不做,但如果我第一次掃描其他讀者,那麼它會中途「找到卡片」然後凍結,然後我可以滑動另一個讀卡器,與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設備。
感謝您的閱讀!
我設法解決這個問題。但我有另一個問題。我無法發現我可以用java來區分多個NFC閱讀器。它們都具有相同的名稱和序列號0.關於此問題的任何想法? –
如果您發現問題的解決方案,最好的辦法就是回答自己的問題。我們準備幫助你,希望你也想這樣做。在評論中提出另一個問題並不是一個好主意(但我認爲你已經注意到了這一點,因爲你已經單獨提出了這個問題:)) –