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

news/2024/5/17 15:14:12 标签: VB.net, TCP, Server, RFID, 通讯, 端口监听

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

Imports System.Threading
Imports System.Net
Imports System.Net.Sockets
Public Class Form1
    Dim ListenSocket As Socket

    Dim Dict As New Dictionary(Of String, Socket) '用于保存连接的客户的套接字的键值对集合
    Dim DictThre As New Dictionary(Of String, Thread) '用于保存通信线程的键值对集合
    Dim LocalIp As String

    Dim SendBuff() As Byte

    Public Sub getIp()     '获取本机所有网卡的IP
        Dim Address() As System.Net.IPAddress
        Dim i As Integer
        Address = Dns.GetHostByName(Dns.GetHostName()).AddressList
        If UBound(Address) < 0 Then
            MsgBox("未能查找到本台电脑安装的网卡,暂不能启动本软件。", MsgBoxStyle.Critical + vbOKOnly, "注意")
            End
        Else
            For i = 0 To UBound(Address)
                comboBox4.Items.Add(Address(i).ToString())
            Next
            comboBox4.SelectedIndex = 0
            LocalIp = comboBox4.Text.Trim()
        End If
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        getIp()
        comboBox4.SelectedIndex = 0
    End Sub

    Private Sub btn_conn_Click(sender As Object, e As EventArgs) Handles btn_conn.Click
        If btn_conn.Text = "开启TCP服务,允许新客户端接入" Then
            TextBox.CheckForIllegalCrossThreadCalls = False             '取消文本框的跨线程检查
            Dim localAddress As IPAddress = IPAddress.Parse(comboBox4.Text.Trim())
            Dim EndPoint As New IPEndPoint(localAddress, txb_port.Text) '创建一个网络节点对象
            ListenSocket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
            ListenSocket.Bind(EndPoint)                                 '给负责监听的套接字绑定一个网络节点
            ListenSocket.Listen(100)                                    '侦听,最多接受100个连接
            Dim thre = New Thread(AddressOf Connect)                    '创建一个新的线程用于处理客户端发来的连接请求
            thre.IsBackground = True                                    '设为后台线程
            thre.Start()                                                '开启线程           
            btn_conn.Text = "停止新客户端连接"
            listBox2.Items.Add("TCP端口监听服务已开启,新客户端设备可以连接并上传数据......")
            listBox2.Items.Add("")
            listBox2.SelectedIndex = listBox2.Items.Count - 1

        Else            
            ListenSocket.Close()
            ListenSocket = Nothing
            btn_conn.Text = "开启TCP服务,允许新客户端接入"
            listBox2.Items.Add("TCP服务端已禁止新客户端连接,已连接的客户端设备可继续上传数据......")
            listBox2.Items.Add("")
            listBox2.SelectedIndex = listBox2.Items.Count - 1
        End If
    End Sub

    Sub Connect() '处理客户端的连接请求的过程
        While True
            Try
                Dim SockConect As Socket = listenSocket.Accept
                Dict.Add(SockConect.RemoteEndPoint.ToString, SockConect)    '将连接成功的套接字添加到键值对集合
                listBox1.Items.Add(SockConect.RemoteEndPoint.ToString)      '添加到列表
                Dim Thre As New Thread(AddressOf RecClient)                 '创建一个新的线程用于和链接成功的套接字通信
                Thre.IsBackground = True                                    '设为后台线程
                Thre.Start(SockConect)
                DictThre.Add(SockConect.RemoteEndPoint.ToString, Thre)      '将创建的通信线程添加到键值对集合
            Catch

            End Try

        End While
    End Sub

    Sub RecClient(ByVal SockTelNet As Socket) '处理客户端发来的数据
        While True
            Try
                Dim getdata(1024) As Byte
                Dim RecLen As Int32
                Dim HexStr As String

                Try '捕获异常
                    RecLen = SockTelNet.Receive(getdata) '接受客户端发来得信息
                Catch ss As SocketException
                    listBox2.Items.Add(ss.NativeErrorCode & vbCrLf & ss.Message) '显示错误信息
                    Dict.Remove(SockTelNet.RemoteEndPoint.ToString) '移除断开连接的套接字                    
                    Return
                Catch s As Exception
                    listBox2.Items.Add(s.Message)
                    Return
                End Try

                If RecLen > 0 Then
                    Dim StrMsg As String
                    StrMsg = DateTime.Now.ToLongTimeString() + "  Get From " + SockTelNet.RemoteEndPoint.ToString + " : "
                    For i = 0 To RecLen - 1
                        StrMsg = StrMsg + getdata(i).ToString("X2") + " "
                    Next
                    If listBox2.Items.Count() > 100 Then listBox2.Items.Clear()
                    listBox2.Items.Add(StrMsg)

                    Select Case getdata(0)
                        Case &HC1, &HCF
                            If getdata(0) = &HC1 Then
                                StrMsg = "数据解析:IC读卡器上传卡号,"
                            Else
                                StrMsg = "数据解析:IC卡离开读卡器,"
                            End If
                            StrMsg = StrMsg + "IP[" + getdata(1).ToString("D") + "." + getdata(2).ToString("D") + "." + getdata(3).ToString("D") + "." + getdata(4).ToString("D") + "],"
                            StrMsg = StrMsg + "机号[" + (getdata(5) + getdata(6) * 256).ToString("D") + "],"
                            StrMsg = StrMsg + "数据包号[" + (getdata(7) + getdata(8) * 256).ToString("D") + "],"
                            StrMsg = StrMsg + "卡号长度[" + getdata(9).ToString("D") + "],"
                            HexStr = ""
                            For i = 10 To 10 + getdata(9) - 1
                                HexStr = HexStr + getdata(i).ToString("X2")
                            Next
                            StrMsg = StrMsg + "16进制卡号[" + HexStr + "],"
                            HexStr = ""
                            For i = 10 + getdata(9) To RecLen - 1
                                HexStr = HexStr + getdata(i).ToString("X2")
                            Next
                            StrMsg = StrMsg + "唯一硬件序号[" + HexStr + "]"
                            listBox2.Items.Add(StrMsg)
                            listBox2.Items.Add("")
                            listBox2.SelectedIndex = listBox2.Items.Count - 1

                            If CheckBox1.Checked Then
                                GetRespData()
                                SockTelNet.Send(SendBuff)

                                StrMsg = DateTime.Now.ToLongTimeString() + "  Send To  " + SockTelNet.RemoteEndPoint.ToString + " : "
                                For i = 0 To SendBuff.Length - 1
                                    StrMsg = StrMsg + SendBuff(i).ToString("X2") + " "
                                Next
                                listBox2.Items.Add(StrMsg)
                                listBox2.Items.Add("")
                                listBox2.SelectedIndex = listBox2.Items.Count - 1
                            End If

                        Case &HD1, &HDF
                            If getdata(0) = &HD1 Then
                                StrMsg = "数据解析:ID读卡器上传卡号,"
                            Else
                                StrMsg = "数据解析:ID卡离开读卡器,"
                            End If
                            StrMsg = StrMsg + "IP[" + getdata(1).ToString("D") + "." + getdata(2).ToString("D") + "." + getdata(3).ToString("D") + "." + getdata(4).ToString("D") + "],"
                            StrMsg = StrMsg + "机号[" + (getdata(5) + getdata(6) * 256).ToString("D") + "],"
                            StrMsg = StrMsg + "数据包号[" + (getdata(7) + getdata(8) * 256).ToString("D") + "],"
                            StrMsg = StrMsg + "16进制卡号[" + getdata(9).ToString("X2") + getdata(10).ToString("X2") + getdata(11).ToString("X2") + getdata(12).ToString("X2") + getdata(13).ToString("X2") + "],"
                            HexStr = ""
                            For i = 14 To RecLen - 1
                                HexStr = HexStr + getdata(i).ToString("X2")
                            Next
                            StrMsg = StrMsg + "唯一硬件序号[" + HexStr + "]"
                            listBox2.Items.Add(StrMsg)
                            listBox2.Items.Add("")
                            listBox2.SelectedIndex = listBox2.Items.Count - 1

                        Case &HF3
                            StrMsg = "数据解析:读卡器心跳数据包,"
                            StrMsg = StrMsg + "IP[" + getdata(1).ToString("D") + "." + getdata(2).ToString("D") + "." + getdata(3).ToString("D") + "." + getdata(4).ToString("D") + "],"
                            StrMsg = StrMsg + "机号[" + (getdata(5) + getdata(6) * 256).ToString("D") + "],"
                            StrMsg = StrMsg + "数据包号[" + (getdata(7) + getdata(8) * 256).ToString("D") + "],"
                            StrMsg = StrMsg + "心跳类型[" + getdata(9).ToString("X2") + "],"
                            StrMsg = StrMsg + "长度[" + getdata(10).ToString("D") + "],"
                            StrMsg = StrMsg + "继电器状态[" + getdata(11).ToString("X2") + "],"
                            StrMsg = StrMsg + "外部输入状态[" + getdata(12).ToString("X2") + "],"
                            StrMsg = StrMsg + "随机动态码[" + getdata(13).ToString("X2") + getdata(14).ToString("X2") + getdata(15).ToString("X2") + getdata(17).ToString("X2") + "],"
                            HexStr = ""
                            For i = 17 To RecLen - 1
                                HexStr = HexStr + getdata(i).ToString("X2")
                            Next
                            StrMsg = StrMsg + "唯一硬件序号[" + HexStr + "]"
                            listBox2.Items.Add(StrMsg)
                            listBox2.Items.Add("")
                            listBox2.SelectedIndex = listBox2.Items.Count - 1
                    End Select

                End If
            Catch
            End Try
        End While
    End Sub
    '选择在线设备向其发送指令
    Sub ButtoSend(ByVal sendcode As Integer)
        Dim seleid As String
        Dim dispstr As String
        Dim i As Integer

        If listBox1.SelectedIndex >= 0 Then
            seleid = listBox1.Text
            GetSenddata(sendcode)
            Dict.Item(seleid).Send(SendBuff)

            dispstr = DateTime.Now.ToLongTimeString() + "  Send To  " + seleid + " : "
            For i = 0 To SendBuff.Length - 1
                dispstr = dispstr + SendBuff(i).ToString("X2") + " "
            Next
            listBox2.Items.Add(dispstr)
            listBox2.Items.Add("")
            listBox2.SelectedIndex = listBox2.Items.Count - 1
        Else
            MsgBox("请先在客户端列表中选择要发送指令的在线客户端!", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "提示")
        End If
    End Sub

    '按回应需要生成发送缓冲数据
    Sub GetRespData()
        If RadioButton3.Checked Then
            GetSenddata(0)
        ElseIf RadioButton4.Checked Then
            GetSenddata(1)
        ElseIf RadioButton5.Checked Then
            GetSenddata(2)
        Else
            GetSenddata(3)
        End If
    End Sub

    '按发送需要生成发送缓冲数据
    Sub GetSenddata(ByVal sendcode As Integer)
        Dim i As Long
        Dim strs As String
        Dim textbyte() As Byte

        Select Case sendcode
            Case 0
                ReDim SendBuff(38)
                SendBuff(0) = &H5A      '驱动显示文字+蜂鸣响声的功能码
                SendBuff(1) = 0         '机号低位
                SendBuff(2) = 0         '机号高位,高低位为0表示任意机号
                If checkBox2.Checked Then   '蜂鸣响声
                    SendBuff(3) = comboBox3.SelectedIndex   '蜂鸣响声代码
                    If radioButton2.Checked Then        '背光灯状态不变
                        SendBuff(3) = SendBuff(3) Or 128
                    End If
                Else
                    SendBuff(3) = &HFF              '不响蜂鸣声
                    If radioButton2.Checked Then    '背光灯状态不变
                        SendBuff(3) = SendBuff(3) And 127
                    End If
                End If
                SendBuff(4) = dispdelay.Value

                strs = textBox12.Text + "                                   "
                textbyte = System.Text.Encoding.GetEncoding(936).GetBytes(strs)
                For i = 0 To 33
                    SendBuff(5 + i) = textbyte(i)
                Next

            Case 1
                strs = "[v" + SYDX.Value.ToString() + "]"    '设置语音大小,在需要发送的语音字符串中任何位置加入[v10],表示将音量调到10级(范围0~16,0表示静音,16最大,每次重开机后,音量重置为10级)!
                strs = strs + textBox1.Text.Trim()
                textbyte = System.Text.Encoding.GetEncoding(936).GetBytes(strs)
                Dim displen As Integer
                Dim voiclen As Integer
                Dim commlen As Integer

                displen = 34
                voiclen = textbyte.Length
                commlen = 10 + displen + voiclen + 4

                ReDim SendBuff(commlen)
                SendBuff(0) = &H5C      '驱动显示文字+蜂鸣响声的功能码+开继电器+播报TTS语音
                SendBuff(1) = 0         '机号低位
                SendBuff(2) = 0         '机号高位,高低位为0表示任意机号
                If checkBox2.Checked Then   '蜂鸣响声
                    SendBuff(3) = comboBox3.SelectedIndex   '蜂鸣响声代码
                    If radioButton2.Checked Then        '背光灯状态不变
                        SendBuff(3) = SendBuff(3) Or 128
                    End If
                Else
                    SendBuff(3) = &HFF              '不响蜂鸣声
                    If radioButton2.Checked Then    '背光灯状态不变
                        SendBuff(3) = SendBuff(3) And 127
                    End If
                End If

                Select Case comboBox2.SelectedIndex '根据选择开启对应的继电器
                    Case 1
                        SendBuff(4) = &HF1
                    Case 2
                        SendBuff(4) = &HF2
                    Case 3
                        SendBuff(4) = &HF3
                    Case 4
                        SendBuff(4) = &HF4
                    Case 5
                        SendBuff(4) = &HF5
                    Case 6
                        SendBuff(4) = &HF6
                    Case 7
                        SendBuff(4) = &HF7
                    Case 8
                        SendBuff(4) = &HF8
                    Case Else
                        SendBuff(4) = &HF0
                End Select
                i = CLng(textBox11.Text)    '继电器开启时长
                SendBuff(5) = i Mod 256
                SendBuff(6) = Int(i / 256) Mod 256

                SendBuff(7) = dispdelay.Value  '显示时长
                SendBuff(8) = 0                '显示起始位
                SendBuff(9) = displen          '显示长度
                SendBuff(10) = voiclen         'TTS语音长度

                strs = textBox12.Text + "                                   "
                Dim dispbyte() As Byte
                dispbyte = System.Text.Encoding.GetEncoding(936).GetBytes(strs)
                For i = 0 To displen - 1        '显示文字
                    SendBuff(11 + i) = dispbyte(i)
                Next

                For i = 0 To voiclen - 1        'TTS语音
                    SendBuff(11 + displen + i) = textbyte(i)
                Next

                SendBuff(11 + displen + voiclen + 0) = &H55     '防干扰后缀
                SendBuff(11 + displen + voiclen + 1) = &HAA
                SendBuff(11 + displen + voiclen + 2) = &H66
                SendBuff(11 + displen + voiclen + 3) = &H99

            Case 2
                ReDim SendBuff(3)
                SendBuff(0) = &H96      '驱动蜂鸣响声的功能码
                SendBuff(1) = 0         '机号低位
                SendBuff(2) = 0         '机号高位,高低位为0表示任意机号
                SendBuff(3) = comboBox3.SelectedIndex

            Case 3
                ReDim SendBuff(5)
                SendBuff(0) = &H78      '驱动开关继电器的功能码
                SendBuff(1) = 0         '机号低位
                SendBuff(2) = 0         '机号高位,高低位为0表示任意机号
                Select Case comboBox2.SelectedIndex '根据选择开启对应的继电器
                    Case 1
                        SendBuff(3) = &HF1
                    Case 2
                        SendBuff(3) = &HF2
                    Case 3
                        SendBuff(3) = &HF3
                    Case 4
                        SendBuff(3) = &HF4
                    Case 5
                        SendBuff(3) = &HF5
                    Case 6
                        SendBuff(3) = &HF6
                    Case 7
                        SendBuff(3) = &HF7
                    Case 8
                        SendBuff(3) = &HF8
                    Case Else
                        SendBuff(3) = &HF0
                End Select
                i = CLng(textBox11.Text)    '开启时长
                SendBuff(4) = i Mod 256
                SendBuff(5) = Int(i / 256) Mod 256

            Case 4
                ReDim SendBuff(5)
                SendBuff(0) = &H78      '驱动开关继电器的功能码
                SendBuff(1) = 0         '机号低位
                SendBuff(2) = 0         '机号高位,高低位为0表示任意机号
                Select Case comboBox2.SelectedIndex '根据选择关闭对应的继电器
                    Case 1
                        SendBuff(3) = &HE1
                    Case 2
                        SendBuff(3) = &HE2
                    Case 3
                        SendBuff(3) = &HE3
                    Case 4
                        SendBuff(3) = &HE4
                    Case 5
                        SendBuff(3) = &HE5
                    Case 6
                        SendBuff(3) = &HE6
                    Case 7
                        SendBuff(3) = &HE7
                    Case 8
                        SendBuff(3) = &HE8
                    Case Else
                        SendBuff(3) = &HE0
                End Select
                i = CLng(textBox11.Text)    '开启时长
                SendBuff(4) = i Mod 256
                SendBuff(5) = Int(i / 256) Mod 256
        End Select
    End Sub
    Private Sub button6_Click(sender As Object, e As EventArgs) Handles button6.Click
        ButtoSend(2)
    End Sub

    Private Sub button7_Click(sender As Object, e As EventArgs) Handles button7.Click
        ButtoSend(3)
    End Sub

    Private Sub button8_Click(sender As Object, e As EventArgs) Handles button8.Click
        ButtoSend(4)
    End Sub

    Private Sub button10_Click(sender As Object, e As EventArgs) Handles button10.Click
        ButtoSend(0)
    End Sub

    Private Sub button9_Click(sender As Object, e As EventArgs) Handles button9.Click
        ButtoSend(1)
    End Sub

    Private Sub button3_Click(sender As Object, e As EventArgs) Handles button3.Click
        Dim copstr As String
        Dim I As Long
        Clipboard.Clear()
        copstr = ""
        For I = 0 To listBox2.Items.Count - 1
            copstr = copstr & listBox2.Items(I)
            copstr = copstr & vbCrLf
        Next
        Clipboard.SetText(copstr)
        MsgBox("TCP通讯日志报文已拷贝!", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "提示")
    End Sub

    Private Sub button2_Click(sender As Object, e As EventArgs) Handles button2.Click
        listBox2.Items.Clear()
    End Sub
