C# 实时监控双门双向门禁控制板源码

本示例使用设备:实时网络双门双向门禁控制板可二次编程控制网络继电器远程开关-淘宝网 (taobao.com)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.Windows.Forms;
using System.Collections;
using System.Collections.Specialized;
using System.Runtime.InteropServices;
using System.Management;namespace UdpPostReaderDemoCSharp
{public partial class Form1 : Form{Form3 f3;Form5 f5;Form6 f6;int PortNumber;public string localIp;string machinnos;Boolean  ready = false ;  //线程运行标志Socket ListenerSock;      //SocketThread ListenerThre;      //端口侦听线程public IPEndPoint RemoteIPoint;  //这里指向发送数据包的目的Ip及端口,EndPoint是IPEndPoint的基类,IPEndPoint可以强转为EndPoint。EndPoint ep = (EndPoint)iep,主要目的是IPEndPoint可以自己写入IP地址和端口string BeepCode;string VoiceCode;public string sendcode;   //批量下传注册号的指针、缓冲、重发标志public bool SendCount;public int SendAgain;public int SendPoin;public int SendPoinMax;public string[] SendBufStr=new string[7950];delegate void Update1(string text1,string text2);   //线程内更新UI委拖[DllImport("Iphlpapi.dll")]private static extern int SendARP(Int32 dest, Int32 host, ref   Int64 mac, ref   Int32 length);[DllImport("Ws2_32.dll")]private static extern Int32 inet_addr(string ip);public Form1(){InitializeComponent();}private void Form1_Load(object sender, EventArgs e){ready = false;GetIp();            StartListener();}private void GetIp(){ArrayList AllIp = new ArrayList();string name = Dns.GetHostName();IPAddress[] ipadrlist = Dns.GetHostAddresses(name);foreach (IPAddress ipa in ipadrlist){if (ipa.AddressFamily == AddressFamily.InterNetwork)localIp = ipa.ToString();if (checkip(localIp)){AllIp.Add(localIp);ComboBox1.Items.Add(localIp);}}if (ComboBox1.Items.Count > 0){ComboBox1.SelectedIndex = 0;localIp = ComboBox1.Text; }else {MessageBox.Show("未能查找到本台电脑安装的网卡,暂不能启动本软件。", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);System.Environment.Exit(0);              //这是最彻底的退出方式,不管什么线程都被强制退出,把程序结束的很干净。}}private void StartListener(){IPEndPoint LocalPoint;PortNumber = Convert.ToInt32(TextBox6.Text) ;while (!ready){try{LocalPoint = new IPEndPoint(IPAddress.Parse(localIp), PortNumber);ListenerSock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);ListenerSock.Bind(LocalPoint);ListenerThre = new Thread(new ThreadStart(ThrListener));ListenerThre.Start();ready = true;}catch {ListenerSock.Close();ready = false;MessageBox.Show("有其它应用程序占用了 " + PortNumber.ToString() + " ,请检查并关闭此应用后再打开本程序", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);System.Environment.Exit(0);                      //这是最彻底的退出方式,不管什么线程都被强制退出,把程序结束的很干净。}}}public  void ThrListener(){while (ready){try{EndPoint RemotePoint = new IPEndPoint(System.Net.IPAddress.Any, 0);byte[] bytes = new byte[1024];int NumGet = ListenerSock.ReceiveFrom(bytes, ref RemotePoint);string Msg = Encoding.GetEncoding(936).GetString(bytes, 0, NumGet);//string Msg = Encoding.UTF8.GetString(bytes, 0, NumGet);             //读wifi参数时,要用utf8编码string dispstr = DateTime.Now.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss") + (" FromIP:" + Convert.ToString(RemotePoint) + "          ").Substring(0, 30) + "Data:";RemoteIPoint =(IPEndPoint)RemotePoint;                      //获取数据包来源IP及端口,原路回应this.BeginInvoke(new Update1(EditUi), dispstr, Msg);        //显示接收到的数据包,并根据情况回应设备            }catch(Exception ex ){this.BeginInvoke(new Update1(EditUi), DateTime.Now.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss")+" ",ex.Message  );}}}private void EditUi(string text1, string Msg)     //解析显示接收到的数据包,并对设备进行回应{if (ListBox1.Items.Count > 50) { ListBox1.Items.Clear();}ListBox1.Items.Add(text1 + Msg);ListBox1.SelectedIndex = ListBox1.Items.Count - 1;string DevRecFramesStr;string DevBufferIpAddrStr;string DevBufferMachinStr;string DevBufferCardidStr;string DevBufferDatetimeStr;string DevBufferUseMoneryStr;string DevBufferUseTimeStr;string DevBufferMaskStr;string DevBufferGatewayStr;string DevBufferRemoteAddrStr;string DevBufferPort;string DevBufferNumberDecStr;string DevBufferGatewayStrMAC;string DevBufferRemoteAddrMACStr;string DevBufferMACSearchStr;string DevBufferSerialNumStr;string DevBufferStaryStr;string DevBufferRecordAddStr;string DevBufferAllNoStr;string DevBufferInputKeyStr;string DevBufferCommandStr;string DevBufferFunctionStr1;string DevBufferFunctionStr2;string DevBufferQrcodeInfStr;string DevBufferReaderStr;string DevBufferDoorNoStr;string DevBufferDelayStr;string DevBufferPasswPropStr;string DevBufferPassWStr;string DevBufferStateStr;string DevBufferRandomCode;string DevBufferScanStr;string MACSTC;string MACCH;string iswifi;string wifidhcp;string Sendinf;int poi;ListViewItem plist;string[] dataArray = Msg.Split(new char[2] { ',', ',' });switch (dataArray[0]){case "101":             //接收到 1、终端开机时向电脑发送的开机信息,         2、电脑发送002查询设备时间所返回的信息DevRecFramesStr = dataArray[1];            //包序列号DevBufferIpAddrStr = dataArray[2];         //终端IPDevBufferRemoteAddrStr = dataArray[3];     //远程电脑指机IPDevBufferMachinStr = dataArray[4];         //机号DevBufferDatetimeStr = dataArray[5];       //日期时间*/if (dataArray.Length  > 6){               //2018年以后的设备有唯一硬件序号*/DevBufferSerialNumStr = dataArray[6];}Response("001," + DevRecFramesStr);       //向设备回应001+包序列号,表示已收到信息,否则设备会连续发三次string SystemTimeStr = DateTime.Now.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss");if( DevBufferDatetimeStr.Substring(0, 16) != SystemTimeStr.Substring(0, 16) )  //判断设备时间是否正确,不正确就更正时间{Response("003," + DevBufferMachinStr+","+SystemTimeStr);//更新设备时钟}TextBox3.Text = DevBufferDatetimeStr;break;case "150":                                    //接收到实时考勤门禁机刷卡数据DevRecFramesStr = dataArray[1];            //包序列号DevBufferIpAddrStr = dataArray[2];         //终端IPDevBufferRemoteAddrStr = dataArray[3];     //远程电脑指机IPDevBufferMachinStr = dataArray[4];         //机号DevBufferCardidStr = dataArray[5];         //十位物理卡号DevBufferReaderStr= dataArray[6];          //读头号DevBufferDoorNoStr = dataArray[7];         //门或继电器号*/DevBufferDelayStr = dataArray[8];          //门磁未关秒数,0表示已关*/DevBufferPasswPropStr = dataArray[9];      //密码属性(0密码无效,1刷卡后按密码,2补签密码,3开门密码,4防迫胁密码)*/DevBufferPassWStr = dataArray[10];         //密码(最多8位数字)*/DevBufferUseTimeStr = dataArray[11];       //刷卡时间                        if(dataArray.Length > 12){DevBufferSerialNumStr = dataArray[12];  //2018年以后的设备有唯一硬件序号}Response("001," + DevRecFramesStr);       //向设备回应001+包序列号,表示已收到信息,否则设备会连续发三次//此处加入业务对数据库的查、增、删、减操作//用050指令返回开门成功,051指令返回开门失败,正式系统开发时要有重发机制switch(  VoiceCode){case "0":case "1":case "2":case "3":case "4":case "5":case "6":case "7":case "8":case "9":case "10":case "11":case "12":case "13":case "14":case "15":  //提示警告不开门Sendinf = "051," + DevBufferMachinStr + "," + DevBufferCardidStr + "," + TextBox5.Text.Trim() + "," + NumericUpDown2.Value.ToString() + "," + BeepCode + "," + VoiceCode;   break;default :   //显示并开门Sendinf = "050," + DevBufferMachinStr + "," + DevBufferCardidStr + "," + TextBox5.Text.Trim() + "," + NumericUpDown2.Value.ToString() + "," + BeepCode + "," + VoiceCode + "," + DevBufferReaderStr + "," + DevBufferDoorNoStr + "," + NumericUpDown1.Value.ToString() + "," + NumericUpDown2.Value.ToString(); break;}Response(Sendinf);break;case "151":                                    //接收到实时考勤门禁机按钮开门申请信息DevRecFramesStr = dataArray[1];            //包序列号DevBufferIpAddrStr = dataArray[2];         //终端IPDevBufferRemoteAddrStr = dataArray[3];     //远程电脑指机IPDevBufferMachinStr = dataArray[4];         //机号DevBufferInputKeyStr = dataArray[5];       //按钮号DevBufferReaderStr = dataArray[6];         //对应的门或继电器号DevBufferDelayStr = dataArray[7];          //门磁未关秒数(0门已关)DevBufferUseTimeStr = dataArray[8];        //按键时间                        if (dataArray.Length > 9){DevBufferSerialNumStr = dataArray[9];  //2018年以后的设备有唯一硬件序号}Response("001," + DevRecFramesStr);       //向设备回应001+包序列号,表示已收到信息,否则设备会连续发三次//此处加入业务对数据库的查、增、删、减操作//用050指令返回开门成功,051指令返回开门失败,正式系统开发时要有重发机制switch (VoiceCode){case "0":case "1":case "2":case "3":case "4":case "5":case "6":case "7":case "8":case "9":case "10":case "11":case "12":case "13":case "14":case "15":  //提示警告不开门Sendinf = "051," + DevBufferMachinStr + "," + DevBufferInputKeyStr + "," + TextBox5.Text.Trim() + "," + NumericUpDown2.Value.ToString() + "," + BeepCode + "," + VoiceCode;break;default:   //显示并开门Sendinf = "050," + DevBufferMachinStr + "," + DevBufferInputKeyStr + "," + TextBox5.Text.Trim() + "," + NumericUpDown2.Value.ToString() + "," + BeepCode + "," + VoiceCode + "," + DevBufferReaderStr + "," +  NumericUpDown1.Value.ToString() + "," + NumericUpDown2.Value.ToString();break;}Response(Sendinf);break;case "152":                                    //接收到实时考勤门禁机门磁报警信息DevRecFramesStr = dataArray[1];            //包序列号DevBufferIpAddrStr = dataArray[2];         //终端IPDevBufferRemoteAddrStr = dataArray[3];     //远程电脑指机IPDevBufferMachinStr = dataArray[4];         //机号DevBufferInputKeyStr = dataArray[5];       //门磁号DevBufferReaderStr = dataArray[6];         //对应的门或继电器号DevBufferDelayStr = dataArray[7];          //门磁未关秒数(0门已关)DevBufferUseTimeStr = dataArray[8];        //设备时间                        if (dataArray.Length > 9){DevBufferSerialNumStr = dataArray[9];  //2018年以后的设备有唯一硬件序号}Response("001," + DevRecFramesStr);       //向设备回应001+包序列号,表示已收到信息,否则设备会连续发三次break;case "153":                                    //接收到实时考勤门禁机查询设备状态的053指令反馈DevRecFramesStr = dataArray[1];            //包序列号DevBufferIpAddrStr = dataArray[2];         //终端IPDevBufferRemoteAddrStr = dataArray[3];     //远程电脑指机IPDevBufferMachinStr = dataArray[4];         //机号DevBufferInputKeyStr = dataArray[5];       //门及继电器号DevBufferReaderStr = dataArray[6];         //继电器状态DevBufferDelayStr = dataArray[7];          //门磁未关秒数(0门已关)DevBufferStateStr = dataArray[8];          //门磁报警状态                        if (dataArray.Length > 9){DevBufferSerialNumStr = dataArray[9];  //2018年以后的设备有唯一硬件序号}Response("001," + DevRecFramesStr);       //向设备回应001+包序列号,表示已收到信息,否则设备会连续发三次f6.TextBox3.Text = "继电器号:" + DevBufferInputKeyStr + ",继电器状态:" + DevBufferReaderStr + ",门磁未关秒数:" + DevBufferDelayStr + ",门磁报警状态:" + DevBufferStateStr;break;case "154":                                    //接收到实时考勤门禁机卡离开感应区DevRecFramesStr = dataArray[1];            //包序列号DevBufferIpAddrStr = dataArray[2];         //终端IPDevBufferRemoteAddrStr = dataArray[3];     //远程电脑指机IPDevBufferMachinStr = dataArray[4];         //机号DevBufferCardidStr = dataArray[5];         //十位物理卡号DevBufferSerialNumStr = dataArray[6];      //设备有唯一硬件序号break;case "155":                                    //接收到实时考勤门禁机脱网记录DevRecFramesStr = dataArray[1];            //包序列号DevBufferIpAddrStr = dataArray[2];         //终端IPDevBufferRemoteAddrStr = dataArray[3];     //远程电脑指机IPDevBufferMachinStr = dataArray[4];         //机号DevBufferCardidStr = dataArray[5];         //十位物理卡号DevBufferReaderStr = dataArray[6];         //读头号DevBufferDoorNoStr = dataArray[7];         //门或继电器号*/DevBufferPasswPropStr = dataArray[8];      //密码属性(0密码无效,1刷卡后按密码,2补签密码,3开门密码,4防迫胁密码)*/DevBufferUseTimeStr = dataArray[9];        //刷卡时间    DevBufferStaryStr = dataArray[10];         //消费方式  0扣费 1计次*/DevBufferRecordAddStr = dataArray[11];     //记录地址标识*/DevBufferAllNoStr = dataArray[12];         //总计录数*/DevBufferSerialNumStr = "";if (dataArray.Length > 12){DevBufferSerialNumStr = dataArray[13];  //2018年以后的设备有唯一硬件序号}Response("001," + DevRecFramesStr);         //向设备回应001+包序列号,表示已收到信息,否则设备会连续发三次if (!checkDateTime(DevBufferUseTimeStr)) { DevBufferUseTimeStr = "9999-99-99 99:99:99"; } //如果接收到非法的消费时间,将消费时间改为"9999-99-99 99:99:99"才能清除。//此处加入这里对脱网记录进行处理,对数据库的查、增、删、减操作if (CheckBox3.Checked)  //清除设备内当前这条已上传的记录,驱动设备继续上传剩下的记录{Sendinf = "055," + DevBufferMachinStr + "," + DevBufferCardidStr + ","  + DevBufferUseTimeStr + "," + DevBufferRecordAddStr;Response(Sendinf);}break;case "158":                                    //接收到实时考勤门禁机远程开门058指令的反馈DevRecFramesStr = dataArray[1];            //包序列号DevBufferIpAddrStr = dataArray[2];         //终端IPDevBufferRemoteAddrStr = dataArray[3];     //远程电脑指机IPDevBufferMachinStr = dataArray[4];         //机号DevBufferStaryStr = dataArray[5];          //0开门成功,1开门密码认证失败,2开门密码认证失败超过6次,需等待一段时间后再能再次接收058指令(防暴力破解),3 不允许此时间段开门DevBufferRandomCode = dataArray[6];        //动态随机码(8位十六进制)switch (DevBufferStaryStr){case "0":f6.TextBox3.Text = "IP:" + DevBufferIpAddrStr + " 开门成功";f6.TextBox2.Text = DevBufferRandomCode;break;case "1":f6.TextBox3.Text = "IP:" + DevBufferIpAddrStr + " 开门密码认证失败!";f6.TextBox2.Text = DevBufferRandomCode;break;case "2":f6.TextBox3.Text = "IP:" + DevBufferIpAddrStr + " 开门密码认证失败超过6次,需等待一段时间后再能再次接收058指令(防暴力破解)";f6.TextBox2.Text = DevBufferRandomCode;break;case "3":f6.TextBox3.Text = "IP:" + DevBufferIpAddrStr + " 不允许此时间段开门";f6.TextBox2.Text = DevBufferRandomCode;break;}break;case "159":                                    //接收到实时考勤门禁机心跳信息DevRecFramesStr = dataArray[1];            //包序列号DevBufferIpAddrStr = dataArray[2];         //终端IPDevBufferRemoteAddrStr = dataArray[3];     //远程电脑指机IPDevBufferMachinStr = dataArray[4];         //机号DevBufferQrcodeInfStr = dataArray[5];      //信息类型DevBufferFunctionStr1 = dataArray[6];      //继电器状态(bit0为第1门bit1为第2门)DevBufferFunctionStr2 = dataArray[7];      //消防输入状态DevBufferRandomCode = dataArray[8];        //&动态随机码(8位十六进制)DevBufferDatetimeStr = dataArray[9];       //日期时间DevBufferSerialNumStr = dataArray[10];     //设备硬件号try{f6.TextBox2.Text = DevBufferRandomCode;}catch { }break;case "170":                                    //接收门禁外接双USB扫描枪模块DevRecFramesStr = dataArray[1];            //包序列号DevBufferIpAddrStr = dataArray[2];         //终端IPDevBufferRemoteAddrStr = dataArray[3];     //远程电脑指机IPDevBufferMachinStr = dataArray[4];         //机号DevBufferScanStr = dataArray[5];           //扫码信息DevBufferReaderStr= dataArray[6];          //对应门的扫描枪号DevBufferDoorNoStr = dataArray[7];         //门或继电器号*/DevBufferDelayStr = dataArray[8];          //门磁未关秒数,0表示已关*/DevBufferUseTimeStr = dataArray[9];        //扫码时间                        DevBufferSerialNumStr = dataArray[10];     //设备有唯一硬件序号Response("001," + DevRecFramesStr);         //向设备回应001+包序列号,表示已收到信息,否则设备会连续发三次switch(  VoiceCode){case "0":case "1":case "2":case "3":case "4":case "5":case "6":case "7":case "8":case "9":case "10":case "11":case "12":case "13":case "14":case "15":  //提示警告不开门Sendinf = "051," + DevBufferMachinStr + "," + DevBufferReaderStr + "," + TextBox5.Text.Trim() + "," + NumericUpDown2.Value.ToString() + "," + BeepCode + "," + VoiceCode;   break;default :   //显示并开门Sendinf = "050," + DevBufferMachinStr + ",0," + TextBox5.Text.Trim() + "," + NumericUpDown2.Value.ToString() + "," + BeepCode + "," + VoiceCode + "," + DevBufferReaderStr + "," + DevBufferDoorNoStr + "," + NumericUpDown1.Value.ToString() + "," + NumericUpDown2.Value.ToString(); break;}Response(Sendinf);break;}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/186185.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

ESP32踩坑记2-组件链接的未定义

发现问题 开发ESP32的时候,当我使用多个组件的时候,编译的时候出现两个问题 Task/Task_Gui.c:13: undefined reference to lvgl_driver_init’ However, the component manager is not enabled 解决问题1 但是我去找这个文件的时候,发现…

构建自定义ChatGPT,微软推出Copilot Studio

11月16日,微软在美国西雅图举办“Microsoft Ignite 2023”全球开发者大会。本次人工智能成为重要主题,微软几乎把所有产品都集成了生成式AI功能并发布了一系列全新产品。 其中,微软重磅推出了Copilot Studio(预览版)&…

使用vs studio 2017的cl命令查看c++类的模型结构

1、定位到当前CPP文件的盘符 2、定位到cpp文件所在目录 3、输入: cl /d1 reportSingleClassLayout查看的类名 所属文件名 例如: 我的代码 //源1.cpp class Base { public:int m_A; protected:int m_B; private:int m_C; //私有成员只是被隐藏了&#x…

提升提测质量之研测共建 | 京东云技术团队

一、序 日常研测工作演绎 你是否也有同样的困惑? 跟进的需求,就在提测前一秒,被告知不能如期提测了,研测计划被打乱; 提测的功能,犹如遇到不好的购物体验,缺斤短两,与prd预期不符…

外汇天眼:世界级的交流碰撞!Wiki Finance EXPO悉尼2023圆满落幕

11月16日对于外汇天眼来说是个特殊的日子,而对于整个世界金融界来说也是一个意义非凡的日子,由WikiGloba展会品牌WiKiEXPO举办的2023 Wiki Finance Expo在悉尼富丽敦酒店顺利举办并圆满落幕。 金融科技作为金融业创新变革的重要引擎,在全球数…

iddata函数合成的数据中采样间隔时间的两种设置方式及其程序举例

iddata函数生成的数据中采样间隔时间的两种设置方式及其程序举例 在对iddata合成的结构体数据,对采样间隔时间的设置可以通过两种方式实现,本文简要讲解其设置方式。 在设置采样间隔时候,会用到属性“ts”。 (1)对于单次实验,ts…

实景三维:穿越时空的奇妙之旅

实景三维:穿越时空的奇妙之旅 在科技的不断进步中,实景三维技术成为了现代社会中不可或缺的一部分。无论是电影、游戏还是建筑设计,实景三维都能够为我们带来身临其境的观感体验。本文将详细介绍实景三维的原理、应用以及未来的发展方向。 一…

合肥数字孪生赋能工业制造,加速推进制造业数字化转型

聚焦国家战略需求和先进制造业发展方向,加快数字化发展战略部署,数字孪生、工业互联网、工业物联网已被广泛认为是工业革命的新引擎。合肥数字孪生正在推动工业制造从制造转向智造。通过数字化建模和仿真的方式,优化设计、生产、质量管理、供…

全套完整版实战型Java云HIS系统源码

一、云HIS系统框架简介 1、技术框架 (1)总体框架: SaaS应用,全浏览器访问 前后端分离,多服务协同 服务可拆分,功能易扩展 (2)技术细节: 前端:AngularN…

五分钟,Docker安装kafka 3.5,kafka-map图形化管理工具

首先确保已经安装docker,如果是windows安装docker,可参考 wsl2安装docker 1、安装zk docker run -d --restartalways -e ALLOW_ANONYMOUS_LOGINyes --log-driver json-file --log-opt max-size100m --log-opt max-file2 --name zookeeper -p 2181:218…

电脑监控软件:保护企业核心信息资产,防止数据泄露

电脑监控软件:保护企业核心信息资产,防止数据泄露 随着信息技术的飞速发展,电脑已经成为了企业中必不可少的办公工具。然而,与此同时,企业的核心信息资产也面临着越来越大的安全风险。 下载使用安企神电脑屏幕监控软…

ssm+vue的OA办公系统(有报告)。Javaee项目,ssm vue前后端分离项目。

演示视频: ssmvue的OA办公系统(有报告)。Javaee项目,ssm vue前后端分离项目。 前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站。 项目介绍&a…