Python:使用pyvisa或pyseri获取设备“模型”

2024-05-23 16:28:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我编写了一个数据采集程序/脚本,与我们合作开发的一个设备一起工作。问题是我只能从这个设备中读取。不可能写,所以不能用连续剧“?IDN*“命令来知道这是什么设备。在

唯一定义这个设备的是它的“模型”,可以在Windows的控制面板的“设备和打印机”中看到。如下图所示:

enter image description here

设计这个设备的人能够创建一个labview简单程序,通过NI-VISA,通过一个名为“Intf Inst name”的东西从设备中提取这个名称,这就是“Interface In”构成:界面描述". 在

如果我得到这个型号的名称并将其与pyvisa设备名称进行比较,我将能够自动检测到我们的设备的存在,这是一件很重要的事情,以防USB断开。这是因为VISA通过在每台计算机上都可能不同的名称来打开设备,但是这个名称“GPS数据记录器”在任何地方都是一样的。在

我需要这个解决方案是跨平台的。这就是为什么我需要使用pyvisa或pyserial。尽管任何跨平台的选择都是可以的。在

所以我的问题简而言之:如何使用pyvisa/pyserial来查找与设备型号对应的型号名称(在我的例子中是“GPS数据记录器”)?在

请询问您可能需要的任何其他信息。在


更新

我知道有一个名为“viu ATTR_INTF_INST_name”的“attribute”pyvisa可以得到这个名称,但我不知道如何使用它。有人知道如何读取这些属性吗?在


Tags: 数据name命令程序脚本名称跨平台visa
2条回答

pyvisa有一个可选的^{} parameter,用于list_resources(),您可以使用它将搜索范围缩小到仅限于您的设备。它的syntax类似于正则表达式。在

试试这个:

from string import Template
VI_ATTR_INTF_INST_NAME = 3221160169
device_name = "GPS DATA LOGGER"
entries = dict(
  ATTR = VI_ATTR_INTF_INST_NAME,
  NAME = device_name )
query_template = Template(u'ASRL?*INSTR{$ATTR == "$NAME"}')
query = query_template.substitute(entries)

rm = visa.ResourceManager()
rm.list_resources(query)

我找到了做这件事的方法。不幸的是,它需要打开你电脑里的每一个VISA设备。我编写了一个小的pyvisa函数,它将用注释为您完成任务。函数返回包含作为参数提到的型号名称/描述符的所有设备:

import pyvisa

def findInstrumentByDescriptor(descriptor):
    devName = descriptor

    rm = pyvisa.ResourceManager()
    com_names=rm.list_resources()
    devicesFound = []

    #loop over all devices, open them, and check the descriptor
    for com in range(len(com_names)):
        try:
            #try to open instrument, if failed, just skip to the next device
            my_instrument=rm.open_resource(com_names[com])
        except:
            print("Failed to open " + com_names[com])
            continue
        try:
            # VI_ATTR_INTF_INST_NAME is 3221160169, it contains the model name "GPS DATA LOGGER" (check pyvisa manual for other VISA attributes)
            modelStr = my_instrument.get_visa_attribute(3221160169)

            #search for the string you need inside the VISA attribute
            if modelStr.find(devName) >= 0:
                #if found, will be added to the array devicesFound
                devicesFound.append(com_names[com])
                my_instrument.close()
        except:
            #if exception is thrown here, then the device should be closed
            my_instrument.close()

    #return the list of devices that contain the VISA attribute required
    return devicesFound



#here's the main call
print(findInstrumentByDescriptor("GPS DATA LOGGER"))

相关问题 更多 >