完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
我正在使用Excel,Command expert和VBA编写测试脚本。
在某些时候我设置了仪器,我有一个VBA代码,用于检查仪器是否已连接。 从这个论坛我得到了VBA代码(如下)。 当仪器连接但挂起并返回错误时它工作正常:“自动化错误。无法转换对象”调试显示,如果给定IP地址没有仪器,代码将在设置instrument.IO = ioMgr行崩溃。 打开..........我有一个电子表格,其中包含一个连接到此脚本的按钮。 它将仪器ID字符串放在给定的单元格中,但在没有仪器时崩溃。 通常我连接了DSO9000示波器。 如果另一端没有仪器,如何使它变得万无一失? Sub instrIDN()Dim ioMgr As VisaComLib.ResourceManager Dim instrument As VisaComLib.FormattedIO488 Dim idn As String Dim instrADDR As String Set ioMgr = New VisaComLib.ResourceManager Set instrument = New VisaComLib.FormattedIO488 instrADDR =“TCPIP0 :: 192.168.1.6 :: inst0 :: INSTR“'instrument.IO.timeout = 1000设置instrument.IO = ioMgr.Open(instrADDR,AccessMode.NO_LOCK,1000,”“)instrument.WriteString”* IDN?“ idn = instrument.ReadString()ActiveSheet.Cells(50,2)= idn instrument.IO.Close End Sub 以上来自于谷歌翻译 以下为原文 I am writing a test script using Excel , Command expert and VBA. At some point I setup instruments and I have a VBA code that checks whether the instrument is connected. From this forum I got the VBA code (below). It works fine when the instrument is connected but hangs and returns error: " Automation error. Not able to convert object" Debugging shows that if there is no instrument at the given IP address the code crashes at the line Set instrument.IO = ioMgr.Open.......... I have a spreadsheet with a button connected to this script. It places the instrument ID string in a given cell, but crashes when there is no instrument. Normally I have DSO9000 scope connected. How to make it foolproof when there is no instrument at the other end ? Sub instrIDN() Dim ioMgr As VisaComLib.ResourceManager Dim instrument As VisaComLib.FormattedIO488 Dim idn As String Dim instrADDR As String Set ioMgr = New VisaComLib.ResourceManager Set instrument = New VisaComLib.FormattedIO488 instrADDR = "TCPIP0::192.168.1.6::inst0::INSTR" ' instrument.IO.Timeout = 1000 Set instrument.IO = ioMgr.Open(instrADDR, AccessMode.NO_LOCK, 1000, "") instrument.WriteString "*IDN?" idn = instrument.ReadString() ActiveSheet.Cells(50, 2) = idn instrument.IO.Close End Sub |
|
相关推荐
1个回答
|
|
您需要添加一些错误处理。
当Open无法找到你的乐器时,Open会抛出一个异常,因此你需要捕获它并优雅地处理它。 下面是对您的代码的修改。 此外,我将指出一个很棒的VBA知识网站:[http://www.cpearson.com/excel/errorhandling.htm]如果您搜索Agilent VISA COM帮助,您可以找到所有VISA COM错误代码 短语'返回代码'Alan VBA代码:Sub instrIDN()* On Error GoTo ErrHandler:* Dim ioMgr As VisaComLib.ResourceManager Dim instrument As VisaComLib.FormattedIO488 Dim idn As String Dim instrADDR As String Set ioMgr = New VisaComLib.ResourceManager Set instrument =新版VisaComLib.FormattedIO488 instrADDR =“TCPIP0 :: 192.168.1.6 :: inst0 :: INSTR”'instrument.IO.Timeout = 1000设置instrument.IO = ioMgr.Open(instrADDR,AccessMode.NO_LOCK,1000,“”) instrument.WriteString“* IDN?” idn = instrument.ReadString()ActiveSheet.Cells(50,2)= idn instrument.IO.Close * Exit Sub * * ErrHandler:* *'在VISA COM帮助中搜索'返回代码'了解更多详情* *如果错误。 Number = -2147221487然后* * MsgBox“找不到资源!”* *结束如果* * End Sub *编辑:hognala于2013年8月2日上午9:53 以上来自于谷歌翻译 以下为原文 You'll want to add some error handling. The Open will throw an exception when it can't find your instrument, so you'll want to capture that and handle it gracefully. Below is a modification of your code that does that. Also, I'll point you to a great site for VBA knowledge: [http://www.cpearson.com/excel/errorhandling.htm ] You can find all of the VISA COM Error codes if you search the Agilent VISA COM help for the phrase 'Return Codes' Alan VBA Code: Sub instrIDN() *On Error GoTo ErrHandler:* Dim ioMgr As VisaComLib.ResourceManager Dim instrument As VisaComLib.FormattedIO488 Dim idn As String Dim instrADDR As String Set ioMgr = New VisaComLib.ResourceManager Set instrument = New VisaComLib.FormattedIO488 instrADDR = "TCPIP0::192.168.1.6::inst0::INSTR" ' instrument.IO.Timeout = 1000 Set instrument.IO = ioMgr.Open(instrADDR, AccessMode.NO_LOCK, 1000, "") instrument.WriteString "*IDN?" idn = instrument.ReadString() ActiveSheet.Cells(50, 2) = idn instrument.IO.Close *Exit Sub* *ErrHandler:* *'Search the VISA COM help for 'Return Codes' for more details* *If Err.Number = -2147221487 Then* *MsgBox "Resource Not Found!"* *End If* *End Sub* Edited by: hognala on Aug 2, 2013 9:53 AM |
|
|
|
只有小组成员才能发言,加入小组>>
1226 浏览 0 评论
2348 浏览 1 评论
2159 浏览 1 评论
2024 浏览 5 评论
2906 浏览 3 评论
972浏览 1评论
关于Keysight x1149 Boundary Scan Analyzer
705浏览 0评论
N5230C用“CALC:MARK:BWID?”获取Bwid,Cent,Q,Loss失败,请问大佬们怎么解决呀
806浏览 0评论
1228浏览 0评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-25 09:11 , Processed in 1.233084 second(s), Total 48, Slave 41 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号