Python:使用pyvisa或pyserial

时间:2015-11-03 13:07:51

标签: python serial-port usb visa data-acquisition

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

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

enter image description here

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

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

我需要这个解决方案是跨平台的。这就是我需要使用pyvisa或pyserial的原因。虽然任何跨平台的替代方案都可以。

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

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

更新

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

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

pyvisalist_resources()提供了一个可选的query parameter,您可以使用它将搜索范围缩小到仅适用于您的设备。这个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)