VB 上位机程序与PLC通讯
发布网友
发布时间:2022-11-06 23:53
我来回答
共4个回答
热心网友
时间:2023-10-30 08:33
'窗体上添加一个MSCOMM,一个按钮,然后粘贴如下代码,点一下按钮,然后PLC发送数据,如果程序收到了,会给出确认消息
Private Const StartFlag = &HA0
Private Const EndFlag = &H23
Private Sub Command1_Click()
Static Db() As Byte
Dim t() As Byte
Dim i As Long, j As Long
Command1.Enabled = False
MSComm1.CommPort = 1
MSComm1.Settings = "9600,N,8,1"
MSComm1.InputLen = 0
MSComm1.InputMode = comInputModeBinary
MSComm1.PortOpen = True
ReDim Db(0 To 0) As Byte
Dim found As Long
Do
DoEvents
t = MSComm1.Input
If UBound(t) >= 0 Then
ReDim Preserve Db(LBound(Db) To UBound(Db) + UBound(t) - LBound(t) + 1) As Byte
j = UBound(t)
For i = UBound(Db) To LBound(Db) Step -1
Db(i) = t(j)
Debug.Print t(j)
j = j - 1
If j < LBound(t) Then Exit For
Next i
found = -1
For i = LBound(Db) To UBound(Db)
If Db(i) = StartFlag Then
found = i
End If
If Db(i) = EndFlag And found <> -1 Then
Exit For
End If
Next i
If i <= UBound(Db) Then
Dim s As String
For j = found To i
s = s & Hex(Db(j)) & " "
Next j
MsgBox s
Exit Do
End If
End If
Loop
Command1.Enabled = True
MSComm1.PortOpen = False
End Sub
不明白的请百度HI
热心网友
时间:2023-10-30 08:34
'窗体上添加一个MSCOMM,一个按钮,然后粘贴如下代码,点一下按钮,然后PLC发送数据,如果程序收到了,会给出确认消息
Private
Const
StartFlag
=
&HA0
Private
Const
EndFlag
=
&H23
Private
Sub
Command1_Click()
Static
Db()
As
Byte
Dim
t()
As
Byte
Dim
i
As
Long,
j
As
Long
Command1.Enabled
=
False
MSComm1.CommPort
=
1
MSComm1.Settings
=
"9600,N,8,1"
MSComm1.InputLen
=
0
MSComm1.InputMode
=
comInputModeBinary
MSComm1.PortOpen
=
True
ReDim
Db(0
To
0)
As
Byte
Dim
found
As
Long
Do
DoEvents
t
=
MSComm1.Input
If
UBound(t)
>=
0
Then
ReDim
Preserve
Db(LBound(Db)
To
UBound(Db)
+
UBound(t)
-
LBound(t)
+
1)
As
Byte
j
=
UBound(t)
For
i
=
UBound(Db)
To
LBound(Db)
Step
-1
Db(i)
=
t(j)
Debug.Print
t(j)
j
=
j
-
1
If
j
<
LBound(t)
Then
Exit
For
Next
i
found
=
-1
For
i
=
LBound(Db)
To
UBound(Db)
If
Db(i)
=
StartFlag
Then
found
=
i
End
If
If
Db(i)
=
EndFlag
And
found
<>
-1
Then
Exit
For
End
If
Next
i
If
i
<=
UBound(Db)
Then
Dim
s
As
String
For
j
=
found
To
i
s
=
s
&
Hex(Db(j))
&
"
"
Next
j
MsgBox
s
Exit
Do
End
If
End
If
Loop
Command1.Enabled
=
True
MSComm1.PortOpen
=
False
End
Sub
不明白的请百度HI
热心网友
时间:2023-10-30 08:34
MSCOMM中断
热心网友
时间:2023-10-30 08:35
是串口的吗,如果是串口就可以用MSCOMM,但如果能用网线的话就用OPC吧