简体   繁体   English

如何加速Java中PC / SC阅读器上的卡缺失检测?

[英]How to speed up card absent detection on PC/SC reader in Java?

an application I am working on requires to detect, as quick as possible (in 500 ms maximum), that a card is removed from the field. 我正在处理的应用程序要求尽快(最长500毫秒)检测到卡从现场移除。

Here is what I did to test the time needed to detect that absence of the card: 以下是我测试检测卡缺失所需的时间:

public static long timeToGetCardAbsent(int version) throws CardException{
    TerminalFactory factory = TerminalFactory.getDefault();

    CardTerminals terminalList = factory.terminals();
    CardTerminal ct = terminalList.list().get(0);

    long connectTime = 0, disconnectTime = 0;

    ct.waitForCardPresent(0);
    connectTime = new Date().getTime();

    ct.waitForCardAbsent(0);
    disconnectTime = new Date().getTime();

    return disconnectTime - connectTime;
}

When the program is running, I tap a (DESFire) contactless card on the reader and remove it immediatly. 当程序运行时,我在阅读器上点击(DESFire)非接触式卡并立即将其移除。

Here are the outputs: 以下是输出:

1437
1437
1438
1437
1422

I means that the reader (or the program?) needs almost 1.5 seconds to detect that the card is absent, which too long time for me. 我的意思是读者(或程序?)需要差不多1.5秒才能检测到卡片不存在,这对我来说太长了。

Is there a way to speed up this detection? 有没有办法加快这种检测? I am currently using javax.smartcardio , would I get better results with another library? 我目前正在使用javax.smartcardio ,我可以用另一个库获得更好的结果吗? I actually don't know what else I could use, do you have leads? 我其实不知道还能用什么,你有领导吗?

Thanks, GChabot 谢谢,GChabot

I actually managed to improve the detection time a lot today. 实际上我今天设法改善了检测时间。 I was looking at that web page from "NFC Wizard" and noticed that they were very fast at detecting my card removal. 我正在查看“NFC向导”的网页 ,发现他们检测到我的卡片移除速度非常快。 This javascript web page communicates with the reader through a Java applet, so it really uses nothing more than I have. 这个javascript网页通过Java applet与读者进行通信,所以它真的只使用了我。

SpringCard, the creators of NFC Wizard, actually provides complete documentation and even source code for a similar application at that page, under Other resources > Java and "SpringCard PC/SC SDK (ZIP archive)" . 作为NFC Wizard的创建者,SpringCard实际上在其他资源> Java和“SpringCard PC / SC SDK(ZIP存档)”下为该页面上的类似应用程序提供了完整的文档甚至源代码。

While browsing their code, I noticed that after performing operations on the card, the use the card.disconnect(false); 在浏览他们的代码时,我注意到在对卡执行操作后,使用card.disconnect(false); function (I had tried to use card.disconnect(true); but had the same results as before... bad luck ). 功能(我曾尝试使用card.disconnect(true);但结果与以前相同... 运气不好 )。

So here is now what I do : 所以现在我做的是:

import java.util.Date;
import javax.smartcardio.*;

public class NewMethod {
    private long connectTime = -1;
    private long disconnectTime = -1;

    private TerminalFactory factory;
    private CardTerminals terminalList;
    private CardTerminal ct;

    public NewMethod() throws CardException{
        factory = TerminalFactory.getDefault();

        terminalList = factory.terminals();
        ct = terminalList.list().get(0);
    }

    public long waitForCardPresent(){
        try {
            ct.waitForCardPresent(0);
        } catch (CardException e) { }
        return new Date().getTime();
    }

    public long waitForCardAbsent(){
        while(true){
            try {
                Thread.sleep(10);
            } catch (InterruptedException e1) { }

            try{
                ct.connect("*").disconnect(false);
            }
            catch(Exception e) {
                return new Date().getTime();
            }
        }
    }

    public void run(){
        while(true){
            connectTime = waitForCardPresent();
            disconnectTime = waitForCardAbsent();
            System.out.println((disconnectTime-connectTime));
        }
    }

    public static void main(String[] args){
        NewMethod nm;
        try {
            nm = new NewMethod();
            nm.run();
        } catch (CardException e) {
            e.printStackTrace();
        }
    }
}

(The thread part is optional but I got the same results with or without it so I preferred to save a bit of processor consumption) (线程部分是可选的,但无论是否有相同的结果,所以我更喜欢节省一点处理器消耗)

Here are the times I got with the threaded version: 531, 437, 656, 657, 735, 657, 547, 844, 15, 766, 859, 563, 765, 562, 422, 437, 563, 562, 562, 672, 672, 16, 547, 546, 672, 15, 344 and here with the unthreaded version: 984, 547, 796, 656, 796, 718, 656, 812, 625, 781, 813, 547, 797, 532, 407, 609, 719, 328, 469, 328, 0, 546, 625, 0, 843, 703 以下是我使用线程版本的时间: 531, 437, 656, 657, 735, 657, 547, 844, 15, 766, 859, 563, 765, 562, 422, 437, 563, 562, 562, 672, 672, 16, 547, 546, 672, 15, 344和这里的无螺纹版本: 984, 547, 796, 656, 796, 718, 656, 812, 625, 781, 813, 547, 797, 532, 407, 609, 719, 328, 469, 328, 0, 546, 625, 0, 843, 703

We may notice that the results are quite unstable (and quite better with the threaded version actually), this might come from the way I tap the card against the reader but I believe this is not the only reason. 我们可能会注意到结果非常不稳定(实际上对于线程版本来说效果更好),这可能来自我将卡片对着阅读器的方式,但我相信这不是唯一的原因。

It looks good enough for me now. 现在看起来对我来说已经够好了。 I just hope I won't get a too great variability when I will actually use it on my application. 我希望当我在我的应用程序中实际使用它时,我不会得到太大的变化。

I've tested a bit on Linux, and in Linux, the default PCSC deamon is polling the driver each 400-500ms. 我在Linux上测试了一下,在Linux中,默认的PCSC守护程序每4到500毫秒轮询一次驱动程序。 Unstable drivers seem to remove this polling and use a different method, if the PCSC Lite information that I found is correct. 如果我找到的PCSC Lite信息正确,则不稳定的驱动程序似乎会删除此轮询并使用其他方法。

The Java method seems to return almost instantly, so that's not likely to be the culprit. Java方法似乎几乎立即返回,因此不太可能是罪魁祸首。 Try and update your PCSC implementation, firmware and drivers to the latest version. 尝试将PCSC实施,固件和驱动程序更新到最新版本。 If that does not work you've got no choice to work around it. 如果这不起作用,你就没有选择解决它。

That answer did cost me an installation of PCSC on Linux + SCM SDI010 driver installation and the test of 3 firmware's. 这个答案确实让我在Linux + SCM SDI010驱动程序安装和3个固件的测试中安装了PCSC。 But I wanted the info myself anyway. 但无论如何我想要自己的信息。 I'll update the answer if I get a newer version of PCSC lite to work. 如果我使用更新版本的PCSC lite,我会更新答案。

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

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