您需要添加一些错误处理。
当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
您需要添加一些错误处理。
当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
举报