📄 form1.vb
字号:
Public Class Form1
Private Declare Sub Sleep Lib "kernel32.dll" (ByVal tm As Integer)
Private Sub ClearDevices()
listView_Dev.Items.Clear()
End Sub
Private Function GetDevType(ByVal devtype As Byte) As String
Dim strtype As String
Dim devarr(20) As Byte
Dim namearr(20) As String
Dim i As Integer
devarr(0) = 1
devarr(1) = 2
devarr(2) = 3
devarr(3) = 4
devarr(4) = 5
devarr(5) = 6
devarr(6) = 7
devarr(7) = 8
devarr(8) = 11
devarr(9) = 12
devarr(10) = 13
devarr(11) = 48
namearr(0) = "ZNE-100"
namearr(1) = "ZNE-100T"
namearr(2) = "ZNE-10"
namearr(3) = "ZNE-200T"
namearr(4) = "NETCOM-10"
namearr(5) = "ZNE-200"
namearr(6) = "ZNE-10T"
namearr(7) = "ZNE-100TI"
namearr(8) = "CANET-E"
namearr(9) = "CANET-100"
namearr(10) = "CANET-200"
namearr(11) = "NETCOM-400"
strtype = System.Convert.ToString(devtype)
For i = 0 To 11
If devarr(i) = devtype Then
strtype = strtype + "(" + namearr(i) + ")"
End If
Next
GetDevType = strtype
End Function
Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
ClearDevices()
ZN_SearchAll()
Dim times As Long
Dim szip(20) As Byte, szver(20) As Byte, szmac(30) As Byte
Dim devtype, ipmode As Byte
Dim tcpport As Long
Dim item As ListViewItem
For times = 0 To 200
While ZN_GetSearchDev(szip(0), szver(0), szmac(0), devtype, ipmode, tcpport) = 1
item = listView_Dev.Items.Add(System.Text.Encoding.ASCII.GetString(szip))
item.SubItems.Add(GetDevType(devtype))
item.SubItems.Add(System.Text.Encoding.ASCII.GetString(szmac))
If (devtype >= 48) Then
item.SubItems.Add(System.Convert.ToString(tcpport))
Else
item.SubItems.Add("")
End If
End While
Sleep(10)
Next
End Sub
Private Function TryGetSelDev() As Integer
If listView_Dev.SelectedItems.Count > 0 Then
TryGetSelDev = listView_Dev.SelectedItems(0).Index
Exit Function
End If
TryGetSelDev = -1
End Function
Private Function GetInfo_MultiCOM(ByVal devtype As Byte, ByVal szip As String, ByVal port As Integer) As Integer
Dim dlg As Form2
dlg = New Form2
dlg.ShowDialog()
If dlg.isOK() = 0 Then
GetInfo_MultiCOM = 0
Exit Function
End If
Dim szpwd As String
szpwd = dlg.GetPwd()
If ZN_LoginTCP(devtype, szip, port, szpwd) <> 1 Then
GetInfo_MultiCOM = 0
Exit Function
End If
Dim res As Integer
res = 1
Dim szval(100) As Byte
res = ZN_GetDevConfigTCP("NAME", szval(0))
If res <> 1 Then
GoTo ext
End If
textBox_Name.Text = System.Text.Encoding.ASCII.GetString(szval)
res = ZN_GetDevConfigTCP("C1_OP", szval(0))
If res <> 1 Then
GoTo ext
End If
textBox_Worktype.Text = System.Text.Encoding.ASCII.GetString(szval)
res = ZN_GetDevConfigTCP("C1_PORT", szval(0))
If res <> 1 Then
GoTo ext
End If
textBox_Workport.Text = System.Text.Encoding.ASCII.GetString(szval)
res = ZN_GetDevConfigTCP("C1_BAUD", szval(0))
If res <> 1 Then
GoTo ext
End If
textBox_Baud.Text = System.Text.Encoding.ASCII.GetString(szval)
res = ZN_GetDevConfigTCP("C1_DATAB", szval(0))
If res <> 1 Then
GoTo ext
End If
textBox_DataB.Text = System.Text.Encoding.ASCII.GetString(szval)
res = ZN_GetDevConfigTCP("C1_STOPB", szval(0))
If res <> 1 Then
GoTo ext
End If
textBox_StopB.Text = System.Text.Encoding.ASCII.GetString(szval)
res = ZN_GetDevConfigTCP("C1_PARITY", szval(0))
If res <> 1 Then
GoTo ext
End If
textBox_Parity.Text = System.Text.Encoding.ASCII.GetString(szval)
ext:
ZN_ExitTCP()
GetInfo_MultiCOM = res
End Function
Private Function GetInfo_SingleCOM(ByVal devtype As Byte, ByVal szip As String)
Dim res As Integer
res = ZN_GetDevInfoUDPbyIP(szip, devtype)
Dim szval(100) As Byte
If res <> 1 Then
GetInfo_SingleCOM = 0
Exit Function
End If
res = ZN_GetDevConfigUDP("NAME", szval(0))
If res = 1 Then
textBox_Name.Text = System.Text.Encoding.ASCII.GetString(szval)
End If
res = ZN_GetDevConfigUDP("C1_OP", szval(0))
If res = 1 Then
textBox_Worktype.Text = System.Text.Encoding.ASCII.GetString(szval)
End If
res = ZN_GetDevConfigUDP("C1_PORT", szval(0))
If res = 1 Then
textBox_Workport.Text = System.Text.Encoding.ASCII.GetString(szval)
End If
res = ZN_GetDevConfigUDP("C1_BAUD", szval(0))
If res = 1 Then
textBox_Baud.Text = System.Text.Encoding.ASCII.GetString(szval)
End If
res = ZN_GetDevConfigUDP("C1_DATAB", szval(0))
If res = 1 Then
textBox_DataB.Text = System.Text.Encoding.ASCII.GetString(szval)
End If
res = ZN_GetDevConfigUDP("C1_STOPB", szval(0))
If res = 1 Then
textBox_StopB.Text = System.Text.Encoding.ASCII.GetString(szval)
End If
res = ZN_GetDevConfigUDP("C1_PARITY", szval(0))
If res = 1 Then
textBox_Parity.Text = System.Text.Encoding.ASCII.GetString(szval)
End If
GetInfo_SingleCOM = res
End Function
Private Sub button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button2.Click
Dim selind As Integer
selind = TryGetSelDev()
If selind = -1 Then
Exit Sub
End If
Dim szip As String, szmac As String
Dim str1 As String
Dim devtype As Byte
Dim tcpport As Integer
szip = listView_Dev.Items(selind).Text
str1 = listView_Dev.Items(selind).SubItems(1).Text
If str1.IndexOf("(") <> -1 Then
str1 = str1.Substring(0, str1.IndexOf("("))
End If
devtype = System.Convert.ToByte(str1)
str1 = listView_Dev.Items(selind).SubItems(3).Text
If str1 <> "" Then
tcpport = Val(str1)
End If
Dim res As Integer
If devtype >= 48 Then
res = GetInfo_MultiCOM(devtype, szip, tcpport)
Else
res = GetInfo_SingleCOM(devtype, szip)
End If
If res <> 1 Then
MsgBox("获取信息失败")
Else
MsgBox("获取信息成功")
End If
End Sub
Private Function Modify_MultiCOM(ByVal devtype As Byte, ByVal szip As String, ByVal port As Integer) As Integer
Dim dlg As Form2
dlg = New Form2
dlg.ShowDialog()
If dlg.isOK() = 0 Then
Modify_MultiCOM = 0
Exit Function
End If
Dim szpwd As String
szpwd = dlg.GetPwd()
If ZN_LoginTCP(devtype, szip, port, szpwd) <> 1 Then
Modify_MultiCOM = 0
Exit Function
End If
Dim res As Integer
res = 1
Dim szval As String
res = ZN_ModifyDevConfigTCP("NAME", textBox_Name.Text)
If res <> 1 Then
GoTo ext
End If
res = ZN_ModifyDevConfigTCP("C1_OP", textBox_Worktype.Text)
If res <> 1 Then
GoTo ext
End If
res = ZN_ModifyDevConfigTCP("C1_PORT", textBox_Workport.Text)
If res <> 1 Then
GoTo ext
End If
res = ZN_ModifyDevConfigTCP("C1_BAUD", textBox_Baud.Text)
If res <> 1 Then
GoTo ext
End If
res = ZN_ModifyDevConfigTCP("C1_DATAB", textBox_DataB.Text)
If res <> 1 Then
GoTo ext
End If
res = ZN_ModifyDevConfigTCP("C1_STOPB", textBox_StopB.Text)
If res <> 1 Then
GoTo ext
End If
res = ZN_ModifyDevConfigTCP("C1_PARITY", textBox_Parity.Text)
If res <> 1 Then
GoTo ext
End If
ext:
ZN_ResetDevTCP()
ZN_ExitTCP()
Modify_MultiCOM = res
End Function
Private Function Modify_SingleCOM(ByVal devtype As Byte, ByVal szip As String) As Integer
Dim dlg As Form2
dlg = New Form2
dlg.ShowDialog()
If dlg.isOK() = 0 Then
Modify_SingleCOM = 0
Exit Function
End If
Dim szpwd As String
szpwd = dlg.GetPwd()
Dim res As Integer
Dim szval As String
ZN_ResetModifyConfigUDP()
res = ZN_SetModifyConfigUDP("NAME", textBox_Name.Text)
res = ZN_SetModifyConfigUDP("C1_OP", textBox_Worktype.Text)
res = ZN_SetModifyConfigUDP("C1_PORT", textBox_Workport.Text)
res = ZN_SetModifyConfigUDP("C1_BAUD", textBox_Baud.Text)
res = ZN_SetModifyConfigUDP("C1_DATAB", textBox_DataB.Text)
res = ZN_SetModifyConfigUDP("C1_STOPB", textBox_StopB.Text)
res = ZN_SetModifyConfigUDP("C1_PARITY", textBox_Parity.Text)
res = ZN_ModifyDevUDPbyIP(szip, szpwd, devtype)
If res <> 1 Then
Modify_SingleCOM = 0
Exit Function
End If
Sleep(500)
res = ZN_GetDevInfoUDPbyIP(szip, devtype)
If res <> 1 Then
Modify_SingleCOM = 0
Exit Function
End If
Dim szvalnew(100) As Byte
Dim tmp As String
If ZN_GetDevConfigUDP("NAME", szvalnew(0)) = 1 Then
If System.String.Compare(textBox_Name.Text, System.Text.Encoding.ASCII.GetString(szvalnew), StringComparison.Ordinal) = False Then
Modify_SingleCOM = 0
Exit Function
End If
End If
If ZN_GetDevConfigUDP("C1_OP", szvalnew(0)) = 1 Then
If System.String.Compare(textBox_Worktype.Text, System.Text.Encoding.ASCII.GetString(szvalnew), StringComparison.Ordinal) = False Then
Modify_SingleCOM = 0
Exit Function
End If
End If
If ZN_GetDevConfigUDP("C1_PORT", szvalnew(0)) = 1 Then
If System.String.Compare(textBox_Workport.Text, System.Text.Encoding.ASCII.GetString(szvalnew), StringComparison.Ordinal) = False Then
Modify_SingleCOM = 0
Exit Function
End If
End If
If ZN_GetDevConfigUDP("C1_BAUD", szvalnew(0)) = 1 Then
If System.String.Compare(textBox_Baud.Text, System.Text.Encoding.ASCII.GetString(szvalnew), StringComparison.Ordinal) = False Then
Modify_SingleCOM = 0
Exit Function
End If
End If
If ZN_GetDevConfigUDP("C1_DATAB", szvalnew(0)) = 1 Then
If System.String.Compare(textBox_DataB.Text, System.Text.Encoding.ASCII.GetString(szvalnew), StringComparison.Ordinal) = False Then
Modify_SingleCOM = 0
Exit Function
End If
End If
If ZN_GetDevConfigUDP("C1_STOPB", szvalnew(0)) = 1 Then
If System.String.Compare(textBox_StopB.Text, System.Text.Encoding.ASCII.GetString(szvalnew), StringComparison.Ordinal) = False Then
Modify_SingleCOM = 0
Exit Function
End If
End If
If ZN_GetDevConfigUDP("C1_PARITY", szvalnew(0)) = 1 Then
If System.String.Compare(textBox_Parity.Text, System.Text.Encoding.ASCII.GetString(szvalnew), StringComparison.Ordinal) = False Then
Modify_SingleCOM = 0
Exit Function
End If
End If
Modify_SingleCOM = 1
End Function
Private Sub button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button3.Click
Dim selind As Integer
selind = TryGetSelDev()
If selind = -1 Then
Exit Sub
End If
Dim szip As String, szmac As String
Dim str1 As String
Dim devtype As Byte
Dim tcpport As Integer
szip = listView_Dev.Items(selind).Text
str1 = listView_Dev.Items(selind).SubItems(1).Text
If str1.IndexOf("(") <> -1 Then
str1 = str1.Substring(0, str1.IndexOf("("))
End If
devtype = System.Convert.ToByte(str1)
str1 = listView_Dev.Items(selind).SubItems(3).Text
If str1 <> "" Then
tcpport = Val(str1)
End If
Dim res As Integer
If devtype >= 48 Then
res = Modify_MultiCOM(devtype, szip, tcpport)
Else
res = Modify_SingleCOM(devtype, szip)
End If
If res <> 1 Then
MsgBox("更改信息失败")
Else
MsgBox("更改信息成功")
End If
End Sub
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -