访问另一个程序的IVI会话

huangapple go评论71阅读模式
英文:

Access an IVI session from another program

问题

I'm using Python comtypes to get measurement device readings with IVI.
Since opening a session with the device is time-consuming, and I need to read the value hundreds of times, I'm trying to keep the session "alive" on a program that keeps running - while polling on the results as needed.

Can it be done?

The code I'm using now (both creating the IVI object AND reading):

import argparse
from comtypes.client import CreateObject

# reading the command line arguments
parser = argparse.ArgumentParser(description='Read the DCV from M918x PXI DMM')
parser.add_argument('--VISA_ADDRESS', default='PXI9::13::0::INSTR', help='get from Keysight connection expert or NI-MAX', type=str)

args, unknown = parser.parse_known_args()             # command line arguments parsed
visa_address = args.VISA_ADDRESS        # 'PXI9::13::0::INSTR'

# Creating the object - notice the ".1"
IVI_COM_AgM918x = CreateObject("AgM918x.AgM918x.1")

# Initialize the instrument
IVI_COM_AgM918x.Initialize(visa_address, False, False, "")
    
# Take a DC voltage measurement with a range of 30V and a resolution of 0.01V
retval = IVI_COM_AgM918x.DCVoltage.Measure(200.0, 0.01, True)
print(retval)

# Close the instrument session
IVI_COM_AgM918x.Close()

I mean to run a program once, with the session opening:

# Creating the object - notice the ".1"
IVI_COM_AgM918x = CreateObject("AgM918x.AgM918x.1")
    
# Initialize the instrument
IVI_COM_AgM918x.Initialize(visa_address, False, False, "")

Then, whenever I need a read:

# Take a DC voltage measurement with a range of 30V and a resolution of 0.01V
retval = IVI_COM_AgM918x.DCVoltage.Measure(200.0, 0.01, True)
print(retval)

Once finished, I will close the session and kill the first program.

  1. Is this the best way to do it?
  2. How to do it?
英文:

I'm using Python comtypes to get measurement device readings with IVI.
Since opening a session with the device is time consuming, and I need to read the value hundreds of times, I'm trying to keep the session "alive" on a program that keeps running - while polling on the results as needed.

Can it be done?

The code I'm using now (both creating the IVI object AND reading):

import argparse
from comtypes.client import CreateObject

# reading the command line arguments
parser = argparse.ArgumentParser(description='Read the DCV from M918x PXI DMM')
parser.add_argument('--VISA_ADDRESS', default='PXI9::13::0::INSTR', help='get from Keysight connection expert or NI-MAX', type=str)

args, unknown = parser.parse_known_args()             # command line arguments parsed
visa_address = args.VISA_ADDRESS        # 'PXI9::13::0::INSTR'

# Creating the object - notice the ".1"
IVI_COM_AgM918x = CreateObject("AgM918x.AgM918x.1")

# Initialize the instrument
IVI_COM_AgM918x.Initialize(visa_address, False, False, "")

# Take a DC voltage measurement with a range of 30V and a resolution of 0.01V
retval = IVI_COM_AgM918x.DCVoltage.Measure(200.0, 0.01, True)
print(retval)

# Close the instrument session
IVI_COM_AgM918x.Close()

I mean to run a program once, with the session opening:

# Creating the object - notice the ".1"
IVI_COM_AgM918x = CreateObject("AgM918x.AgM918x.1")
    
# Initialize the instrument
IVI_COM_AgM918x.Initialize(visa_address, False, False, "")

then, whenever I need a read:

# Take a DC voltage measurement with a range of 30V and a resolution of 0.01V
retval = IVI_COM_AgM918x.DCVoltage.Measure(200.0, 0.01, True)
print(retval)

Once finished, I will close the session and kill the first program.

  1. Is this the best way to do it?
  2. How to do it?

答案1

得分: 1

# 为 IVI 设备创建一个独立的类:
# 将管理 IVI 会话的代码放在主脚本之外,将 IVI 设备功能封装在一个独立的类中是个好主意。这样可以使代码更有组织性,也更容易管理。

import argparse
from comtypes.client import CreateObject

class IVIDevice:
    def __init__(self, visa_address):
        self.visa_address = visa_address
        self.IVI_COM_AgM918x = CreateObject("AgM918x.AgM918x.1")
        self.IVI_COM_AgM918x.Initialize(self.visa_address, False, False, "")

    def read_voltage(self, range, resolution):
        retval = self.IVI_COM_AgM918x.DCVoltage.Measure(range, resolution, True)
        return retval

    def close(self):
        self.IVI_COM_AgM918x.Close()

# 为多次读取创建一个循环:
# 可以创建一个循环来多次读取设备值。这样,你可以控制读取的次数,并且轻松地重用相同的会话。

def main():
    parser = argparse.ArgumentParser(description='从 M918x PXI DMM 读取 DCV')
    parser.add_argument('--VISA_ADDRESS', default='PXI9::13::0::INSTR', help='从 Keysight 连接专家或 NI-MAX 获取', type=str)
    args, unknown = parser.parse_known_args()
    visa_address = args.VISA_ADDRESS

    device = IVIDevice(visa_address)

    try:
        num_readings = 10  # 根据需要调整读取次数
        for _ in range(num_readings):
            retval = device.read_voltage(200.0, 0.01)
            print(retval)
    finally:
        device.close()

if __name__ == "__main__":
    main()

# 异常处理:
# 确保正确处理异常,尤其是在与外部设备交互时。
# 优化测量速率:
# 请记住,多次读取设备可能需要相当长的时间。
英文:

Create a Separate Class for the IVI Device:
Instead of writing the code to manage the IVI session directly in your main script, it's a good idea to encapsulate the IVI device functionality in a separate class. This makes the code more organized and easier to manage.

import argparse
from comtypes.client import CreateObject

class IVIDevice:
    def __init__(self, visa_address):
        self.visa_address = visa_address
        self.IVI_COM_AgM918x = CreateObject("AgM918x.AgM918x.1")
        self.IVI_COM_AgM918x.Initialize(self.visa_address, False, False, "")

    def read_voltage(self, range, resolution):
        retval = self.IVI_COM_AgM918x.DCVoltage.Measure(range, resolution, True)
        return retval

    def close(self):
        self.IVI_COM_AgM918x.Close()

Use a Loop for Multiple Readings:
You can create a loop to read the device values multiple times. This way, you can control the number of readings and easily reuse the same session.

def main():
    parser = argparse.ArgumentParser(description='Read the DCV from M918x PXI DMM')
    parser.add_argument('--VISA_ADDRESS', default='PXI9::13::0::INSTR', help='get from Keysight connection expert or NI-MAX', type=str)
    args, unknown = parser.parse_known_args()
    visa_address = args.VISA_ADDRESS

    device = IVIDevice(visa_address)

    try:
        num_readings = 10  # Adjust the number of readings as needed
        for _ in range(num_readings):
            retval = device.read_voltage(200.0, 0.01)
            print(retval)
    finally:
        device.close()

if __name__ == "__main__":
    main()

Exception Handling:
Ensure you handle exceptions properly, especially when working with external devices.
Optimize the Measurement Rate:
Keep in mind that reading the device hundreds of times might take a considerable amount of time.

huangapple
  • 本文由 发表于 2023年7月23日 20:11:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76748169.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定