找回密码
 加入
搜索
查看: 4757|回复: 5

[系统综合] 如何屏蔽MSComm控件弹出的错误提示?

[复制链接]
发表于 2011-2-20 22:50:43 | 显示全部楼层 |阅读模式
我想调用MSComm控件写一个上位机软件,但在调用MSComm控件打开串口,如果不能正常打开时,会弹出一个错误提示框,并且中止程序,我不想串口打开错误就结束我的程序,有什么办法能很好解决这个问题??
初学AU3,请名位大鸟多多指教!!
发表于 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口被占用了,那么程序就会自动关掉,我想让它只弹出警告,让使用者再选择一个串口。。
发表于 2011-3-3 23:05:48 | 显示全部楼层
凡是象这种创建了“对象”的,你得对出错的对象事件进行处理,不然au3肯定自动退出, 如:
Global $oMyRet[2], $oMyError = ObjEvent("AutoIt.Error", "MyErrFunc")
自己随便写MyErrFunc函数了,象下面的:
Func MyErrFunc()
        Local $HexNumber
        $HexNumber = Hex($oMyError.number, 8)
        $oMyRet[0] = $HexNumber
        $oMyRet[1] = StringStripWS($oMyError.description, 3)
        SetError(1)
        Return
EndFunc  
然后在调用对象函数里加上象下面就可以了
If @error Then
        SetError(100)
        Return $oMyRet[1]
EndIf
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-9-21 13:51 , Processed in 0.071865 second(s), 21 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表