C#线程和密码键盘设备事件 [英] C# Threading and events for pin pad device

查看:240
本文介绍了C#线程和密码键盘设备事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#中新目前正在使用的后端代码来支持密码键盘。基本上,我的代码

I am new in C# and currently working on the backend code to support PIN pad. Basically, my code

OpenDevice() -> RequestPIN() 
-> key in PIN on PIN PAD -> GetResultPIN() 
-> ConsolePrintOutPIN() -> Print keyed PIN on the Console 



我不知道怎么写线程这一点,所以,一旦回车键被击中后,PIN设备上,系统会自动滚动运作 GetResultPIN()。所以,我的基础知识,我写了使用以下代码到Console.ReadLine()来分隔各个步骤:

I don't know how to write thread for this, so that once the "Enter key" is hit on the device after PIN, the system would automatically rolls to function GetResultPIN(). So, with my elementary knowledge, I wrote the following codes using Console.ReadLine() to separate each procedure:

    static void Main(string[] args)
    {
        // 1. Open PIN Pad device
        OpenDevice();

        Console.ReadLine();// to hold up from the previous procedure, it is *not* for input data purpose

        // 2. Request PIN from PIN Pad device.
        //    On the PIN Pad device, it reads:
        //    "Key in the PIN:     "
        RequestPIN();

        Console.ReadLine();// to hold up from the previous procedure, it is *not* for input data purpose

        // 3. get PIN from the device
        GetResultPIN();

        // 4. Print out on the Console 
        ConsolePrintOutPIN();

        Console.ReadLine();// to hold up from the previous procedure, it is *not* for input data purpose
    }

问:的谁能给我如何使用线程/事件/委托任何建议,可以尽量避免使用到Console.ReadLine()

Question: Can anyone give me any suggestions on how to use threading/event/delegate that can avoid using Console.ReadLine()?

如上赞扬,到Console.ReadLine( )则只是用来停止程序(对我使用这种方式的幼稚对不起....)当我使用到Console.ReadLine()之间, RequestPIN()调用getResult(),该系统将至少等待我的PIN密钥从PIN PAD(连接到USB通过电脑,不可以从键板),然后我就按键盘上的任意键通到Console.ReadLine() GetResultPIN()将能够从密码键盘让我的PIN号码.....整个项目工程,现在,它不是客户准备好,因为它是非常不连贯,不流动,由于到Console.ReadLine()我加.....

As commended above, Console.ReadLine() is used just to stop the procedure (sorry about my naivety of using it this way....) Once I use Console.ReadLine(), between RequestPIN() and GetResult(), the system would at least wait for me to Key in the PIN from the PIN PAD (connected to the computer through USB, not from key board), and then I would hit any key on the keyboard to pass Console.ReadLine() and GetResultPIN() would be able to get my PIN number from PIN Pad.....the whole program works now, it is not customer ready, because it is very choppy, doesn't flow due to Console.ReadLine() I added.....

所以,理想情况下,所有的方法是一起流动。一旦设备被打开, RequestPIN()应显示密码键盘屏幕上的要求PIN码,有些人能键,打在密码键盘输入,并将其自然流向 GetResultPIN()和读取结果,然后它打印在控制台上的PIN ...`

So ideally, all the method would flow together. Once the device is opened, RequestPIN() should show on the PIN Pad screen asking for PIN number, some one can key in and hit enter on PIN Pad and it naturally flow to GetResultPIN() and read the result, and then it prints the PIN on the console...`

如果此人没有在PIN密钥,设备会等待30秒,并直接进入 GetResultPIN()并在控制台打印出0000

if the person doesn't key in PIN, the device would wait for 30s and directly goes to GetResultPIN() and print out "0000" on the Console

我已经看过了践踏和委托,但我不知道如何在这种情况下使用它们....谢谢!

I have looked up treading and delegate, but am not sure how to use them in this situation.... Thank you!

参考: RequestPin()和GetResultPIN如下下来列出:

Reference: RequestPin() and GetResultPIN are listed down below:

mIPAD.requestPIN(waitTime, pinMsg, minLen, maxLen, tone, option, ",");
//This function wraps device command 0x04.  
//It directs the device to prompt the user to enter a PIN 
//by displaying one of five predetermined messages and playing
// a specified sound.  
//The messages on the device’s screen look like the figures below.  
//The event associated with this function is 
//OnPINRequestCompleteEvent. 

WAITTIME:时间设备应该等待用户开始PIN码输入

pinMsg:消息显示为用户提示,如请输入密码,重新输入密码,验证码,等等

minLen和MAXLEN:最小长度和密码的最大长度

音:蜂鸣音选项

选项:验证码,没有校验码,ISO0格式,ISO3格式

输出将是:一个整数,0:成功,非零:错误

    public void GetResultPIN()
    {
        StringBuilder sb = new StringBuilder();
        sb.Append(mIPAD.pin.KSN); 
               // Key Serial Number: 
               //a given number from the device, unique for each device
        sb.Append("," + mIPAD.pin.EPB);
               // EPB: encryption of PIN after Dubpt TripleDES,
               // essentially, EPB is PIN
        sb.Append("," + mIPAD.getStatusCode());
               //status code: Zero is good/done
               //             None-Zero is Error
        sb.Append("\r\n");
        result = sb.ToString();
    }

基本上,GetResultPIN()返回的随机码串,例如:
9A00030000047A2000AB,AD781711481B08A2,0 时,PIN是成功的。如果引脚输入部分被跳过,它会返回 ,, 0

Basically, the GetResultPIN() returns a string of random code, for example: 9A00030000047A2000AB,AD781711481B08A2,0 when PIN is successful. If the pin-input part is skipped, it would return ,,0.

推荐答案

真的很难知道这是否会工作或没有硬件搭配...

Really hard to know if this will work or not without hardware to play with...

这是我想象它的工作方式:

This is the way I envisioned it working:

    static void Main()
    {
        OpenDevice();
        RequestPIN();
        if (GetResultPIN())
        {
            // do something with the PIN:
            var pin = mIPAD.pin.EPB;

            // ...

        }
        else
        {
            Console.WriteLine("0000");
        }
    }

    public static bool GetResultPIN()
    {
        TimeSpan timeout = TimeSpan.FromSeconds(30);
        System.Diagnostics.Stopwatch SW = new System.Diagnostics.Stopwatch();
        SW.Start();
        while (mIPAD.getStatusCode() != 0 && SW.Elapsed < timeout)
        {
            System.Threading.Thread.Sleep(50); // small call to prevent CPU usage ramping to 100%
        }
        return (mIPAD.getStatusCode() == 0);
    }

这篇关于C#线程和密码键盘设备事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