使用Visa和get_instrument时,“例外忽略:”为什么?

问题描述

我似乎收到了与Visa.ResourceManager.get_instrument()命令相关联的“忽略异常:”警告消息。但是,我不明白为什么收到警告。

  • 请注意,没有与警告相关的方法或类或其他任何内容。这使我认为脚本退出时正在发生。
  • 我可以通过注释代码中的“ self.rm.get_instrument()”行来消除警告。

即使有警告,从中提取代码的脚本也会正确运行。但是,发出不理解的警告会让我非常紧张。

我的代码示例如下。我正在使用Python 3.5.1和Visa 1.8。

对于警告的根本原因的建议将不胜感激。

编辑:我添加了我收到的警告(?)消息,格式为代码

import visa


class TestEquipment:
    def __init__(self,usbAddress='-1'):
        self.rm = visa.ResourceManager()
        # The following line appears to cause an "Exception ignored in:" warning.
        #   - If I comment-out the line,the warning goes away.
        self.my_instrument = self.rm.get_instrument(usbAddress)


class SpecificInstrument(TestEquipment):
    def __init__(self,usbAddress='-1'):
        TestEquipment.__init__(self,usbAddress=usbAddress)

if __name__ == '__main__':
    usb_address = 'USB0::0x0957::0x9018::MY52350569::INSTR'  # Hard-coded USB address for my borrowed instrument
    FieldSource = SpecificInstrument(usbAddress=usb_address)

这是我收到的警告(?)消息。

Exception ignored in: 
Process finished with exit code 0

解决方法

基于jasonharper在评论中的建议,解决方案是:

    FieldSource = SpecificInstrument(usbAddress=usb_address)  # Existing code
    # FieldSource.close()  # Doesn't work.  AttributeError: 'SpecificInstrument' object has no attribute 'close'
    # FieldSource.rm.close()  # No error but still get the "Exception ignored in" warning
    FieldSource.my_instrument.close()  # Success!  No more "Exception ignored in:" warning message!

请注意,为了完整起见,我提供了一些错误的开始(作为评论,但这并未导致解决方案。)