广州荣士电子_18002295132头像
VB6端口侦听HTTP协议读卡器提交的GET、POST请求,解析Json获取卡号、设备编号等参数封面图

VB6端口侦听HTTP协议读卡器提交的GET、POST请求,解析Json获取卡号、设备编号等参数

本示例使用的设备:https://item.taobao.com/item.htm?ft=t&id=1053367166610

Const BUSY As Boolean = False    '定义常量
Const FREE As Boolean = True
Dim ConnectState() As Boolean   '定义连接状态
Dim SockIndex As Long

Private Sub Command1_Click()
initsock
End Sub

Private Sub RefreshDisp(ByVal dispstr As String)
If Len(Text16.Text) > 10000 Then Text16.Text = ""
Text16.Text = Text16.Text & dispstr & vbCrLf
Text16.SelStart = Len(Text16)
End Sub

Private Sub Command31_Click()
Text16.Text = ""
End Sub

Private Sub Form_Load()
Dim i As Integer
Combo1.ListIndex = 0
Combo2.ListIndex = 2
Combo4.ListIndex = 10
For i = 0 To 255
    Combo3.AddItem (Trim(str(i)))
Next
Combo3.ListIndex = 20

Command1_Click
End Sub

Private Sub Listener_Close()
Dim dispinf As String
dispinf = getdatetime() & " 监听服务已关闭!"
RefreshDisp dispinf
End Sub

Private Sub Listener_ConnectionRequest(ByVal requestID As Long)
Dim SockIndex As Integer
Dim SockNum As Integer
Dim dispinf As String

On Error Resume Next

SockNum = UBound(ConnectState)   '查找连接的用户数
If SockNum > 100 Then
    dispinf = dispinf & SockIndex & " ,当前连接数>100,系统不接受新连接!"
    RefreshDisp dispinf
    Exit Sub
End If

SockIndex = FindFreeSocket()   '查找空闲的sock
If SockIndex > SockNum Then    '如果已有的sock都忙,而且sock数不超过15个,动态添加sock
    Load Sock(SockIndex)
End If

ConnectState(SockIndex) = BUSY
Sock(SockIndex).LocalPort = 0
Sock(SockIndex).Tag = SockIndex
Sock(SockIndex).Accept (requestID)  '接受请求

dispinf = getdatetime() & " 接收到 " & requestID & " 的连接请求,请求IP:" & Listener.RemoteHostIP & ":" & Listener.RemotePort
RefreshDisp dispinf
End Sub

Private Sub Listener_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
Dim dispinf As String
dispinf = getdatetime() & " 监听到异常错误!"
RefreshDisp dispinf
End Sub

Private Sub Sock_Close(Index As Integer)
Dim dispinf As String
Dim i As Integer

If Sock(Index).State <> sckClosed Then
    Sock(Index).Close
End If
ConnectState(Index) = FREE

dispinf = getdatetime() & " 连接 " & Index & " 已关闭"
RefreshDisp dispinf

For i = 0 To List1.ListCount - 1
    If Val(Mid(List1.List(i), 1, 2)) = Index Then
        List1.RemoveItem i
        Exit For
    End If
Next
End Sub

Private Sub Sock_DataArrival(Index As Integer, ByVal bytesTotal As Long)
Dim Respond As String
Dim i, j, m As Integer
Dim TcpData() As Byte
Dim IPort As String
Dim RequestStr As String

Dim info As String
Dim jihao As String
Dim cardtype As String
Dim scantype As String
Dim heartbeattype As String
Dim card As String
Dim data As String
Dim inputi As String
Dim output As String
Dim dn As String
Dim rand As String
Dim status As String

Sock(Index).GetData TcpData
IPort = Sock(Index).RemoteHostIP & ":" & Sock(Index).RemotePort

RequestStr = BytesToStringANSI(TcpData)
RefreshDisp RequestStr

