2

I wrote a data acquisition program/script that works with a device developed by our collaboration. The problem is that I can only read from this device. No writing is possible, so it's not possible to use a serial "?IDN*" command to know what device this is.

The only thing that defines this device is its "Model" that can be seen in "Devices and Printers" in Control Panel of Windows. The following figure shows it:

enter image description here

The guy who designed the device was able to create a labview simple program that extracts this name from the device through NI-VISA through something called "Intf Inst Name", which is called "Interface Information:Interface Description".

If I get this model name and compare it with the pyvisa device name, I'll be able to automatically detect the presence of our device, which is an important thing to have, in case a USB disconnect happens. This is because VISA opens the device through a name that can be different on every computer, but this name "GPS DATA LOGGER" is the same everywhere and always.

I need this solution to be cross-platform. That's why I need to use pyvisa or pyserial. Though any cross-platform alternative is OK.

So my question in brief: How can I use pyvisa/pyserial to find the model name corresponding to the device model (in my case "GPS DATA LOGGER")?

Please ask for any additional information you may require.


Update

I learned that there's an "attribute" pyvisa with the name "VI_ATTR_INTF_INST_NAME" that would get this name, but I don't know how to use it. Does anyone know how to read these attributes?

The Quantum Physicist
  • 24,987
  • 19
  • 103
  • 189

2 Answers2

1

I found the way to do it. Unfortunately it involves opening every VISA device you have in your computer. I wrote a small pyvisa function that will do the task for you with comments. The function returns all the devices that contain the model name/descriptor mentioned as a parameter:

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"))
The Quantum Physicist
  • 24,987
  • 19
  • 103
  • 189
0

pyvisa has an optional query parameter for list_resources(), which you can use to narrow the scope of your search to just your device. The syntax for this is like a regular expression.

Try this:

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)
Joe Friedrichsen
  • 1,976
  • 14
  • 14
  • I'll try it some time and give you my feedback. Thanks :) – The Quantum Physicist Nov 03 '15 at 20:54
  • I'd like to use this solution as it is much shorter, yet I could not make it to work... I guess it needs the exact attribute name for the instrument, while the previous solution allowed for partial name. I'll try to find a way to go around this. – DarkLight Feb 16 '20 at 08:07