VB 上位机程序与PLC通讯

用VB编一个上位机程序,用来接收PLC发送的数据,只要求接受,然后把其中几位数据提取出来就可以,比如说PLC发送数据格式为021000000004080A010313FF... 用VB编一个上位机程序,用来接收PLC发送的数据,只要求接受,然后把其中几位数据提取出来就可以,比如说PLC发送数据格式为02 10 00 00 00 04 08 0A 01 03 13 FF DD 01 23 00 53其中从0A到23的数据是我想要的。用MSComm做
plc是S7-200 ,谁能给个VB程序
展开
 我来答
远风的梦想家
推荐于2016-08-15 · TA获得超过2550个赞
知道大有可为答主
回答量:1389
采纳率:0%
帮助的人:0
展开全部
'窗体上添加一个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
创作者Uxp8DgG7cQ
2019-08-16 · TA获得超过3868个赞
知道大有可为答主
回答量:3194
采纳率:30%
帮助的人:160万
展开全部
'窗体上添加一个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
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
LFSLY
2010-07-23
知道答主
回答量:43
采纳率:0%
帮助的人:19.6万
展开全部
MSCOMM中断
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
食趣无限mI
2010-07-23 · 超过16用户采纳过TA的回答
知道答主
回答量:134
采纳率:0%
帮助的人:66.3万
展开全部
是串口的吗,如果是串口就可以用MSCOMM,但如果能用网线的话就用OPC吧
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 2条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式