发布于 2007-01-18 11:49:35
0楼
楼主的意思是不是说ONComm事件的处理代码写在何处?
选择MSCOMM控件->属性->事件->对象事件->ONCOMM->"VBS动作"
代码如下:
Sub OnComm(Byval Item)
'此处写入事件处理代码
End Sub
至于书定规则,按VBS语法要求就可以了。
网上的例子确实很少,我提供一段代码供你参考,这是一段处理CDT规约的代码。如果你不懂CDT规约,也不用去管它了,只注意语法就行了。
Sub OnComm(Byval Item)
InitMod()
Dim cnvt
Dim obj_Com
Dim obj_textInput
Dim strInput
Dim strForCheck
Dim byForCheck
Dim bParseResult
Set cnvt = CreateObject("ADs.ArrayConvert")
Set obj_Com = ScreenItems("m_Com")
Set obj_textInput = ScreenItems("txt_Input")
If obj_Com.CommEvent = 2 Then
If obj_Com.InBufferCount > 0 Then
obj_Com.InputLen = obj_Com.InBufferCount
strInput = cnvt.CvOctetStr2vHexStr(obj_Com.Input)
strHInputAll = strHInputAll & strInput
obj_textInput.Text = strHInputAll
End If
If intHFirstSyn = 0 Then
If InStr(1,strHInputAll,strSyn,vbTextCompare) > 0 Then
strHInputAll = Right(strHInputAll , Len(strHInputAll) - InStr(1,strHInputAll,strSyn,vbTextCompare) + 1)
intHFirstSyn = 1
End If
End If
intHSecondSyn = Len(strHInputAll) + 1
If intHFirstSyn = 1 Then
If InStr(2,strHInputAll,strSyn,vbTextCompare) > 0 Then
intHSecondSyn = InStr(2,strHInputAll,strSyn,vbTextCompare)
End If
If intHP = 0 And intHSecondSyn > 24 Then intHP = 24
End If
If intHP >= 24 Then
If intHSecondSyn > intHP + 12 Then
strForCheck = Mid(strHInputAll,intHP + 1,((intHSecondSyn - intHP - 1)\12)*12)
byForCheck = cnvt.CvHexStr2vOctetStr(strForCheck)
bParseResult = CDTHParse(byForCheck)
Select Case bParseResult
Case 1
' HMIRuntime.Trace "OK!" &vbCrLf
Case 0
HMIRuntime.Trace "Length is Error!" &vbCrLf
Case -1
HMIRuntime.Trace "CRC is Error!" &vbCrLf
End Select
intHP = ((intHSecondSyn - 1)\12) * 12
End If
End If
If Len(strHInputAll) > intHSecondSyn Then
strHInputAll = Right(strHInputAll,Len(strHInputAll) - intHSecondSyn +1)
intHP = 0
intHFirstSyn = 0
End If
End If
End Sub