If InStr(1, RequestStr, "application/json", vbBinaryCompare) > 0 Then
    RequestStr = GetStrBetween(RequestStr, "{", "}")
    RequestStr = Replace(RequestStr, """:""", "=")
    RequestStr = Replace(RequestStr, """,""", "&")
    RequestStr = Replace(RequestStr, """", "")
ElseIf InStr(1, RequestStr, "application/x-www-form-urlencoded", vbBinaryCompare) > 0 Then
    RequestStr = GetStrAfter(RequestStr, "info")
Else
    RequestStr = GetStrBetween(RequestStr, "?", " ")
End If

info = ParseQueryString(RequestStr, "info")                   '数据包帧号,回应时必须对应,否则设备不理会
jihao = ParseQueryString(RequestStr, "jihao")                 '设备机号(可自编),以上是必选参数,以下是可选参数
cardtype = ParseQueryString(RequestStr, "cardtype")           '刷卡标志,卡类型
scantype = ParseQueryString(RequestStr, "scantype")           '扫码标志
heartbeattype = ParseQueryString(RequestStr, "heartbeattype") '心跳包标志
card = ParseQueryString(RequestStr, "card")                   '卡号,16进制
data = ParseQueryString(RequestStr, "data")                   'IC卡扇区内容 或 扫码信息
inputi = ParseQueryString(RequestStr, "input")                '输入标识
output = ParseQueryString(RequestStr, "output")               '输出标识
dn = ParseQueryString(RequestStr, "dn")                       '设备唯一硬件序号
rand = ParseQueryString(RequestStr, "rand")                   '设备随机数
status = ParseQueryString(RequestStr, "status")               '读卡状态,如12表示卡密码认证失败

If Check1.Value > 0 Then
    If Combo1.ListIndex = 0 Then
        Respond = "{""Response"":""json"",""infotype"":""1"",""info"":""" & info & """"
        If Check3.Value > 0 Then Respond = Respond & ",""disp"":""" & getChinesecode(Text7.Text) & """,""dispdelay"":""" & Combo3.Text & """"
        If Check2.Value > 0 Then Respond = Respond & ",""beeptype"":""" & Combo2.ListIndex & """"
        If Check4.Value > 0 Then Respond = Respond & ",""voicetype"":""" & "[v" & Combo4.ListIndex & "]" & getChinesecode(Text3.Text) & """"
        If Check5.Value > 0 Then Respond = Respond & ",""k1delay"":""" & switch1.Text & """"
        If Check6.Value > 0 Then Respond = Respond & ",""k2delay"":""" & switch2.Text & """"
        Respond = Respond & "}"
    Else
        Respond = "Response=1," & info
        If Check3.Value > 0 Then Respond = Respond & "," & getChinesecode(Text7.Text) & "," & Combo3.Text Else Respond = Respond & ",,"
        If Check2.Value > 0 Then Respond = Respond & "," & Combo2.ListIndex Else Respond = Respond & ","
        If Check4.Value > 0 Then Respond = Respond & "," & "[v" & Combo4.ListIndex & "]" & getChinesecode(Text3.Text) Else: Respond = Respond & ","
        If Check5.Value > 0 Then Respond = Respond & "," & switch1.Text Else: Respond = Respond & ","
        If Check6.Value > 0 Then Respond = Respond & "," & switch2.Text Else: Respond = Respond
    End If
    
    Sock(Index).SendData Respond
    
    RefreshDisp Respond & vbCrLf

End If

SockIndex = Index
    
Timer1.Enabled = True

End Sub

Public Function FindFreeSocket()   '寻找空闲的sock
Dim SockCount, i As Integer
SockCount = UBound(ConnectState)
For i = 0 To SockCount
    If ConnectState(i) = FREE Then
        FindFreeSocket = i
        Exit Function
    End If
Next i

ReDim Preserve ConnectState(0 To SockCount + 1)
FindFreeSocket = UBound(ConnectState)
End Function

Private Sub initsock()
Dim dispinf As String
ReDim Preserve ConnectState(0 To 1)
On Error GoTo err1

ConnectState(0) = FREE
ConnectState(1) = FREE

If Listener.State = sckClosed Then
    Listener.LocalPort = CLng(Text1.Text)    '指定网络端口号
    Listener.Listen                          '开始侦听
    
    dispinf = getdatetime() & " 已创建监听服务!可以接收客户端的连接请求。"
    RefreshDisp dispinf
    Command1.Caption = "关闭监听服务"
Else
    Listener.Close
    
    dispinf = getdatetime() & " 已关闭监听服务!不再接受新的客户端连接请求。"
    RefreshDisp dispinf
    Command1.Caption = "创建监听服务"
End If

Exit Sub

err1:
    dispinf = getdatetime() & " 开启监听服务时出现错误:" & err.Number & err.Description
    RefreshDisp dispinf
End Sub

Private Sub Sock_Error(Index As Integer, ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
Dim dispinf As String
Dim i As Integer

If Sock(Index).State <> sckClosed Then
    Sock(Index).Close
End If
ConnectState(Index) = FREE

dispinf = getdatetime() & " 连接 " & Index & " 错误提示:" & Description
RefreshDisp dispinf

For i = 0 To List1.ListCount - 1
    If Val(Mid(List1.List(i), 1, 2)) = Index Then
        List1.RemoveItem i
        Exit For
    End If
Next
End Sub

Public Function BytesToStringANSI(b() As Byte) As String
If UBound(b) < LBound(b) Then
    BytesToStringANSI = ""
    Exit Function
End If
BytesToStringANSI = StrConv(b, vbUnicode)
End Function

Private Sub Timer1_Timer()
ConnectState(SockIndex) = FREE
Sock(SockIndex).Close
Timer1.Enabled = False
End Sub

Function getChinesecode(ByVal inputstr As String) As String
Dim b() As Byte
Dim i As Integer
Dim str As String
getChinesecode = ""

For i = 1 To Len(inputstr)
    str = Mid(inputstr, i, 1)
    b = StrConv(str, vbFromUnicode)      '字符串转换为字节数组
    lensy = UBound(b) + 1
    If lensy = 1 Then
        getChinesecode = getChinesecode + str
    Else
        getChinesecode = getChinesecode + "\x" + Right("00" + Hex(b(0)), 2) + Right("00" + Hex(b(1)), 2)
    End If
Next
End Function

转载自 CSDN-专业IT技术社区

原文链接:https://blog.csdn.net/zhangjin7422/article/details/171349590

文章来源转载

评论

评论列表

微信小程序
QQ小程序

关于作者

点赞数:0
关注数:0
粉丝:0
文章:0
关注标签:0
加入于:--