Private Sub Command1_Click()
Winsock1.Close
Winsock1.Connect Text1, 502
Label2.Caption = "Version: 1.0 : " + Format(Now, "mmm dd yyyy Hh:Nn:Ss")
Command1.Enabled = False
Command1.Enabled = False
Text1.Enabled = False
End Sub
(2)、Y0的ON/OFF控制
Private Sub Comd_send_Click()
On Error GoTo ErrProc
Dim sendstr(14) As Byte
sendstr(0) = &H0 '交换识别号高字节,通常为0
sendstr(1) = &H0 '交换识别号低字节,通常为0
sendstr(2) = &H0 '协议识别号高字节,为 0
sendstr(3) = &H0 '协议识别号低字节,为 0
sendstr(4) = &H0 '字节长度高字节
sendstr(5) = &H9 '以下字节长度低字节
sendstr(6) = &HFF '单元识别号,确省为 255
sendstr(7) = &H5 '写一个线圈命令代码
sendstr(8) = &H0 '写线圈的起始地址高字节
sendstr(9) = &H1'写线圈的起始地址低字节
sendstr(10) = a '=FF打开线圈,=00关闭线圈
sendstr(11) = &H0 '
Dim aStr As String
Dim i, j As Integer
For i = 0 To 11
aStr = aStr & Chr(sendstr(i))
Next
Winsock1.SendData aStr
Exit Sub
ErrProc:
MsgBox "传输数据失败", vbCritical, "网络传输"
End Sub
(3)线圈状态显示及寄存器读出数据显示
Private Sub winsock1_DataArrival(ByVal bytesTotal As Long)
Dim strdata() As Byte
Dim i, j As Integer
Dim s As String
Dim s1 As String
Dim IO As Byte
i = Winsock1.BytesReceived
ReDim strdata(i)
Winsock1.GetData strdata, vbByte, i
For j = 0 To i - 1
s = s + Hex(strdata(j))
Next
If s Like "*5*" Then
s1 = Mid(s, 12, 2)
If s1 = "FF" Then
IO = 1
a = &H0
Else
IO = 0
a = &HFF
End If
If IO = 1 Then
Shape1.FillColor = RGB(0, 255, 0) 'green
Labl3.Caption = "IO点接通"
Else
Shape1.FillColor = RGB(255, 0, 0) 'red
Labl3.Caption = "IO点断开"
End If
End If If s Like "*3*" Then
Text3.Text = Text3.Text + Mid(s, 11) + ""
End If
(4)、读多个寄存器数据
End Sub Private Sub Command4_Click()
On Error GoTo ErrProc
Dim str(12) As Byte
str(0) = &H0 '交换识别号高字节,通常为 0
str(1) = &H0 '交换识别号低字节,通常为 0
str(2) = &H0 '协议识别号高字节,为 0
str(3) = &H0 '协议识别号低字节,为 0
str(4) = &H0 '字节长度高字节
str(5) = &H6 '以下字节长度低字节
str(6) = &HFF '单元识别号,确省为 255
str(7) = &H3 '读多个寄存器命令代码
str(8) = &H10 '读数据的起始地址高字节
str(9) = &HC8 '读数据的起始地址低字节
str(10) = &H0 '数据长度高字节
str(11) = &H1 '数据长度低字节
Dim bStr As String
Dim i As Integer
For i = 0 To 11
bStr = bStr & Chr(str(i))
Next
Winsock1.SendData bStr
Exit Sub
ErrProc:
MsgBox "传输数据失败", vbCritical, "网络传输"
End Sub