本帖最后由 baikhgmv 于 2016-9-6 12:41 编辑
通过MSComm控件实现WINCC串口通讯(C脚本和VB脚本两种方式)
测试环境:操作系统 win7 WINCC版本:V7.2 辅助工具:串口调试工具 ASPD虚拟串口工具 测试WINCC组态画面: 测试实验过程画面:
通过MSComm控件实现WINCC串口通讯(C脚本和VB脚本两种方式)
通过MSComm控件实现WINCC串口通讯(C脚本和VB脚本两种方式)
1、通过VB实现串口通讯 画面对象“打开画面”VB事件脚本: Sub OnOpen() Dim objMSComm, tagConnection Set objMSComm = HMIRuntime.Screens("串口通讯VB版").ScreenItems("COM") Set tagConnection = HMIRuntime.Tags("tagConnection1")
If objMSComm.PortOpen = False Then
' Assign com port number objMSComm.Commport = 4 ' Values: 9600 Baud, N - No Parity, 8 - Databit, 1 - Stopbit objMSComm.Settings = "9600,N,8,1" objMSComm.RThreshold = 1 objMSComm.SThreshold = 0 objMSComm.InBufferCount = 0 objMSComm.InputLen = 0 objMSComm.PortOpen = True tagConnection.Write (True) HMIRuntime.Trace("Port open." & vbCrLf) Else HMIRuntime.Trace("Port is already opened." & vbCrLf) End If End Sub
MSComm控件OnComm对象事件: Sub OnOpen() Dim objMSComm, tagConnection Set objMSComm = HMIRuntime.Screens("串口通讯VB版").ScreenItems("COM") Set tagConnection = HMIRuntime.Tags("tagConnection1")
If objMSComm.PortOpen = False Then
' Assign com port number objMSComm.Commport = 4 ' Values: 9600 Baud, N - No Parity, 8 - Databit, 1 - Stopbit objMSComm.Settings = "9600,N,8,1" objMSComm.RThreshold = 1 objMSComm.SThreshold = 0 objMSComm.InBufferCount = 0 objMSComm.InputLen = 0 objMSComm.PortOpen = True tagConnection.Write (True) HMIRuntime.Trace("Port open." & vbCrLf) Else HMIRuntime.Trace("Port is already opened." & vbCrLf) End If End Sub
“SEND按钮”鼠标左键按下事件:
Sub OnLButtonDown(ByVal Item, ByVal Flags, ByVal x, ByVal y) Dim objMSComm Dim strTemp,buffer
HMIRuntime.Trace("Communication!" & vbCrLf) Set objMsComm = HMIRuntime.Screens("串口通讯VB版").ScreenItems("COM") Set buffer = HMIRuntime.Tags("Buffer1")
strTemp = buffer.Read If objMSComm.PortOpen = True Then If strTemp<>"" then objMSComm.Output=strTemp End If End If End Sub
百度云附件:MSComm控件.zip
|