• VB.net TCP服务端监听端口接收客户端RFID网络读卡器上传的读卡数据


    本 示例使用设备介绍:WIFI/TCP/UDP/HTTP协议RFID液显网络读卡器可二次开发语音播报POE-淘宝网 (taobao.com)

    1. Imports System.Threading
    2. Imports System.Net
    3. Imports System.Net.Sockets
    4. Public Class Form1
    5. Dim ListenSocket As Socket
    6. Dim Dict As New Dictionary(Of String, Socket) '用于保存连接的客户的套接字的键值对集合
    7. Dim DictThre As New Dictionary(Of String, Thread) '用于保存通信线程的键值对集合
    8. Dim LocalIp As String
    9. Dim SendBuff() As Byte
    10. Public Sub getIp() '获取本机所有网卡的IP
    11. Dim Address() As System.Net.IPAddress
    12. Dim i As Integer
    13. Address = Dns.GetHostByName(Dns.GetHostName()).AddressList
    14. If UBound(Address) < 0 Then
    15. MsgBox("未能查找到本台电脑安装的网卡,暂不能启动本软件。", MsgBoxStyle.Critical + vbOKOnly, "注意")
    16. End
    17. Else
    18. For i = 0 To UBound(Address)
    19. comboBox4.Items.Add(Address(i).ToString())
    20. Next
    21. comboBox4.SelectedIndex = 0
    22. LocalIp = comboBox4.Text.Trim()
    23. End If
    24. End Sub
    25. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    26. getIp()
    27. comboBox4.SelectedIndex = 0
    28. End Sub
    29. Private Sub btn_conn_Click(sender As Object, e As EventArgs) Handles btn_conn.Click
    30. If btn_conn.Text = "开启TCP服务,允许新客户端接入" Then
    31. TextBox.CheckForIllegalCrossThreadCalls = False '取消文本框的跨线程检查
    32. Dim localAddress As IPAddress = IPAddress.Parse(comboBox4.Text.Trim())
    33. Dim EndPoint As New IPEndPoint(localAddress, txb_port.Text) '创建一个网络节点对象
    34. ListenSocket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
    35. ListenSocket.Bind(EndPoint) '给负责监听的套接字绑定一个网络节点
    36. ListenSocket.Listen(100) '侦听,最多接受100个连接
    37. Dim thre = New Thread(AddressOf Connect) '创建一个新的线程用于处理客户端发来的连接请求
    38. thre.IsBackground = True '设为后台线程
    39. thre.Start() '开启线程
    40. btn_conn.Text = "停止新客户端连接"
    41. listBox2.Items.Add("TCP端口监听服务已开启,新客户端设备可以连接并上传数据......")
    42. listBox2.Items.Add("")
    43. listBox2.SelectedIndex = listBox2.Items.Count - 1
    44. Else
    45. ListenSocket.Close()
    46. ListenSocket = Nothing
    47. btn_conn.Text = "开启TCP服务,允许新客户端接入"
    48. listBox2.Items.Add("TCP服务端已禁止新客户端连接,已连接的客户端设备可继续上传数据......")
    49. listBox2.Items.Add("")
    50. listBox2.SelectedIndex = listBox2.Items.Count - 1
    51. End If
    52. End Sub
    53. Sub Connect() '处理客户端的连接请求的过程
    54. While True
    55. Try
    56. Dim SockConect As Socket = listenSocket.Accept
    57. Dict.Add(SockConect.RemoteEndPoint.ToString, SockConect) '将连接成功的套接字添加到键值对集合
    58. listBox1.Items.Add(SockConect.RemoteEndPoint.ToString) '添加到列表
    59. Dim Thre As New Thread(AddressOf RecClient) '创建一个新的线程用于和链接成功的套接字通信
    60. Thre.IsBackground = True '设为后台线程
    61. Thre.Start(SockConect)
    62. DictThre.Add(SockConect.RemoteEndPoint.ToString, Thre) '将创建的通信线程添加到键值对集合
    63. Catch
    64. End Try
    65. End While
    66. End Sub
    67. Sub RecClient(ByVal SockTelNet As Socket) '处理客户端发来的数据
    68. While True
    69. Try
    70. Dim getdata(1024) As Byte
    71. Dim RecLen As Int32
    72. Dim HexStr As String
    73. Try '捕获异常
    74. RecLen = SockTelNet.Receive(getdata) '接受客户端发来得信息
    75. Catch ss As SocketException
    76. listBox2.Items.Add(ss.NativeErrorCode & vbCrLf & ss.Message) '显示错误信息
    77. Dict.Remove(SockTelNet.RemoteEndPoint.ToString) '移除断开连接的套接字
    78. Return
    79. Catch s As Exception
    80. listBox2.Items.Add(s.Message)
    81. Return
    82. End Try
    83. If RecLen > 0 Then
    84. Dim StrMsg As String
    85. StrMsg = DateTime.Now.ToLongTimeString() + " Get From " + SockTelNet.RemoteEndPoint.ToString + " : "
    86. For i = 0 To RecLen - 1
    87. StrMsg = StrMsg + getdata(i).ToString("X2") + " "
    88. Next
    89. If listBox2.Items.Count() > 100 Then listBox2.Items.Clear()
    90. listBox2.Items.Add(StrMsg)
    91. Select Case getdata(0)
    92. Case &HC1, &HCF
    93. If getdata(0) = &HC1 Then
    94. StrMsg = "数据解析:IC读卡器上传卡号,"
    95. Else
    96. StrMsg = "数据解析:IC卡离开读卡器,"
    97. End If
    98. StrMsg = StrMsg + "IP[" + getdata(1).ToString("D") + "." + getdata(2).ToString("D") + "." + getdata(3).ToString("D") + "." + getdata(4).ToString("D") + "],"
    99. StrMsg = StrMsg + "机号[" + (getdata(5) + getdata(6) * 256).ToString("D") + "],"
    100. StrMsg = StrMsg + "数据包号[" + (getdata(7) + getdata(8) * 256).ToString("D") + "],"
    101. StrMsg = StrMsg + "卡号长度[" + getdata(9).ToString("D") + "],"
    102. HexStr = ""
    103. For i = 10 To 10 + getdata(9) - 1
    104. HexStr = HexStr + getdata(i).ToString("X2")
    105. Next
    106. StrMsg = StrMsg + "16进制卡号[" + HexStr + "],"
    107. HexStr = ""
    108. For i = 10 + getdata(9) To RecLen - 1
    109. HexStr = HexStr + getdata(i).ToString("X2")
    110. Next
    111. StrMsg = StrMsg + "唯一硬件序号[" + HexStr + "]"
    112. listBox2.Items.Add(StrMsg)
    113. listBox2.Items.Add("")
    114. listBox2.SelectedIndex = listBox2.Items.Count - 1
    115. If CheckBox1.Checked Then
    116. GetRespData()
    117. SockTelNet.Send(SendBuff)
    118. StrMsg = DateTime.Now.ToLongTimeString() + " Send To " + SockTelNet.RemoteEndPoint.ToString + " : "
    119. For i = 0 To SendBuff.Length - 1
    120. StrMsg = StrMsg + SendBuff(i).ToString("X2") + " "
    121. Next
    122. listBox2.Items.Add(StrMsg)
    123. listBox2.Items.Add("")
    124. listBox2.SelectedIndex = listBox2.Items.Count - 1
    125. End If
    126. Case &HD1, &HDF
    127. If getdata(0) = &HD1 Then
    128. StrMsg = "数据解析:ID读卡器上传卡号,"
    129. Else
    130. StrMsg = "数据解析:ID卡离开读卡器,"
    131. End If
    132. StrMsg = StrMsg + "IP[" + getdata(1).ToString("D") + "." + getdata(2).ToString("D") + "." + getdata(3).ToString("D") + "." + getdata(4).ToString("D") + "],"
    133. StrMsg = StrMsg + "机号[" + (getdata(5) + getdata(6) * 256).ToString("D") + "],"
    134. StrMsg = StrMsg + "数据包号[" + (getdata(7) + getdata(8) * 256).ToString("D") + "],"
    135. StrMsg = StrMsg + "16进制卡号[" + getdata(9).ToString("X2") + getdata(10).ToString("X2") + getdata(11).ToString("X2") + getdata(12).ToString("X2") + getdata(13).ToString("X2") + "],"
    136. HexStr = ""
    137. For i = 14 To RecLen - 1
    138. HexStr = HexStr + getdata(i).ToString("X2")
    139. Next
    140. StrMsg = StrMsg + "唯一硬件序号[" + HexStr + "]"
    141. listBox2.Items.Add(StrMsg)
    142. listBox2.Items.Add("")
    143. listBox2.SelectedIndex = listBox2.Items.Count - 1
    144. Case &HF3
    145. StrMsg = "数据解析:读卡器心跳数据包,"
    146. StrMsg = StrMsg + "IP[" + getdata(1).ToString("D") + "." + getdata(2).ToString("D") + "." + getdata(3).ToString("D") + "." + getdata(4).ToString("D") + "],"
    147. StrMsg = StrMsg + "机号[" + (getdata(5) + getdata(6) * 256).ToString("D") + "],"
    148. StrMsg = StrMsg + "数据包号[" + (getdata(7) + getdata(8) * 256).ToString("D") + "],"
    149. StrMsg = StrMsg + "心跳类型[" + getdata(9).ToString("X2") + "],"
    150. StrMsg = StrMsg + "长度[" + getdata(10).ToString("D") + "],"
    151. StrMsg = StrMsg + "继电器状态[" + getdata(11).ToString("X2") + "],"
    152. StrMsg = StrMsg + "外部输入状态[" + getdata(12).ToString("X2") + "],"
    153. StrMsg = StrMsg + "随机动态码[" + getdata(13).ToString("X2") + getdata(14).ToString("X2") + getdata(15).ToString("X2") + getdata(17).ToString("X2") + "],"
    154. HexStr = ""
    155. For i = 17 To RecLen - 1
    156. HexStr = HexStr + getdata(i).ToString("X2")
    157. Next
    158. StrMsg = StrMsg + "唯一硬件序号[" + HexStr + "]"
    159. listBox2.Items.Add(StrMsg)
    160. listBox2.Items.Add("")
    161. listBox2.SelectedIndex = listBox2.Items.Count - 1
    162. End Select
    163. End If
    164. Catch
    165. End Try
    166. End While
    167. End Sub
    168. '选择在线设备向其发送指令
    169. Sub ButtoSend(ByVal sendcode As Integer)
    170. Dim seleid As String
    171. Dim dispstr As String
    172. Dim i As Integer
    173. If listBox1.SelectedIndex >= 0 Then
    174. seleid = listBox1.Text
    175. GetSenddata(sendcode)
    176. Dict.Item(seleid).Send(SendBuff)
    177. dispstr = DateTime.Now.ToLongTimeString() + " Send To " + seleid + " : "
    178. For i = 0 To SendBuff.Length - 1
    179. dispstr = dispstr + SendBuff(i).ToString("X2") + " "
    180. Next
    181. listBox2.Items.Add(dispstr)
    182. listBox2.Items.Add("")
    183. listBox2.SelectedIndex = listBox2.Items.Count - 1
    184. Else
    185. MsgBox("请先在客户端列表中选择要发送指令的在线客户端!", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "提示")
    186. End If
    187. End Sub
    188. '按回应需要生成发送缓冲数据
    189. Sub GetRespData()
    190. If RadioButton3.Checked Then
    191. GetSenddata(0)
    192. ElseIf RadioButton4.Checked Then
    193. GetSenddata(1)
    194. ElseIf RadioButton5.Checked Then
    195. GetSenddata(2)
    196. Else
    197. GetSenddata(3)
    198. End If
    199. End Sub
    200. '按发送需要生成发送缓冲数据
    201. Sub GetSenddata(ByVal sendcode As Integer)
    202. Dim i As Long
    203. Dim strs As String
    204. Dim textbyte() As Byte
    205. Select Case sendcode
    206. Case 0
    207. ReDim SendBuff(38)
    208. SendBuff(0) = &H5A '驱动显示文字+蜂鸣响声的功能码
    209. SendBuff(1) = 0 '机号低位
    210. SendBuff(2) = 0 '机号高位,高低位为0表示任意机号
    211. If checkBox2.Checked Then '蜂鸣响声
    212. SendBuff(3) = comboBox3.SelectedIndex '蜂鸣响声代码
    213. If radioButton2.Checked Then '背光灯状态不变
    214. SendBuff(3) = SendBuff(3) Or 128
    215. End If
    216. Else
    217. SendBuff(3) = &HFF '不响蜂鸣声
    218. If radioButton2.Checked Then '背光灯状态不变
    219. SendBuff(3) = SendBuff(3) And 127
    220. End If
    221. End If
    222. SendBuff(4) = dispdelay.Value
    223. strs = textBox12.Text + " "
    224. textbyte = System.Text.Encoding.GetEncoding(936).GetBytes(strs)
    225. For i = 0 To 33
    226. SendBuff(5 + i) = textbyte(i)
    227. Next
    228. Case 1
    229. strs = "[v" + SYDX.Value.ToString() + "]" '设置语音大小,在需要发送的语音字符串中任何位置加入[v10],表示将音量调到10级(范围0~16,0表示静音,16最大,每次重开机后,音量重置为10级)!
    230. strs = strs + textBox1.Text.Trim()
    231. textbyte = System.Text.Encoding.GetEncoding(936).GetBytes(strs)
    232. Dim displen As Integer
    233. Dim voiclen As Integer
    234. Dim commlen As Integer
    235. displen = 34
    236. voiclen = textbyte.Length
    237. commlen = 10 + displen + voiclen + 4
    238. ReDim SendBuff(commlen)
    239. SendBuff(0) = &H5C '驱动显示文字+蜂鸣响声的功能码+开继电器+播报TTS语音
    240. SendBuff(1) = 0 '机号低位
    241. SendBuff(2) = 0 '机号高位,高低位为0表示任意机号
    242. If checkBox2.Checked Then '蜂鸣响声
    243. SendBuff(3) = comboBox3.SelectedIndex '蜂鸣响声代码
    244. If radioButton2.Checked Then '背光灯状态不变
    245. SendBuff(3) = SendBuff(3) Or 128
    246. End If
    247. Else
    248. SendBuff(3) = &HFF '不响蜂鸣声
    249. If radioButton2.Checked Then '背光灯状态不变
    250. SendBuff(3) = SendBuff(3) And 127
    251. End If
    252. End If
    253. Select Case comboBox2.SelectedIndex '根据选择开启对应的继电器
    254. Case 1
    255. SendBuff(4) = &HF1
    256. Case 2
    257. SendBuff(4) = &HF2
    258. Case 3
    259. SendBuff(4) = &HF3
    260. Case 4
    261. SendBuff(4) = &HF4
    262. Case 5
    263. SendBuff(4) = &HF5
    264. Case 6
    265. SendBuff(4) = &HF6
    266. Case 7
    267. SendBuff(4) = &HF7
    268. Case 8
    269. SendBuff(4) = &HF8
    270. Case Else
    271. SendBuff(4) = &HF0
    272. End Select
    273. i = CLng(textBox11.Text) '继电器开启时长
    274. SendBuff(5) = i Mod 256
    275. SendBuff(6) = Int(i / 256) Mod 256
    276. SendBuff(7) = dispdelay.Value '显示时长
    277. SendBuff(8) = 0 '显示起始位
    278. SendBuff(9) = displen '显示长度
    279. SendBuff(10) = voiclen 'TTS语音长度
    280. strs = textBox12.Text + " "
    281. Dim dispbyte() As Byte
    282. dispbyte = System.Text.Encoding.GetEncoding(936).GetBytes(strs)
    283. For i = 0 To displen - 1 '显示文字
    284. SendBuff(11 + i) = dispbyte(i)
    285. Next
    286. For i = 0 To voiclen - 1 'TTS语音
    287. SendBuff(11 + displen + i) = textbyte(i)
    288. Next
    289. SendBuff(11 + displen + voiclen + 0) = &H55 '防干扰后缀
    290. SendBuff(11 + displen + voiclen + 1) = &HAA
    291. SendBuff(11 + displen + voiclen + 2) = &H66
    292. SendBuff(11 + displen + voiclen + 3) = &H99
    293. Case 2
    294. ReDim SendBuff(3)
    295. SendBuff(0) = &H96 '驱动蜂鸣响声的功能码
    296. SendBuff(1) = 0 '机号低位
    297. SendBuff(2) = 0 '机号高位,高低位为0表示任意机号
    298. SendBuff(3) = comboBox3.SelectedIndex
    299. Case 3
    300. ReDim SendBuff(5)
    301. SendBuff(0) = &H78 '驱动开关继电器的功能码
    302. SendBuff(1) = 0 '机号低位
    303. SendBuff(2) = 0 '机号高位,高低位为0表示任意机号
    304. Select Case comboBox2.SelectedIndex '根据选择开启对应的继电器
    305. Case 1
    306. SendBuff(3) = &HF1
    307. Case 2
    308. SendBuff(3) = &HF2
    309. Case 3
    310. SendBuff(3) = &HF3
    311. Case 4
    312. SendBuff(3) = &HF4
    313. Case 5
    314. SendBuff(3) = &HF5
    315. Case 6
    316. SendBuff(3) = &HF6
    317. Case 7
    318. SendBuff(3) = &HF7
    319. Case 8
    320. SendBuff(3) = &HF8
    321. Case Else
    322. SendBuff(3) = &HF0
    323. End Select
    324. i = CLng(textBox11.Text) '开启时长
    325. SendBuff(4) = i Mod 256
    326. SendBuff(5) = Int(i / 256) Mod 256
    327. Case 4
    328. ReDim SendBuff(5)
    329. SendBuff(0) = &H78 '驱动开关继电器的功能码
    330. SendBuff(1) = 0 '机号低位
    331. SendBuff(2) = 0 '机号高位,高低位为0表示任意机号
    332. Select Case comboBox2.SelectedIndex '根据选择关闭对应的继电器
    333. Case 1
    334. SendBuff(3) = &HE1
    335. Case 2
    336. SendBuff(3) = &HE2
    337. Case 3
    338. SendBuff(3) = &HE3
    339. Case 4
    340. SendBuff(3) = &HE4
    341. Case 5
    342. SendBuff(3) = &HE5
    343. Case 6
    344. SendBuff(3) = &HE6
    345. Case 7
    346. SendBuff(3) = &HE7
    347. Case 8
    348. SendBuff(3) = &HE8
    349. Case Else
    350. SendBuff(3) = &HE0
    351. End Select
    352. i = CLng(textBox11.Text) '开启时长
    353. SendBuff(4) = i Mod 256
    354. SendBuff(5) = Int(i / 256) Mod 256
    355. End Select
    356. End Sub
    357. Private Sub button6_Click(sender As Object, e As EventArgs) Handles button6.Click
    358. ButtoSend(2)
    359. End Sub
    360. Private Sub button7_Click(sender As Object, e As EventArgs) Handles button7.Click
    361. ButtoSend(3)
    362. End Sub
    363. Private Sub button8_Click(sender As Object, e As EventArgs) Handles button8.Click
    364. ButtoSend(4)
    365. End Sub
    366. Private Sub button10_Click(sender As Object, e As EventArgs) Handles button10.Click
    367. ButtoSend(0)
    368. End Sub
    369. Private Sub button9_Click(sender As Object, e As EventArgs) Handles button9.Click
    370. ButtoSend(1)
    371. End Sub
    372. Private Sub button3_Click(sender As Object, e As EventArgs) Handles button3.Click
    373. Dim copstr As String
    374. Dim I As Long
    375. Clipboard.Clear()
    376. copstr = ""
    377. For I = 0 To listBox2.Items.Count - 1
    378. copstr = copstr & listBox2.Items(I)
    379. copstr = copstr & vbCrLf
    380. Next
    381. Clipboard.SetText(copstr)
    382. MsgBox("TCP通讯日志报文已拷贝!", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "提示")
    383. End Sub
    384. Private Sub button2_Click(sender As Object, e As EventArgs) Handles button2.Click
    385. listBox2.Items.Clear()
    386. End Sub
    387. End Class

     

  • 相关阅读:
    PAT A1018 Public Bike Management(Dijkstra + DFS)
    模型部署——CenterPoint转ONNX(自定义onnx算子)
    2023年9月青少年软件编程(Python) 等级考试试卷(三级)
    day-43 代码随想录算法训练营(19) 动态规划 part 05
    beetlsql3.x版本适配达梦数据库
    十、面向对象 之 多态
    测试/开发程序员的思考,突破变得更强......
    工作应当有挑战
    BUUCTF学习(8): 随便注,SQL
    全网最全最深:web前端架构师面试题+缜密全面的学习笔记
  • 原文地址:https://blog.csdn.net/zhangjin7422/article/details/134376115