我应该如何构造该项目中的模块和功能?

时间:2019-12-20 15:13:43

标签: python python-3.x

我正在编写一个与安捷伦设备接口的框架。到目前为止,我可以与设备进行很好的交谈,但是我在弄清楚如何布局和构造整个类中使用的功能时遇到了麻烦。

一些背景知识:设备通过SCPI进行通信。我正在使用pyvisa库来执行此操作。

一些SCPI命令示例:

CALC:LIM:LOW -40, (@6001,6002)
CALC:LIM:UPP 75, (@6001,6002)
CALC:LIM:UPP:STAT ON, (@6001,6002,6003)
CALC:LIM:LOW:STAT ON, (@6003,6004,6005,6006)
CONF:TEMP THER,10000, (@6001)
CONF:VOLT:DC (@6003)
ROUT:SCAN (@6001,6002,6003,6004,6005,6006)
FREQ:VOLT:RANG 10,(@6003,6004,6005,6006)

我目前有一个Agilent基类,该基类由设备类继承,我们称之为DAU(数据采集单元)。

import pyvisa


class Agilent:
    """
    This is a parent class for interfacing with Agilent devices.

    Attributes:
        ip (str): The IP address of the device to connect to.
    """
    def __init__(self, ip: str) -> None:
        """
        The constructor for the Agilent class.

        Args:
            ip (str): The IP address of the device to connect to.
        """
        self.rm = pyvisa.ResourceManager()
        self.inst = self.rm.open_resource(f"TCPIP::{ip}::INSTR")

    def get_id(self) -> str:
        """
        The function to query the device ID.

        Returns:
            str: The device ID.
        """
        return self.query_command("*IDN?")


    def write_command(self, cmd: str) -> str:
        """
        The function to write a command over SCPI.

        Args:
            cmd (str): The command to write to the device.

        Returns:
            str: The command response from the device
        """
        command = '*WAI;' + cmd
        return self.inst.write(command)

还有dau.py

from ..agilent import Agilent

class DAU(Agilent):
    """
    This is the child class for Agilent 34980A

    Attributes:
        ip (str): The IP address of the device to connect to.
    """

    def __init__(self, ip: str) -> None:
        """
        The constructor for the DAU class.

        Uses super() to run the parent class initialization.

        Parameters:
            ip (str): The IP address of the device to connect to.
        """
        super().__init__(ip)

现在,当我导入agilent.dau.DAU时,就可以访问write_command()函数。

示例:

import agilent.dau

d = agilent.dau.DAU('10.10.10.206')
print(d.get_id())

这有效。但是现在我想组织并添加我需要实现的其余SCPI命令。

所以,我的主要问题是:我应该如何组织我的项目和模块,以便可以将命令添加到DAU类中,格式为(遵循SCPI布局)d.calc.limit.upper()d.conf.temp.thermal()?我想这样做,而不是像d.calc_limit_upper()这样使用长函数名,但是我似乎无法在测试中使用它。希望有人可以照亮我的道路。

预先感谢您的帮助,如果我需要发布更多代码,我可以做到。

0 个答案:

没有答案