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
请问这有什么区别?