End Class

 


http://www.niftyadmin.cn/n/5177856.html

相关文章

Android图形系统之X11、Weston、Wayland、Mesa3D、ANGLE、SwiftShader介绍(十五)

简介&#xff1a; CSDN博客专家&#xff0c;专注Android/Linux系统&#xff0c;分享多mic语音方案、音视频、编解码等技术&#xff0c;与大家一起成长&#xff01; 优质专栏&#xff1a;Audio工程师进阶系列【原创干货持续更新中……】&#x1f680; 人生格言&#xff1a; 人生…

(论文阅读29/100 人体姿态估计)

29.文献阅读笔记 简介 题目 DeepCut: Joint Subset Partition and Labeling for Multi Person Pose Estimation 作者 Leonid Pishchulin, Eldar Insafutdinov, Siyu Tang, Bjoern Andres, Mykhaylo Andriluka, Peter Gehler, and Bernt Schiele, CVPR, 2016. 原文链接 h…

点大商城V2版 2.5.3全插件开源独立版 百度+支付宝+QQ+头条+小程序端+unipp开源端安装测试教程

点大商城V2是一款采用全新界面设计支持多端覆盖的小程序应用&#xff0c;支持H5、微信公众号、微信小程序、头条小程序、支付宝小程序、百度小程序&#xff0c;本程序是点大商城V2独立版&#xff0c;包含全部插件&#xff0c;代码全开源&#xff0c;并且有VUE全端代码。 适用范…

