我是 C# 的新手,目前正在编写支持密码键盘的后端代码。基本上,我的代码
OpenDevice() -> RequestPIN()
-> key in PIN on PIN PAD -> GetResultPIN()
-> ConsolePrintOutPIN() -> Print keyed PIN on the Console
我不知道如何为此编写线程,因此一旦在 PIN 后在设备上按下“Enter 键”,系统将自动滚动到函数 GetResultPIN()
。因此,凭借我的基础知识,我使用 Console.ReadLine()
编写了以下代码来分隔每个过程:
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
}
问题:谁能给我任何关于如何使用线程/事件/委托(delegate)的建议,以避免使用 Console.ReadLine()
?
如上文所述,Console.ReadLine()
仅用于停止程序(很抱歉我天真地以这种方式使用它......)一旦我使用 Console.ReadLine ()
,在 RequestPIN()
和 GetResult()
之间,系统至少会等待我从 PIN PAD(连接到通过 USB 连接计算机,不是 从键盘),然后我会按键盘上的任意键以传递 Console.ReadLine()
和 GetResultPIN()
将能够从 PIN Pad 获取我的 PIN 号码......整个程序现在可以运行,它还没有为客户准备好,因为它非常不稳定,由于 Console.ReadLine( )
我添加了.....
理想情况下,所有方法都将一起流动。打开设备后,RequestPIN()
应该会在 PIN Pad 屏幕上显示要求输入 PIN 码,有人可以在 PIN Pad 上键入并按回车键,它自然会流向 GetResultPIN()
并读取结果,然后在控制台上打印 PIN...`
或
如果此人不输入 PIN,设备将等待 30 秒并直接转到 GetResultPIN()
并在控制台上打印出“0000”
我已经查看了 treading 和 delegate,但我不确定如何在这种情况下使用它们......谢谢!
引用:下面列出了 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 的最小长度和最大长度
tone:提示音选项
选项:验证 PIN,不验证 PIN,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 成功时。如果跳过 pin 输入部分,它将返回 ,,0
。
最佳答案
如果没有硬件可以玩,真的很难知道这是否可行......
这是我设想的工作方式:
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);
}
关于pin pad 设备的 C# 线程和事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31662622/