WinCC 通过VBS脚本进行串口通讯
一、打开端口
Sub 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
二、读Buffer
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
三、发送数据
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
四、关闭端口
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
因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- huatuowenda.com 版权所有 湘ICP备2023022495号-1
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务