• vb.net 实时监控双门双向门禁控制板源代码


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

    1. Imports System.Net.Sockets
    2. Imports System.Net
    3. Imports System.Text
    4. Imports System.Threading
    5. Imports System.Net.NetworkInformation
    6. Imports System.Management
    7. Public Class Form1
    8. Dim PortNumber As Integer ''侦听端口号
    9. Dim ListenerThre As Thread ''侦听线程
    10. Dim LocalIp As String ''本地ip64
    11. Dim ready As Boolean = False ''线程运行标识
    12. Dim machinnos As String
    13. Dim RemoteIPoint As IPEndPoint
    14. Dim BeepCode As String
    15. Dim VoiceCode As String
    16. Public ListenerSock As Socket ''侦听socket
    17. Delegate Sub EditUi(ByVal data0 As String, ByVal data1 As String) '线程内更新UI传送两个参数
    18. Delegate Sub EditTC(ByVal con As Integer, ByVal data1 As String)
    19. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    20. 'Control.CheckForIllegalCrossThreadCalls = False '经典解决“线程间操作无效: 从不是创建控件的线程访问它 ,尽量不要用这种方式
    21. getIp()
    22. ComboBox2.SelectedIndex = 0
    23. ComboBox3.SelectedIndex = 1
    24. ComboBox4.SelectedIndex = 16
    25. ComboBox5.SelectedIndex = 1
    26. StartListener() ''开始侦听
    27. End Sub
    28. Public Sub getIp() '获取本机所有网卡的IP
    29. Dim Address() As System.Net.IPAddress
    30. Dim i As Integer
    31. Address = Dns.GetHostByName(Dns.GetHostName()).AddressList
    32. If UBound(Address) < 0 Then
    33. MsgBox("未能查找到本台电脑安装的网卡,暂不能启动本软件。", MsgBoxStyle.Critical + vbOKOnly, "注意")
    34. End
    35. Else
    36. For i = 0 To UBound(Address)
    37. ComboBox1.Items.Add(Address(i).ToString())
    38. Next
    39. ComboBox1.SelectedIndex = 0
    40. LocalIp = ComboBox1.Text.Trim()
    41. End If
    42. End Sub
    43. Private Sub StartListener()
    44. Dim LocalPoint As IPEndPoint
    45. PortNumber = TextBox6.Text
    46. While Not ready
    47. Try
    48. LocalPoint = New IPEndPoint(IPAddress.Parse(LocalIp), PortNumber)
    49. ListenerSock = New Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
    50. ListenerSock.Bind(LocalPoint)
    51. ListenerThre = New Thread(AddressOf ThrListener)
    52. ListenerThre.Start()
    53. ready = True
    54. 'ListBox1.Items.Add(LocalPoint.ToString() & "已开始侦听......")
    55. Catch ex As Exception
    56. ListenerSock.Close()
    57. ready = False
    58. MsgBox(" 有其它应用程序占用了 " & LocalPoint.ToString() & " ,请检查并关闭此应用后再打开本程序。", vbCritical + vbOKOnly, "警告")
    59. End
    60. End Try
    61. End While
    62. End Sub
    63. Private Sub ThrListener() '侦听线程
    64. While ready
    65. Try
    66. Dim bytes(1024) As Byte
    67. Dim dataArray() As String
    68. Dim RemotePoint As System.Net.EndPoint = New System.Net.IPEndPoint(System.Net.IPAddress.Any, 0)
    69. Dim NumGet As Integer
    70. Dim Msg As String
    71. Dim Sendinf As String
    72. Dim SendBuf As Byte()
    73. Dim i As Integer
    74. Dim DevBufferIpAddrStr As String
    75. Dim DevBufferRemoteAddrStr As String
    76. Dim DevBufferCardCode As String
    77. Dim DevBufferUseTimeStr As String
    78. Dim DevRecFramesStr As String
    79. Dim DevBufferMachinStr As String
    80. Dim DevBufferDatetimeStr As String
    81. Dim DevBufferCardidStr As String
    82. Dim DevBufferUseMoneryStr As String
    83. Dim DevBufferReaderStr As String
    84. Dim DevBufferDoorNoStr As String
    85. Dim DevBufferDelayStr As String
    86. Dim DevBufferPasswPropStr As String
    87. Dim DevBufferPassWStr As String
    88. Dim DevBufferStaryStr As String
    89. Dim DevBufferRecordAddStr As String
    90. Dim DevBufferAllNoStr As String
    91. Dim DevBufferFunctionStr1 As String
    92. Dim DevBufferFunctionStr2 As String
    93. Dim DevBufferSerialNumStr As String
    94. Dim DevBufferInputKeyStr As String
    95. Dim DevBufferCommandStr As String
    96. Dim DevBufferQrcodeInfStr As String
    97. Dim DevBufferStateStr As String
    98. Dim DevBufferRandomCode As String
    99. Dim DevBufferSwitchStr As String
    100. Dim DevBufferFireStr As String
    101. NumGet = ListenerSock.ReceiveFrom(bytes, RemotePoint)
    102. Msg = Encoding.GetEncoding(936).GetString(bytes, 0, NumGet)
    103. 'Msg = Encoding.UTF8.GetString(bytes, 0, NumGet) '读取中文wifi名称时,需用utf8解码
    104. Me.Invoke(New EditUi(AddressOf EditUiNow), Now() & (" FromIP:" & Convert.ToString(RemotePoint) + " ").Substring(0, 30) & "Data:", Msg) '用Invoke跨线程更新UI
    105. Me.Invoke(New EditTC(AddressOf EditTCNow), 3, Convert.ToString(RemotePoint)) '用Invoke跨线程更新UI
    106. dataArray = Split(Convert.ToString(Msg), ",")
    107. Select Case dataArray(0)
    108. Case "101" '接收到 1、终端开机时向电脑发送的开机信息, 2、电脑发送002查询设备时间所返回的信息
    109. DevRecFramesStr = dataArray(1) '包序列号
    110. DevBufferIpAddrStr = dataArray(2) '终端IP
    111. DevBufferRemoteAddrStr = dataArray(3) '远程电脑指机IP
    112. DevBufferMachinStr = dataArray(4) '机号
    113. DevBufferDatetimeStr = dataArray(5) '日期时间*/
    114. If dataArray.Length > 6 Then '2018年以后的设备有唯一硬件序号*/
    115. DevBufferSerialNumStr = dataArray(6)
    116. End If
    117. Sendinf = "001," + DevRecFramesStr '向设备发此数据表示已收到信息,否则设备会连续发三次
    118. SendBuf = Encoding.GetEncoding(936).GetBytes(Sendinf)
    119. ListenerSock.SendTo(SendBuf, SendBuf.Length, SocketFlags.None, RemotePoint)
    120. Me.Invoke(New EditTC(AddressOf EditTCNow), 2, Now() & (" SendTo:" & Convert.ToString(RemotePoint) + " ").Substring(0, 30) & "Data:" & Sendinf)
    121. Dim SystemTimeStr As String = String.Format("{0:yyyy-MM-dd HH:mm:ss}", DateTime.Now)
    122. If DevBufferDatetimeStr.Substring(0, 16) <> SystemTimeStr.Substring(0, 16) Then
    123. Sendinf = "003," & DevRecFramesStr & "," & SystemTimeStr
    124. SendBuf = Encoding.GetEncoding(936).GetBytes(Sendinf)
    125. ListenerSock.SendTo(SendBuf, SendBuf.Length, SocketFlags.None, RemotePoint)
    126. Me.Invoke(New EditTC(AddressOf EditTCNow), 2, Now() & (" SendTo:" & Convert.ToString(RemotePoint) + " ").Substring(0, 30) & "Data:" & Sendinf)
    127. End If
    128. Me.Invoke(New EditTC(AddressOf EditTCNow), 1, DevBufferDatetimeStr) '用Invoke跨线程更新UI
    129. Case "150" '接收到实时考勤门禁机刷卡数据
    130. DevRecFramesStr = dataArray(1) '包序列号
    131. DevBufferIpAddrStr = dataArray(2) '终端IP
    132. DevBufferRemoteAddrStr = dataArray(3) '远程电脑指机IP
    133. DevBufferMachinStr = dataArray(4) '机号
    134. DevBufferCardidStr = dataArray(5) '卡号
    135. DevBufferReaderStr = dataArray(6) '消费金额*/
    136. DevBufferDoorNoStr = dataArray(7) '门或继电器号*/
    137. DevBufferDelayStr = dataArray(8) '门磁未关秒数,0表示已关*/
    138. DevBufferPasswPropStr = dataArray(9) '密码属性(0密码无效,1刷卡后按密码,2补签密码,3开门密码,4防迫胁密码)*/
    139. DevBufferPassWStr = dataArray(10) '密码(最多8位数字)*/
    140. DevBufferUseTimeStr = dataArray(11) '消费时间*/
    141. If dataArray.Length > 12 Then
    142. DevBufferSerialNumStr = dataArray(12) '2018年以后的设备有唯一硬件序号
    143. End If
    144. Sendinf = "001," + DevRecFramesStr '向设备发此数据表示已收到信息,否则设备会连续发三次
    145. SendBuf = Encoding.GetEncoding(936).GetBytes(Sendinf)
    146. ListenerSock.SendTo(SendBuf, SendBuf.Length, SocketFlags.None, RemotePoint)
    147. Me.Invoke(New EditTC(AddressOf EditTCNow), 2, Now() & (" SendTo:" & Convert.ToString(RemotePoint) + " ").Substring(0, 30) & "Data:" & Sendinf)
    148. '此处加入业务对数据库的查、增、删、减操作
    149. '用050指令返回开门成功,051指令返回开门失败,正式系统开发时要有重发机制
    150. Select Case VoiceCode
    151. Case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15" '发送禁止开门的指令"
    152. Sendinf = "051," & DevBufferMachinStr & "," & DevBufferCardidStr & "," & TextBox5.Text.Trim() & "," & Format(NumericUpDown2.Value, "0") & "," & BeepCode & "," & VoiceCode
    153. Case Else '发送开门指令
    154. Sendinf = "050," & DevBufferMachinStr & "," & DevBufferCardidStr & "," & TextBox5.Text.Trim() & "," & Format(NumericUpDown2.Value, "0") & "," & BeepCode & "," & VoiceCode & "," & DevBufferReaderStr & "," & DevBufferDoorNoStr & "," & Format(NumericUpDown1.Value, "0") & "," & Format(NumericUpDown2.Value, "0")
    155. End Select
    156. SendBuf = Encoding.GetEncoding(936).GetBytes(Sendinf)
    157. ListenerSock.SendTo(SendBuf, SendBuf.Length, SocketFlags.None, RemotePoint)
    158. Me.Invoke(New EditTC(AddressOf EditTCNow), 2, Now() & (" SendTo:" & Convert.ToString(RemotePoint) + " ").Substring(0, 30) & "Data:" & Sendinf)
    159. Case "151" '发送按钮开门申请信息
    160. DevRecFramesStr = dataArray(1) '包序列号
    161. DevBufferIpAddrStr = dataArray(2) '终端IP
    162. DevBufferRemoteAddrStr = dataArray(3) '远程电脑指机IP
    163. DevBufferMachinStr = dataArray(4) '机号
    164. DevBufferInputKeyStr = dataArray(5) '&&按钮号
    165. DevBufferReaderStr = dataArray(6) '&&对应的门或继电器号
    166. DevBufferDelayStr = dataArray(7) '&&门磁未关秒数(0门已关)
    167. DevBufferUseTimeStr = dataArray(8) '&&刷卡时间
    168. If UBound(dataArray) >= 9 Then DevBufferSerialNumStr = dataArray(9) Else DevBufferSerialNumStr = "" '唯一硬件序号
    169. Sendinf = "001," + DevRecFramesStr '向设备发此数据表示已收到信息,否则设备会连续发三次
    170. SendBuf = Encoding.GetEncoding(936).GetBytes(Sendinf)
    171. ListenerSock.SendTo(SendBuf, SendBuf.Length, SocketFlags.None, RemotePoint)
    172. Me.Invoke(New EditTC(AddressOf EditTCNow), 2, Now() & (" SendTo:" & Convert.ToString(RemotePoint) + " ").Substring(0, 30) & "Data:" & Sendinf)
    173. Select Case VoiceCode
    174. Case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15" '发送禁止开门的指令"
    175. Sendinf = "051," & DevBufferMachinStr & "," & DevBufferInputKeyStr & "," & TextBox5.Text.Trim() & "," & Format(NumericUpDown2.Value, "0") & "," & BeepCode & "," & VoiceCode
    176. Case Else '发送开门指令
    177. Sendinf = "050," & DevBufferMachinStr & "," & DevBufferInputKeyStr & "," & TextBox5.Text.Trim() & "," & Format(NumericUpDown2.Value, "0") & "," & BeepCode & "," & VoiceCode & "," & DevBufferInputKeyStr & "," & DevBufferReaderStr & "," & Format(NumericUpDown1.Value, "0") & "," & Format(NumericUpDown2.Value, "0")
    178. End Select
    179. SendBuf = Encoding.GetEncoding(936).GetBytes(Sendinf)
    180. ListenerSock.SendTo(SendBuf, SendBuf.Length, SocketFlags.None, RemotePoint)
    181. Me.Invoke(New EditTC(AddressOf EditTCNow), 2, Now() & (" SendTo:" & Convert.ToString(RemotePoint) + " ").Substring(0, 30) & "Data:" & Sendinf)
    182. Case "152" '门磁报警信息
    183. DevRecFramesStr = dataArray(1) ' &&包序列号
    184. DevBufferIpAddrStr = dataArray(2) ' &&终端IP
    185. DevBufferRemoteAddrStr = dataArray(3) '&&远程电脑指机IP
    186. DevBufferMachinStr = dataArray(4) '&&机号
    187. DevBufferInputKeyStr = dataArray(5) '&&门磁号
    188. DevBufferReaderStr = dataArray(6) '&&对应的门或继电器号
    189. DevBufferDelayStr = dataArray(7) '&&门磁未关秒数(0门已关)
    190. DevBufferUseTimeStr = dataArray(8) '&&刷卡时间
    191. If UBound(dataArray) >= 9 Then DevBufferSerialNumStr = dataArray(9) Else DevBufferSerialNumStr = "" '唯一硬件序号
    192. Sendinf = "001," + DevRecFramesStr '向设备发此数据表示已收到信息,否则设备会连续发三次
    193. SendBuf = Encoding.GetEncoding(936).GetBytes(Sendinf)
    194. ListenerSock.SendTo(SendBuf, SendBuf.Length, SocketFlags.None, RemotePoint)
    195. Me.Invoke(New EditTC(AddressOf EditTCNow), 2, Now() & (" SendTo:" & Convert.ToString(RemotePoint) + " ").Substring(0, 30) & "Data:" & Sendinf)
    196. Case "153" '返回查询设备状态的053指令
    197. DevRecFramesStr = dataArray(1) ' &&包序列号
    198. DevBufferIpAddrStr = dataArray(2) ' &&终端IP
    199. DevBufferRemoteAddrStr = dataArray(3) '&&远程电脑指机IP
    200. DevBufferMachinStr = dataArray(4) '&&机号
    201. DevBufferInputKeyStr = dataArray(5) '&&继电器号
    202. DevBufferReaderStr = dataArray(6) '&&继电器状态
    203. DevBufferDelayStr = dataArray(7) '&&门磁未关秒数(0门已关)
    204. DevBufferStateStr = dataArray(8) '&&门磁报警状态
    205. If UBound(dataArray) >= 9 Then DevBufferSerialNumStr = dataArray(9) Else DevBufferSerialNumStr = "" '唯一硬件序号
    206. Sendinf = "001," + DevRecFramesStr '向设备发此数据表示已收到信息,否则设备会连续发三次
    207. SendBuf = Encoding.GetEncoding(936).GetBytes(Sendinf)
    208. ListenerSock.SendTo(SendBuf, SendBuf.Length, SocketFlags.None, RemotePoint)
    209. Me.Invoke(New EditTC(AddressOf EditTCNow), 2, Now() & (" SendTo:" & Convert.ToString(RemotePoint) + " ").Substring(0, 30) & "Data:" & Sendinf)
    210. Msg = "继电器号:" & DevBufferInputKeyStr & ",继电器状态:" & DevBufferReaderStr & ",门磁未关秒数:" & DevBufferDelayStr & ",门磁报警状态:" & DevBufferStateStr
    211. Me.Invoke(New EditTC(AddressOf EditTCNow), 9, Msg)
    212. Case "154" '接收到实时考勤门禁机卡离开感应区
    213. DevRecFramesStr = dataArray(1) '包序列号
    214. DevBufferIpAddrStr = dataArray(2) '终端IP
    215. DevBufferRemoteAddrStr = dataArray(3) '远程电脑指机IP
    216. DevBufferMachinStr = dataArray(4) '机号
    217. DevBufferCardidStr = dataArray(5) '卡号
    218. DevBufferSerialNumStr = dataArray(6)
    219. Case "155" '&&门禁脱网记录发送----
    220. DevRecFramesStr = dataArray(1) ' &&包序列号
    221. DevBufferIpAddrStr = dataArray(2) ' &&终端IP
    222. DevBufferRemoteAddrStr = dataArray(3) '&&远程电脑指机IP
    223. DevBufferMachinStr = dataArray(4) '&&机号
    224. DevBufferCardidStr = dataArray(5) '&&卡号
    225. DevBufferReaderStr = dataArray(6) '&&读头
    226. DevBufferDoorNoStr = dataArray(7) '&&门或继电器号
    227. DevBufferPasswPropStr = dataArray(8) '&&密码属性(0密码无效,1刷卡后按密码,2补签密码,3开门密码,4防迫胁密码)
    228. DevBufferUseTimeStr = dataArray(9) '&&刷卡时间
    229. DevBufferStaryStr = dataArray(10) '&&状态 0扣费 1计次
    230. DevBufferRecordAddStr = dataArray(11) '&&记录地址标识
    231. DevBufferAllNoStr = dataArray(12) '&&总计录数
    232. If UBound(dataArray) = 13 Then DevBufferSerialNumStr = dataArray(13) Else DevBufferSerialNumStr = "" '唯一硬件序号
    233. Sendinf = "001," + DevRecFramesStr '向设备发此数据表示已收到信息,否则设备会连续发三次
    234. SendBuf = Encoding.GetEncoding(936).GetBytes(Sendinf)
    235. ListenerSock.SendTo(SendBuf, SendBuf.Length, SocketFlags.None, RemotePoint)
    236. Me.Invoke(New EditTC(AddressOf EditTCNow), 2, Now() & (" SendTo:" & Convert.ToString(RemotePoint) + " ").Substring(0, 30) & "Data:" & Sendinf)
    237. If Not IsDateTime(DevBufferUseTimeStr) Then '记录的刷卡时间非法,表示此记录有问题(测试时的其它数据)
    238. DevBufferUseTimeStr = "9999-99-99 99:99:99"
    239. End If
    240. '这里对脱网记录进行处理
    241. If CheckBox3.Checked Then '选择清除已上传的记录驱使用设备继续上传剩下的记录
    242. Sendinf = "055," + DevBufferMachinStr + "," + DevBufferCardidStr + "," + DevBufferUseTimeStr + "," + DevBufferRecordAddStr '&&清除设备内当前这条记录,设备才会传下一条记录
    243. SendBuf = Encoding.GetEncoding(936).GetBytes(Sendinf)
    244. ListenerSock.SendTo(SendBuf, SendBuf.Length, SocketFlags.None, RemotePoint)
    245. Me.Invoke(New EditTC(AddressOf EditTCNow), 2, Now() & (" SendTo:" & Convert.ToString(RemotePoint) + " ").Substring(0, 30) & "Data:" & Sendinf)
    246. End If
    247. Case "158" '响应电脑远程开门的058指令
    248. DevRecFramesStr = dataArray(1) ' &&包序列号
    249. DevBufferIpAddrStr = dataArray(2) ' &&终端IP
    250. DevBufferRemoteAddrStr = dataArray(3) '&&远程电脑指机IP
    251. DevBufferMachinStr = dataArray(4) '&&机号
    252. DevBufferStaryStr = dataArray(5) '&&0开门成功,1开门密码认证失败,2开门密码认证失败超过6次,需等待一段时间后再能再次接收058指令(防暴力破解),3 不允许此时间段开门
    253. DevBufferRandomCode = dataArray(6) '&&动态随机码(8位十六进制)
    254. Me.Invoke(New EditTC(AddressOf EditTCNow), 10, DevBufferRandomCode)
    255. Msg = ""
    256. Select Case DevBufferStaryStr
    257. Case "0"
    258. Msg = "IP:" & DevBufferIpAddrStr & " 开门成功"
    259. Case "1"
    260. Msg = "IP:" & DevBufferIpAddrStr & " 开门密码认证失败!"
    261. Case "2"
    262. Msg = "IP:" & DevBufferIpAddrStr & " 开门密码认证失败超过6次,需等待一段时间后再能再次接收058指令(防暴力破解)"
    263. Case "3"
    264. Msg = "IP:" & DevBufferIpAddrStr & " 不允许此时间段开门"
    265. End Select
    266. Me.Invoke(New EditTC(AddressOf EditTCNow), 9, Msg)
    267. Me.Invoke(New EditTC(AddressOf EditTCNow), 10, DevBufferRandomCode)
    268. Case "159" '&&心跳信息
    269. DevRecFramesStr = dataArray(1) ' &&包序列号
    270. DevBufferIpAddrStr = dataArray(2) ' &&终端IP
    271. DevBufferRemoteAddrStr = dataArray(3) '&&远程电脑指机IP
    272. DevBufferMachinStr = dataArray(4) '&&机号
    273. DevBufferQrcodeInfStr = dataArray(5) '&&信息类型
    274. DevBufferSwitchStr = dataArray(6) '继电器状态(bit0为第1门bit1为第2门)
    275. DevBufferFireStr = dataArray(7) '消防输入状态
    276. DevBufferRandomCode = dataArray(8) '&&动态随机码(8位十六进制)
    277. DevBufferDatetimeStr = dataArray(9) '日期时间
    278. DevBufferSerialNumStr = dataArray(10) '设备硬件号
    279. Me.Invoke(New EditTC(AddressOf EditTCNow), 10, DevBufferRandomCode)
    280. End Select
    281. Catch ex As Exception
    282. Me.Invoke(New EditTC(AddressOf EditTCNow), 2, "ERROR:" & vbCrLf & ex.GetHashCode & ex.Message & vbCrLf)
    283. End Try
    284. End While
    285. End Sub

     

  • 相关阅读:
    设计模式(五)设计原则part2
    Istio-EnvoyFilter配置浅谈
    使用uwsgi和gunicorn部署Django项目(智能客服系统)
    时间戳的理解
    【Overload游戏引擎分析】编辑器对象鼠标拾取原理
    《uni-app》一个非canvas的飞机对战小游戏实现-子弹模型的实现
    element ui框架(路由参数传递)
    DO280管理和监控OpenShift平台--Web控制台使用
    电源硬件设计----升降压变换器(负压输出)基础
    windows 11 安装 ubuntu
  • 原文地址:https://blog.csdn.net/zhangjin7422/article/details/134444416