分享

WinCC 通过VBS脚本进行串口通讯

 工控库吧 2021-12-08
PLC价格还要缺货多久?
电气人上哪儿接单靠谱?    

(一)打开端口

(1)代码图片

(2)VBS代码

Sub OnClick(Byval Item)     

       DimobjMSComm1, tagConnection

       SetobjMSComm1 = HMIRuntime.Screens("Main").ScreenItems("MSComm1")

       SettagConnection = HMIRuntime.Tags("Connection")

       IfobjMSComm1.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("Portopen." & vbCrLf)

       Else

              HMIRuntime.Trace("Portis already opened." & vbCrLf)

       EndIf

End Sub

(二)读Buffer

(1)代码图片

(2)VBS代码

Option Explicit

Function action

       DimstrBuffer, strTemp

       DimobjMSComm1, tagBuffer

       SetobjMsComm1 = HMIRuntime.Screens("Main").ScreenItems("MSComm1")

       SettagBuffer  =HMIRuntime.Tags("Buffer")

       strTemp= ""

       IfobjMSComm1.PortOpen = True Then

              'readthe buffer

              strTemp= CStr(objMSComm1.Input)

              IfstrTemp <> "" Then

                     'checkingfor the delimited character

                     IfInStr(strTemp, Chr(6)) Then

                            strBuffer= Left(strTemp,Len(strTemp)-1)

                     Else

                            strBuffer= strTemp

                     EndIf

                     tagBuffer.Value= strBuffer

                     tagBuffer.Write

              EndIf

       Else

              HMIRuntime.Trace("Noport is opened!" & vbCrLf)

       EndIf

End Function

(三)发送数据

(1)代码图片

(2)VBS代码

Sub OnClick(ByVal Item) 

       DimtagOutput, objMSComm1

       SettagOutput = HMIRuntime.Tags("Output")

       SetobjMSComm1 = HMIRuntime.Screens("Main").ScreenItems("MSComm1")

       IfobjMSComm1.PortOpen = True Then

              tagOutput.Read

              objMSComm1.Output= tagOutput.Value

              tagOutput.Write("")

       Else

              HMIRuntime.Trace("Noport is opened!" & vbCrLf)

       EndIf

End Sub

(四)关闭端口

(1)代码图片

 (2)VBS代码

Sub OnClick(Byval Item)  

       DimobjMSComm1, tagConnection

       SetobjMSComm1 = HMIRuntime.Screens("Main").ScreenItems("MSComm1")

       SettagConnection = HMIRuntime.Tags("Connection")

       IfobjMSComm1.PortOpen = True Then     

              objMSComm1.PortOpen= False

              tagConnection.Write(False)

              HMIRuntime.Trace("Portclose." & vbCrLf)

       EndIf

End Sub



大家都在看



【视频】德国工程师是如何制作PLC柜的?

【视频】德国威图电气柜是如何生产出的?

【视频】德国工程师的办公环境是怎样的?

【视频】PROFINET为何比PROFIBUS更牛?

    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多