是德科技
直播中

徐静怡

7年用户 142经验值
私信 关注
[问答]

VISA COM和Excel VBA错误处理

我正在使用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)

杜文渊

2019-2-25 16:17:46
您需要添加一些错误处理。
当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
举报

更多回帖

发帖
×
20
完善资料,
赚取积分