我今天看wincc时,看到可以在画面里加MSComm控件,是不是wincc可以使用这个控件使用COM口,可以与其他仪表建立简单的基于COM口的通讯。如果可以请前辈们给个列子,学习学习!我的邮箱是:w1182@sohu.com
最佳答案
以下可以参考,也是别人提供的。
打开串口
ub OnClick(Byval Item)
Dim objMSComm1, tagConnection
Set objMSComm1 = HMIRuntime.Screens("Main").ScreenItems("MSComm1")
Set tagConnection = HMIRuntime.Tags("Connection")
If objMSComm1.PortOpen = False Then
' Assign com port number
objMSComm1.Commport = 1
' Values: 9600 Baud, N - No Parity, 8 - Databit, 1 - Stopbit
objMSComm1.Settings = "9600,N,8,1"
objMSComm1.RThreshold = 1
objMSComm1.SThreshold = 1
objMSComm1.InputLen = 0
objMSComm1.PortOpen = True
tagConnection.Write(True)
HMIRuntime.Trace("Port open." & vbCrLf)
Else
HMIRuntime.Trace("Port is already opened." & vbCrLf)
End If
End Sub
关闭串口
Sub OnClick(Byval Item)
Dim objMSComm1, tagConnection
Set objMSComm1 = HMIRuntime.Screens("Main").ScreenItems("MSComm1")
Set tagConnection = HMIRuntime.Tags("Connection")
If objMSComm1.PortOpen = True Then
objMSComm1.PortOpen = False
tagConnection.Write(False)
HMIRuntime.Trace("Port close." & vbCrLf)
End If
End Sub
发送数据
Sub OnClick(ByVal Item)
Dim tagOutput, objMSComm1
Set tagOutput = HMIRuntime.Tags("Output")
Set objMSComm1 = HMIRuntime.Screens("Main").ScreenItems("MSComm1")
If objMSComm1.PortOpen = True Then
tagOutput.Read
objMSComm1.Output = tagOutput.Value
tagOutput.Write("")
Else
HMIRuntime.Trace("No port is opened!" & vbCrLf)
End If
End Sub
读取数据
Option Explicit
Function action
Dim strBuffer, strTemp
Dim objMSComm1, tagBuffer
Set objMsComm1 = HMIRuntime.Screens("Main").ScreenItems("MSComm1")
Set tagBuffer = HMIRuntime.Tags("Buffer")
strTemp = ""
If objMSComm1.PortOpen = True Then
'read the buffer
strTemp = CStr(objMSComm1.Input)
If strTemp <> "" Then
'checking for the delimited character
If InStr(strTemp, Chr(6)) Then
strBuffer = Left(strTemp,Len(strTemp)-1)
Else
strBuffer = strTemp
End If
tagBuffer.Value = strBuffer
tagBuffer.Write
End If
Else
HMIRuntime.Trace("No port is opened!" & vbCrLf)
End If
End Function
以上
提问者对于答案的评价:
谢谢了,我回去试了一下可以。
原创文章,作者:more0621,如若转载,请注明出处:https://www.zhaoplc.com/plc274409.html