C # Threading and events for pin pad device

I am new to C # and am currently working on internal code to support PIN pads. Basically, my code

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

I don’t know how to write a stream for this, so after pressing the Enter key on the device after the PIN code, the system will automatically switch to the function GetResultPIN(). So, with my basic 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
    }

Question: Can someone give me any suggestions on using streaming / event / delegate that can avoid using Console.ReadLine()?

, Console.ReadLine() ( ...). Console.ReadLine(), RequestPIN() GetResult(), , PIN- PIN- ( USB, ), , Console.ReadLine() GetResultPIN() PIN- PIN-..... , , , - Console.ReadLine() .....

, . , , RequestPIN() PIN- PIN-, - Enter PIN-, , , GetResultPIN() , PIN- ... `

PIN-, 30 GetResultPIN() "0000"

, , .... !

: RequestPin() GetResultPIN :

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: , " PIN-", "PIN- ", " PIN-" ..

minLen maxLen: PIN-

:

: PIN-, PIN-, ISO0 FOrmat, 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.

+4
4

, , ...

, :

    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);
    }
+2

api :

  • make GetResultPIN()
  • ConsolePrintOutPIN()

GetResultPIN Task To ReadYour Pin .

: https://msdn.microsoft.com/en-us/library/dd537610(v=vs.110).aspx

- :

public string 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");
    Thread.Sleep(20*1000);  // it is in milliseconds
    return sb.ToString();
}
+1

... - ... RequestPIN(). :

  • PIN- , waitTime . onPINRequestComplete : OpStatus:0 KSN:9A00030000047A2000C8 EPB:39DED176D3EA40B9 ..............................
  • PIN- waitTime.
    onPINRequestComplete : OpStatus:2 KSN:00000000000000000000 EPB:0000000000000000 ..............................

  • PIN-, " X" -.

    onPINRequestComplete : OpStatus:1 KSN:00000000000000000000 EPB:0000000000000000 ..............................

  • PIN- waitTime, waitTime .

    onPINRequestComplete : OpStatus:2 KSN:00000000000000000000 EPB:0000000000000000 .............................. , 1 3 , , 2 4 , , waiTime . Thread.sleep(20*1000) GetResultPIN() 2 4. 1 3, ....

, Event

Car.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WaitOnTasksToComplete
{
    class Car
    {
        public event Action OnChange;

        private double speed;
        public double Speed
        {
            get { return speed; }
            set { speed = value;
                if (speed >= 60)
                {
                    if (OnChange != null)
                    {
                        OnChange();
                    }
                }
            }
        }
    }
}

Program.cs:

using System;

namespace WaitOnTasksToComplete
{
    class Program
    {
        static void Main(string[] args)
        {
            Car c = new Car();
            c.OnChange += C_OnChange;

            c.Speed = 5;
            c.Speed = 55;
            c.Speed = 65;
            c.Speed = 75;

        }

        private static void C_OnChange()
        {
            Console.WriteLine("Event fired: Car goes higher than 60 MPH.");
        }
    }
}

, , Car.speed 60, . : OpStatus = -999. OpStatus=0 or 1, GetResultPIN() PrintMessagePIN(). OpStatus=2 or others, ...

... , .... .....

0

, . threading . OpenDevice()->RequestPIN()->Thread(()=>CheckOpStatus(getResultPIN)) -> Thread.Start(). , OpStatus. OpStatus - PIN Pad, zero- success; non-zero: failure. , , bool condition_WithinWaitTime bool condition_NoKeyEvent. getResultPIN .....

Here is my source code, since PIN input is one of my functions, the rest of which have very similar programming behavior (request-> manual-> feedback), so I also included a delegate variable for represents all functions (card scrolling, PIN, bla bla signature).

    static void Main(string[] args)
    {
        OpenDevice();
        EventGetPIN();

    }
    static void EventGetPIN()
    {
        myDel getResult = new myDel(GetResultPIN);
        Thread thread1 = new Thread(() => CheckOpStatus(getResult));

        myDel requestDel = new myDel(RequestPIN); requestDel();
        thread1.Start();
    }
    static void CheckOpStatus(Delegate getResult)
    {
        int count = 0;
        int checkingPeriod = 500;
        int totalWaitTime = waitTime * 1000 + offsetTime;
        string OpStatus;
        string ksnStart = mIPAD.getKSN();
        string ksn = ksnStart;
        bool condition_WithinWaitTime = true;
        bool condition_NoKeyEvent = true;
        while (condition_WithinWaitTime & condition_NoKeyEvent)
        {
            count++;
            OpStatus = mIPAD.getStatusCode().ToString();
            ksn = mIPAD.getKSN();
            //Console.WriteLine(OpStatus);
            condition_WithinWaitTime = (count * checkingPeriod) < totalWaitTime;
            condition_NoKeyEvent = (ksn == ksnStart);
            Thread.Sleep(checkingPeriod);
        }

        getResult.DynamicInvoke();
    }
0
source

All Articles