📄 rfideqctrl.bas
字号:
Attribute VB_Name = "RFIDEQCtrl"
Option Explicit
'*************************************************************************
'检查通讯方式和通讯参数是否合法
'*************************************************************************
Public Function CommIsLegal(CommType As Integer, CommParam As String, ByRef ErrorDesc As String) As Boolean
If CommType = 0 Then
If CommParamTCPIPIsLegal(CommParam) Then
CommIsLegal = True
Else
ErrorDesc = "TCP/IP通讯参数不合法"
CommIsLegal = False
End If
ElseIf CommType = 1 Then
If CommParamCOMIsLegal(CommParam) Then
CommIsLegal = True
Else
ErrorDesc = "COM口通讯参数不合法"
CommIsLegal = False
End If
Else
ErrorDesc = "通讯方式错误"
CommIsLegal = False
End If
End Function
'*************************************************************************
'检查TCP/IP通讯参数是否合法,应该是"192.9.100.1;255.255.255.0;192.9.100.2"的样式
'*************************************************************************
Public Function CommParamTCPIPIsLegal(CommParam As String) As Boolean
On Error GoTo ErrDeal
Dim f1 As Integer
Dim f2 As Integer
Dim ip As String
Dim mask As String
Dim gateway As String
f1 = InStr(1, CommParam, ";")
f2 = InStr(f1 + 1, CommParam, ";")
ip = Left(CommParam, f1 - 1)
mask = Mid(CommParam, f1 + 1, f2 - f1 - 1)
gateway = Right(CommParam, Len(CommParam) - f2)
If Not (IsIP(ip) And IsIP(mask) And IsIP(gateway)) Then
GoTo ErrDeal
End If
CommParamTCPIPIsLegal = True
Exit Function
ErrDeal:
CommParamTCPIPIsLegal = False
End Function
'*************************************************************************
'检查COM口通讯参数是否合法,应该是"COM1;9600,n,8,1"的样式
'*************************************************************************
Public Function CommParamCOMIsLegal(CommParam As String) As Boolean
Dim semicolon As Integer
Dim str_len As Integer
str_len = Len(CommParam)
Dim Index As Integer
If UCase(Mid(CommParam, 1, 3)) <> "COM" Then
GoTo ErrDeal
End If
'找到“;”的位置
For Index = 1 To str_len
If Mid(CommParam, Index, 1) = ";" Then
semicolon = Index
Exit For
End If
Next
'如果“;”没有或者在首尾则非法
If semicolon = 0 Or semicolon = 1 Or semicolon = str_len Then
GoTo ErrDeal
End If
'判断COM口的号码的字符是否都是数字
Dim port_str As String
port_str = Mid(CommParam, 4, semicolon - 4)
If (IsFigure(port_str)) Then
Else
GoTo ErrDeal
End If
'判断“;”之后的字符串是否与“9600,n,8,1”相同
If Not Mid(CommParam, semicolon + 1, str_len - semicolon) = "9600,n,8,1" Then
GoTo ErrDeal
End If
CommParamCOMIsLegal = True
Exit Function
ErrDeal:
CommParamCOMIsLegal = False
End Function
'*************************************************************************
'检查ID是否合法
'*************************************************************************
Public Function IDIslegal(IDs As String, ErrorDesc As String) As Boolean
'判断IDs的长度是否16,否的话IDs不合法
If Len(IDs) <> 16 Then
GoTo ErrDeal
End If
'判断IDs里面的字符是否都是0-9或A-F或a-f,否的话则为非法
Dim temp As String
Dim Index As Integer
Dim CharIsLegal As Boolean
For Index = 1 To 16
temp = Mid(IDs, Index, 1)
CharIsLegal = (temp >= "0" And temp <= "9") _
Or (temp >= "A" And temp <= "F") _
Or (temp >= "a" And temp <= "f")
If Not CharIsLegal Then
GoTo ErrDeal
End If
Next
IDIslegal = True
Exit Function
ErrDeal:
ErrorDesc = "ID不合法"
IDIslegal = False
End Function
'*************************************************************************
'判断字符串中的字符是否全是数字
'*************************************************************************
Public Function IsFigure(str As String) As Boolean
IsFigure = True
Dim temp As String
Dim Index As Integer
For Index = 1 To Len(str)
temp = Mid(str, Index, 1)
If temp < "0" Or temp > "9" Then
IsFigure = False
Exit For
End If
Next
End Function
'*************************************************************************
'判断字符串中的字符是否类似于“192.168.0.1”的IP地址
'*************************************************************************
Public Function IsIP(str As String) As Boolean
Dim dot1 As Integer
Dim dot2 As Integer
Dim dot3 As Integer
Dim str_len As Integer
str_len = Len(str)
Dim Index As Integer
'找到3个“.”的位置
For Index = 1 To str_len
If Mid(str, Index, 1) = "." Then
dot1 = Index
Exit For
End If
Next
If dot1 = 0 Or dot1 >= str_len Then
GoTo ErrDeal
End If
For Index = dot1 + 1 To str_len
If Mid(str, Index, 1) = "." Then
dot2 = Index
Exit For
End If
Next
If dot2 = 0 Or dot2 >= str_len Then
GoTo ErrDeal
End If
For Index = dot2 + 1 To str_len
If Mid(str, Index, 1) = "." Then
dot3 = Index
Exit For
End If
Next
If dot3 = 0 Or dot3 >= str_len Then
GoTo ErrDeal
End If
Dim ip_str_1 As String
Dim ip_str_2 As String
Dim ip_str_3 As String
Dim ip_str_4 As String
Dim ip_int_1 As String
Dim ip_int_2 As String
Dim ip_int_3 As String
Dim ip_int_4 As String
Dim port_int As String
ip_str_1 = Mid(str, 1, dot1 - 1)
ip_str_2 = Mid(str, dot1 + 1, dot2 - dot1 - 1)
ip_str_3 = Mid(str, dot2 + 1, dot3 - dot2 - 1)
ip_str_4 = Mid(str, dot3 + 1, str_len - dot3)
'判断ip地址和端口号的字符是否全为数字
If (IsFigure(ip_str_1) And IsFigure(ip_str_2) And IsFigure(ip_str_3) And IsFigure(ip_str_4)) Then
ip_int_1 = ip_str_1
ip_int_2 = ip_str_2
ip_int_3 = ip_str_3
ip_int_4 = ip_str_4
Else
GoTo ErrDeal
End If
'判断ip地址和端口号是否符合要求
If Not (ip_int_1 < 256 And ip_int_2 < 256 And ip_int_3 < 256 And ip_int_4 < 256) Then
GoTo ErrDeal
End If
IsIP = True
Exit Function
ErrDeal:
IsIP = False
End Function
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -