Search code examples
pythonserial-portusbvisadata-acquisition

Python: Get device "model" using pyvisa or pyserial


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?


Solution

  • 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"))