繁体   English   中英

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

[英]Python: Get device “model” using pyvisa or pyserial

我编写了一个数据采集程序/脚本,该程序可与我们合作开发的设备一起使用。 问题是我只能从此设备读取 无法进行写操作,因此无法使用串行“?IDN *”命令来知道这是什么设备。

定义此设备的唯一内容是其“型号”,可以在Windows“控制面板”的“设备和打印机”中看到。 下图显示了它:

在此处输入图片说明

设计该设备的人能够创建一个Labview简单程序,该程序通过NI-VISA通过称为“接口信息:接口描述”的名称通过NI-VISA从设备中提取该名称。

如果获得此型号名称并将其与pyvisa设备名称进行比较,我将能够自动检测到我们设备的存在,以防万一USB断开连接,这很重要。 这是因为VISA通过在每台计算机上都可以使用不同名称的设备来打开设备,但是该名称“ GPS DATA LOGGER”在任何地方始终都是相同的。

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

因此,我的问题很简短 :如何使用pyvisa / pyserial查找与设备型号相对应的型号名称(在我的情况下为“ GPS DATA LOGGER”)?

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


更新

我了解到有一个名为“ VI_ATTR_INTF_INST_NAME”的“属性” pyvisa可以使用此名称,但是我不知道如何使用它。 有谁知道如何读取这些属性?

我找到了方法。 不幸的是,这涉及到打开计算机中拥有的每个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"))

pyvisa对于list_resources()有一个可选的query参数 ,您可以使用该参数将搜索范围缩小到仅设备。 语法类似于正则表达式。

尝试这个:

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM