Random VISA Resource Query Operation

  • Share this:

Code introduction


This function randomly selects a VISA resource and performs a corresponding query operation based on the resource type (serial or VISA). For serial resources, it reads data; for VISA resources, it queries the device's IDN (instrument identification number).


Technology Stack : PyVISA

Code Type : PyVISA resource query

Code Difficulty : Intermediate


                
                    
import random
from pyvisa import ResourceManager

def random_resource_query(resource_type):
    rm = ResourceManager()
    resources = rm.list_resources()
    if resources:
        resource = random.choice(resources)
        rm.open_resource(resource)
        if resource_type == "asrl":
            return rm.read()
        elif resource_type == "visa":
            return rm.query("*IDN?")
        else:
            rm.close()
            return "Unsupported resource type"
    else:
        return "No resources found"                
              
Tags: