碧焱 发表于 2011-2-20 22:50:43

如何屏蔽MSComm控件弹出的错误提示?

我想调用MSComm控件写一个上位机软件,但在调用MSComm控件打开串口,如果不能正常打开时,会弹出一个错误提示框,并且中止程序,我不想串口打开错误就结束我的程序,有什么办法能很好解决这个问题??
初学AU3,请名位大鸟多多指教!!

ceoguang 发表于 2011-2-23 22:40:47

贴源码

碧焱 发表于 2011-3-3 22:27:52

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>


#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 375, 167, 192, 124)
$Button1 = GUICtrlCreateButton("打开串口", 40, 16, 75, 25)
$Edit1 = GUICtrlCreateEdit("", 176, 24, 185, 89)
GUICtrlSetData(-1, "Edit1")
$Button2 = GUICtrlCreateButton("发送数据", 40, 56, 65, 25)
$Button3 = GUICtrlCreateButton("关闭串口", 32, 96, 75, 25)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

DllOpen(@ScriptDir&'\CameraDll.dll')
While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
                Case $GUI_EVENT_CLOSE
                Exit
        Case $Button1
                $com =1
                $Comm=ObjCreate("MSCOMMLib.MSComm")
                $Comm.CommPort=$com;设定要使用的端口1~16
                $Comm.Handshaking=0
                $Comm.RThreshold=0
                $Comm.Settings="9600,N,8,1"
                $Comm.InputLen=0
                $Comm.RTSEnable=False
                $Comm.InputMode=1;0文本传输
                $Comm.PortOpen=TRUE
               
        Case $Button2
                $temp="this a test!"
                        $Comm.Output=$temp
                        $inp=$Comm.Input
                        GUICtrlSetData($Edit1,$inp)
                       
        EndSwitch
WEnd

碧焱 发表于 2011-3-3 22:30:34

抱歉,回到学校申请上网比较慢。。。请高手帮帮忙啊。。

碧焱 发表于 2011-3-3 22:34:34

如果COM1口被占用了,那么程序就会自动关掉,我想让它只弹出警告,让使用者再选择一个串口。。

happytc 发表于 2011-3-3 23:05:48

凡是象这种创建了“对象”的,你得对出错的对象事件进行处理,不然au3肯定自动退出, 如:
Global $oMyRet, $oMyError = ObjEvent("AutoIt.Error", "MyErrFunc")
自己随便写MyErrFunc函数了,象下面的:
Func MyErrFunc()
        Local $HexNumber
        $HexNumber = Hex($oMyError.number, 8)
        $oMyRet = $HexNumber
        $oMyRet = StringStripWS($oMyError.description, 3)
        SetError(1)
        Return
EndFunc
然后在调用对象函数里加上象下面就可以了
If @error Then
        SetError(100)
        Return $oMyRet
EndIf
页: [1]
查看完整版本: 如何屏蔽MSComm控件弹出的错误提示?