Maya 2024 for Mac(3D建模软件)

Maya 2024是一款三维计算机图形软件&#xff0c;具有强大的建模、动画、渲染、特效等功能&#xff0c;广泛应用于影视、游戏、广告等行业。以下是Maya 2024软件的主要功能介绍&#xff1a; 建模&#xff1a;Maya 2024具有强大的建模工具&#xff0c;包括多边形建模、曲面建模、…

Python--集合----无序,去重,空集合只能用set()方法

集合&#xff08;set&#xff09;是一个无序的不重复元素序列。 特点&#xff1a;天生去重 无序 集合定义&#xff1a;在Python中&#xff0c;我们可以使用一对花括号 {} 或者 set()方法 来定义集合&#xff0c; 但是如果你 定义的集合是一个 空集合&#xf…

GB28181/GB35114国标平台LiveGBS适配国产信创环境,使用国产数据库达梦数据库、高斯数据库、瀚高数据库的配置方法...

1、如何配置切换信创达梦数据库&#xff1f; livecms.ini -> [db]下面添加配置如&#xff1a; ... [db] dialectdm url dm://SYSDBA:Aa12345678localhost:5236/livegbs 2、如何配置切换高斯数据库&#xff1f; livecms.ini -> [db]下面添加配置如&#xff1a; ... [db] d…

OpenCV 矩阵元素的数据类型

引言 在以下两个场景中使用 OpenCV 时&#xff0c;我们必须事先知道矩阵元素的数据类型&#xff1a; 使用 at 方法访问数据元素的时候要指明数据类型做数值运算的时候&#xff0c;比如究竟是整数除法还是浮点数除法。 但面对一大堆代码&#xff0c;我们有时并不清楚当前的矩…

结合大模型进行降本增效之——自动化测试

软件测试中&#xff0c;有哪些步骤能结合大模型的AIGC和数据分析能力&#xff1f; 生成测试用例 利用GPT-3.5 Turbo的自然语言生成能力&#xff0c;让它根据需求自动生成测试用例。例如&#xff0c;你可以向GPT-3.5 Turbo提供关于某个功能或者页面的描述&#xff0c;然后让它生…