📄 form1.frm
字号:
VERSION 5.00
Object = "{248DD890-BB45-11CF-9ABC-0080C7E7B78D}#1.0#0"; "MSWINSCK.OCX"
Begin VB.Form Form1
Caption = "Form1"
ClientHeight = 3090
ClientLeft = 60
ClientTop = 450
ClientWidth = 4680
LinkTopic = "Form1"
ScaleHeight = 3090
ScaleWidth = 4680
StartUpPosition = 3 '窗口缺省
Begin VB.CommandButton readbutton
Caption = "Command1"
Height = 495
Left = 1440
TabIndex = 1
Top = 1800
Width = 1215
End
Begin VB.CommandButton connectbutton
Caption = "Command1"
Height = 495
Left = 2640
TabIndex = 0
Top = 840
Width = 975
End
Begin MSWinsockLib.Winsock Winsock
Left = 960
Top = 840
_ExtentX = 741
_ExtentY = 741
_Version = 393216
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Sub Form_Load()
aflag = True
bflag = True
outbuf(0) = &H0
outbuf(1) = &H0
outbuf(2) = &H0
outbuf(3) = &H0
outbuf(4) = &H0
outbuf(5) = &H6
outbuf(6) = &H0
outbuf(7) = &H1 'Op-Code
outbuf(8) = &H0 'Start-Hi
outbuf(9) = &H0 'Start-Lo
outbuf(10) = &H0 'Count-Hi
outbuf(11) = &H1 'Count-Lo
End Sub
'(2) 连接PLC
Private Sub connectbutton_Click()
Winsock.Close
Winsock.LocalPort = 0
Winsock.Connect
End Sub
'(3) 发送数据
Private Sub readbutton_Click()
Winsock.SendData outbuf
End Sub
'(4) 提取数据
Private Sub Winsock_DataArrival(ByVal bytesTotal As Long)
Dim inBuf(20) As Byte
For i = 0 To bytesTotal - 1
Winsock.GetData inBuf(i)
Next
If coil.Value = True Then
Value.Caption = Str(inBuf(9))
' // 提取0xxxxx中的数据
Else
Value.Caption = Str(inBuf(9) * 256 + inBuf(10)) '提取4xxxxx中的数据
bflag = Not bflag
n = n + 1
Winsock.Close
Winsock.LocalPort = 0
Winsock.Connect
End If
'(5) 连续发送
Private Sub Winsock_Connect()
Do While (n <= step And ((aflag Xor bflag) = True))
Winsock.SendData outbuf
aflag = bflag
Loop
If n > step Then
status.Caption = “命令执行完毕,仍保持连接状态”
End If
End Sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -