PLC與PC機(jī)通訊的VB源程序大體如下:我采用的是FX232BD通訊模塊,協(xié)議就是FX通訊協(xié)議。
強(qiáng)制PLC為RUN
Private Sub command1_Click()
Dim dat As String
commli.OutBufferCount = 0
commli.InBufferCount = 0
dat = "01FFRR0"
commli.Output = Chr$(5) + dat + SumChk(dat)
End Sub
強(qiáng)制PLC為STOP
Private Sub command2_Click()
Dim dat As String
commli.OutBufferCount = 0
commli.InBufferCount = 0
dat = "01FFRS0"
commli.Output = Chr$(5) + dat + SumChk(dat)
End Sub
強(qiáng)制Y0為1
Private Sub command3_Click()
Dim dat As String
commli.OutBufferCount = 0
commli.InBufferCount = 0
commli.Output = “01FFBW0Y001011”
End Sub
強(qiáng)制Y0為0
Private Sub command3_Click()
Dim dat As String
commli.OutBufferCount = 0
commli.InBufferCount = 0
commli.Output = “01FFBW0Y001010”
End Sub
設(shè)置電腦通信參數(shù)
Private Sub Commlis()
If commli.PortOpen = True Then
commli.PortOpen = False
End If
If Not commli.PortOpen Then
commli.CommPort = 1 ''通信口
commli.Settings = "9600,N,8,1"
commli.Handshaking = 0
commli.InputLen = 0
commli.OutBufferCount = 0
commli.InBufferCount = 0
commli.PortOpen = True
End If
Exit Sub
End Sub
Private Function SumChk(Dats$) As String
Dim i&
Dim CHK&
For i = 1 To Len(Dats)
CHK = CHK + Asc(Mid(Dats, i, 1))
Next i
SumChk = Right(Hex$(CHK), 2)
End Function