c# - 生物识别设备 ping 一段时间后失败
问题描述
我有ZKTeco Biometrics 设备,它使用This tutorial (C# ZKTeco Biometric Device Getting Started)与 C# windows 应用程序连接。
它工作正常,但一段时间后,我的应用程序无法 ping 设备。如下代码所示,我尝试每 25 秒 ping 一次设备。
private void TimerCheckPingAndCloseAttendanceForm() {
timerCheckPingAndCloseAttendanceForm = new Timer();
timerCheckPingAndCloseAttendanceForm.Tick += new EventHandler(CheckPingAndCloseAttendanceForm);
timerCheckPingAndCloseAttendanceForm.Interval = 25000;//25 seconds.
timerCheckPingAndCloseAttendanceForm.Start();
}
private void CheckPingAndCloseAttendanceForm(object sender, EventArgs e) {
string ipAddress = tbxDeviceIP.Text.Trim();
if (UniversalStatic.PingTheDevice(ipAddress) == false) {
//CloseAttendaceListForm();
IsDeviceConnected = false;
string infoString = "Application started on " + applicationStartDateTime.ToString() + " and ping failed on " + DateTime.Now.ToString() + " then, app closed while device ip is "+ ipAddress;
File.AppendAllText("ConnectionLog.txt", infoString + Environment.NewLine);
Application.Exit();
//timerCheckPingAndCloseAttendanceForm.Tick -= new EventHandler(CheckPingAndCloseAttendanceForm);
}
}
当我尝试从 cmd ping 命令时,设备显示destination host is unreachable
。但是每当我重新启动设备时,ping 工作正常。不知道问题出在哪里?是网络问题还是它的编码问题?
注意:我正在定期执行 ping 操作,因为Disconnected Event is not working。我假设 ping 失败意味着设备已与应用程序断开连接。
解决方案
首先:感谢您阅读我的文章
你做错了。
每 25 秒后尝试 ping 设备是不必要的。
UniversalStatic.PingTheDevice方法的唯一工作是在您第一次连接设备时检查设备是否可能处于活动状态。
如果要查看设备的状态,只需注册SDK提供的设备OnDisConnected 事件即可。IsDeviceConnected
似乎第 57 行的代码已经为您完成了OnDisConnected事件注册。
当设备本身调用 ZkemClient.cs 类中的objCZKEM_OnDisConnected方法时,您现在需要做的就是将IsDeviceConnected设置为 false 。
示例片段: 在 ZkemClient.cs 类文件中,行号 81-84 之间
void objCZKEM_OnDisConnected()
{
IsDeviceConnected = false; // <-- Add this line
}
现在,每次您尝试调用设备时,您需要做的就是检查IsDeviceConnected的值。
推荐阅读
- python - 在 tkinter 窗口中实现 Vkeyboard 导致错误:AttributeError: 'int' object has no attribute 'insert'
- python - 尝试检查 Instagram 页面是否存在
- winforms - 如何使用 Microsoft Store 等短 url 运行我的应用程序
- python - AttributeError:'Timestamp'对象在python中没有属性'split'
- javascript - 如何在javascript中存储带有值的id?
- javascript - 向用户显示键入的值,但在表单值 React js 中进行一些计算
- amazon-web-services - SSL证书在laravel项目aws中不起作用
- python - 如何找到numpy数组中图像的差异?
- windows-subsystem-for-linux - 如何在 WSL2 ubuntu 18.04 中重新挂载 rootfs?
- python - 如何在 Windows 中禁用 python 后重新启用通知?