(一)打開端口(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 (三)發(fā)送數(shù)據(jù)(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 (四)關(guān)閉端口(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 大家都在看 |
|
|