我是 C # 的新手,目前正在处理后端代码以支持 PIN pad。基本上,我的代码
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
}
问题:任何人都可以给我任何有关如何使用可以避免使用Console.ReadLine()
的线程 / 事件 / 委托的建议吗?
如上所述,Console.ReadLine()
只是用来停止程序(对不起,我这样使用它的天真....)一旦我使用Console.ReadLine()
,在RequestPIN()
和GetResult()
之间,系统至少会等待我从 PIN PAD 输入 PIN(通过 USB 连接到计算机,不会从键盘上的 0)
因此,理想情况下,所有方法将一起流动。打开设备后,RequestPIN()
应显示在 PIN Pad 屏幕上,要求输入 PIN 号,有人可以键入并按下 PIN Pad 上的回车键,然后自然流向GetResultPIN()
并读取结果,然后在控制台上打印 PIN...'
或
如果用户没有输入 PIN,设备将等待 30s,然后直接转到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()返回一个随机代码字符串,例如:当 PIN 成功时:9A00030000047A2000AB,AD781711481B08A2,0
。如果跳过 pin-input 部分,它将返回,,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);
}
你可以重写你的 API:
使GetResultPIN()
返回一个值
使用此值作为ConsolePrintOutPIN()
的输入
在GetResultPIN
中,你需要做一个任务来读取你的引脚并等待它。
你可以这样做:
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();
}
感谢您的发布...解决方案仍然不理想....我还做了一些关于函数RequestPIN()
的测试,我有以下四种情况:
用户在waitTime
消失之前完成 PIN 的键入。 onPINRequestComplete : OpStatus:0 KSN:9A00030000047A2000C8 EPB:39DED176D3EA40B9 ..............................
waitTime
输出时,用户无法完成 PIN 的键入。 onPINRequestComplete : OpStatus:2 KSN:00000000000000000000 EPB:0000000000000000 ..............................
用户通过按 PIN 键盘上的“取消 X”键取消 PIN 键盘选项。
onPINRequestComplete:
OpStatus:1
KSN:00000000000000000000
EPB:0000000000000000
..............................
用户在 waitTime 期间根本不输入 PIN,然后 waitTime 熄灭。
onPINRequestComplete : OpStatus:2 KSN:00000000000000000000 EPB:0000000000000000 ..............................
因此,场景 1 和 3 需要线程立即唤醒,而 2 和 4 需要线程在 waiTime 结束时唤醒。因此,在GetResultPIN()
中使用Thread.sleep(20*1000)
将非常适合场景 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
,继续等待...
这只是我的想法....仍然不知道如何实现它....任何相关的想法或建议将不胜感激.....
啊,我想通了。我基本上在这里使用线程。主要流程是OpenDevice()->RequestPIN()->Thread(()=>CheckOpStatus(getResultPIN)) -> Thread.Start()
。在 Thread 中,设置了一个循环,每半秒检查一次OpStatus
是什么。根据我以前的帖子,OpStatus
是 PIN Pad 的输出参数,zero- success; non-zero: failure
因此,循环将在bool condition_WithinWaitTime
之后中断。
这是我的源代码,因为 PIN 输入是我的功能之一,其余的在编程方面具有非常相似的行为(请求-手动操作-反馈),所以我还包括一个代表所有功能的委托变量(刷卡,PIN,签名 bla bla)。
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();
}
本站系公益性非盈利分享网址,本文来自用户投稿,不代表边看边学立场,如若转载,请注明出处
评论列表(66条)