标题:VB与台达PLC通讯问题
只看楼主
h1305833
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2013-10-9
结帖率:0
已结贴  问题点数:20 回复次数:2 
VB与台达PLC通讯问题
我将下面的程序放在Private Sub Form_Load()里面,就可以读取并显示PLC的运行状态,
Private Sub Form_Load()
    Dim s1 As String
    Dim s2 As String
    Dim s22 As String
    Dim s3 As String
    Dim s4 As String
     
    s2 = "01020C300001"
    s22 = LRC(s2)
    s1 = ":" + s2 + s22 + Chr$(13) + Chr$(10)
    MSComm1.InBufferCount = 0
    MSComm1.OutBufferCount = 0
    MSComm1.Output = s1
    延时 200
    s3 = MSComm1.Input
    s4 = Mid$(s3, 8, 2)
    If s4 = "31" Then
        plc = 1
    Else: If s4 = "30" Then plc = 0
    End If
    If plc = 1 Then
        Label2.Caption = "PLC Running"
        Shape1.FillColor = RGB(0, 255, 0) 'green
    Else
        Label2.Caption = "PLC Stopped!"
        Shape1.FillColor = RGB(255, 0, 0) 'red
    End If
End Sub
但是放在TIMER里面就不可以的?
Private Sub Timer1_Timer()
Dim s1 As String
    Dim s2 As String
    Dim s22 As String
    Dim s3 As String
    Dim s4 As String
     
    s2 = "01020C300001"
    s22 = LRC(s2)
    s1 = ":" + s2 + s22 + Chr$(13) + Chr$(10)
    MSComm1.InBufferCount = 0
    MSComm1.OutBufferCount = 0
    MSComm1.Output = s1
    延时 200
    s3 = MSComm1.Input
    s4 = Mid$(s3, 8, 2)
    If s4 = "31" Then
        plc = 1
    Else: If s4 = "30" Then plc = 0
    End If
    If plc = 1 Then
        Label2.Caption = "PLC Running"
        Shape1.FillColor = RGB(0, 255, 0) 'green
    Else
        Label2.Caption = "PLC Stopped!"
        Shape1.FillColor = RGB(255, 0, 0) 'red
    End If
End Sub
请问这有什么区别?
2013-10-09 12:11
Artless
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:103
帖 子:4211
专家分:28888
注 册:2009-4-8
得分:20 
Timer1.Interval = 0?
Timer1.Enabled = False?

无知
2013-10-10 12:14
vbvcr51
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:18
帖 子:364
专家分:1724
注 册:2013-11-3
得分:0 
timer时间间隔够吗,plc的数据来得及传吗?
2013-11-04 21:53



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-421612-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.318806 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved