diff --git a/pyproject.toml b/pyproject.toml index 1e12783..31c448c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,5 +1,5 @@ [project] -name = "rigol-py-rig" +name = "rigol_dho_lib" version = "0.1.0" description = "Add your description here" readme = "README.md" diff --git a/src/main.py b/src/main.py index 6c5370e..1967568 100644 --- a/src/main.py +++ b/src/main.py @@ -1,15 +1,23 @@ from rigol_py_rig.rigol import RigolOsc +from rigol_py_rig.siglent import SiglentGen, ChannelID import matplotlib.pyplot as plt # uv def main(): - osc = RigolOsc("TCPIP::10.112.1.9::INSTR") + + gen = SiglentGen("TCPIP::10.112.1.17::INSTR") + + gen.channels[ChannelID.CH1].apply_sine(2e3, 1, 0) + gen.channels[ChannelID.CH1].set_output(1) + + return + + # osc = RigolOsc("TCPIP::10.112.1.9::INSTR") print(osc.getTrigStatus()) osc.single() - depth = osc.getMemoryDepth() # osc.setPoints(depth) ret = osc.channels[0].getWaveform() @@ -22,7 +30,7 @@ def main(): print(f"Y orogin {osc.channels[0].getYOrigin()}") print(f"Y reference {osc.channels[0].getYReference()}") - plt.plot(ret) + plt.plot(osc.channels[0].genTimeArray(ret), ret) plt.show() diff --git a/src/rigol_py_rig/__init__.py b/src/rigol_dho_lib/__init__.py similarity index 100% rename from src/rigol_py_rig/__init__.py rename to src/rigol_dho_lib/__init__.py diff --git a/src/rigol_py_rig/rigol.py b/src/rigol_dho_lib/rigol.py similarity index 95% rename from src/rigol_py_rig/rigol.py rename to src/rigol_dho_lib/rigol.py index 85cfb80..c606bdc 100644 --- a/src/rigol_py_rig/rigol.py +++ b/src/rigol_dho_lib/rigol.py @@ -6,6 +6,7 @@ from enum import Enum import numpy as np CHANNEL_COUNT = 4 +# Rigol DHO4804 class TrigState(Enum): @@ -99,6 +100,12 @@ class Channel: ret = self.instr.query(":WAVeform:YREFerence?") return int(ret) + def genTimeArray(self, waveform): + x_delta = self.getXincrement() + time = np.arange(0, len(waveform) * x_delta, x_delta) + + return time + class RigolOsc: def __init__(self, visa_address: str): diff --git a/src/rigol_dho_lib/siglent.py b/src/rigol_dho_lib/siglent.py new file mode 100644 index 0000000..c8a4153 --- /dev/null +++ b/src/rigol_dho_lib/siglent.py @@ -0,0 +1,96 @@ +import wave +from pyvisa.resources.resource import Resource +import pyvisa +from enum import Enum +import numpy as np + + +class Waveform(Enum): + SINE = "SINE" + SQUARE = "SQUARE" + RAMP = "RAMP" + PULSE = "PULSE" + NOISE = "NOISE" + ARB = "ARB" + + +class ChannelID(Enum): + CH1 = 1 + CH2 = 2 + + +CHANNEL_COUNT = 2 + + +class Channel: + def __init__(self, channel_id: ChannelID, instr: Resource): + self.id = channel_id + self.index = channel_id.value + self.instr = instr + + # ---- Basic waveform ---- + def set_waveform(self, waveform: Waveform): + self.instr.write(f"C{self.index}:BSWV WVTP,{waveform.value}") + + def set_frequency(self, freq: float): + self.instr.write(f"C{self.index}:BSWV FRQ,{freq}") + + def set_amplitude(self, amplitude: float): + self.instr.write(f"C{self.index}:BSWV AMP,{amplitude}") + + def set_offset(self, offset: float): + self.instr.write(f"C{self.index}:BSWV OFST,{offset}") + + # ---- Output control (merged) ---- + def set_output(self, state: bool): + cmd = "ON" if state else "OFF" + self.instr.write(f"C{self.index}:OUTP {cmd}") + + # ---- Convenience ---- + def apply_sine(self, freq: float, amp: float, offset: float = 0): + self.set_waveform(Waveform.SINE) + self.set_frequency(freq) + self.set_amplitude(amp) + self.set_offset(offset) + + # ---- Query ---- + def get_frequency(self) -> float: + response = self.instr.query(f"C{self.index}:BSWV?") + return float(response.split(",")[1]) + + +class GenChannels: + def __init__(self, instr: Resource): + self.instr = instr + self.channels = { + ChannelID.CH1: Channel(ChannelID.CH1, instr), + ChannelID.CH2: Channel(ChannelID.CH2, instr), + } + + def __getitem__(self, ch: ChannelID) -> Channel: + return self.channels[ch] + + +class SiglentGen: + def __init__(self, visa_address: str): + rm = pyvisa.ResourceManager() + self.instr: Resource = rm.open_resource(visa_address) + + self.channels = GenChannels(self.instr) + + self.initialize() + + # ---- Initialization ---- + def initialize(self): + self.instr.write("*RST") + self.instr.write("*CLS") + + # ---- Global commands ---- + def identify(self) -> str: + return self.instr.query("*IDN?") + + def close(self): + self.instr.close() + + def get_error(self): + return self.instr.query("SYST:ERR?")