Skip to content

Commit

Permalink
Clean up pyvisa and other imports
Browse files Browse the repository at this point in the history
  • Loading branch information
natezb committed Nov 15, 2023
1 parent 74d80d2 commit b577632
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 155 deletions.
7 changes: 4 additions & 3 deletions instrumental/drivers/funcgenerators/rigol.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
Driver module for Rigol signal generators.
"""
from enum import Enum, auto

from pyvisa import ResourceManager

from .. import ParamSet, SCPI_Facet, VisaMixin
from . import FunctionGenerator
from .. import VisaMixin, SCPI_Facet
from .. import ParamSet
from visa import ResourceManager

_INST_PARAMS = ['visa_address']
_INST_VISA_INFO = {
Expand Down
16 changes: 8 additions & 8 deletions instrumental/drivers/powersupplies/rigol.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# -*- coding: utf-8 -*-

from . import PowerSupply
from .. import VisaMixin, Facet, SCPI_Facet
from ... import u, Q_
from .. import ParamSet
import re
from enum import Enum
from visa import ResourceManager

import pyvisa
import re

from ... import Q_, u
from .. import ParamSet, SCPI_Facet, VisaMixin
from . import PowerSupply

_INST_PARAMS_ = ['visa_address']
_INST_VISA_INFO_ = {
Expand All @@ -18,7 +18,7 @@ def list_instruments():
"""Get a list of all power supplies currently attached"""
paramsets = []
search_string = "ASRL?*"
rm = ResourceManager()
rm = pyvisa.ResourceManager()
raw_spec_list = rm.list_resources(search_string)

for spec in raw_spec_list:
Expand Down Expand Up @@ -96,4 +96,4 @@ def local(self):
self.write('SYSTem:LOCal')

def remote(self):
self.write('SYSTem:REMote')
self.write('SYSTem:REMote')
21 changes: 7 additions & 14 deletions instrumental/drivers/scopes/rigol.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,14 @@
* DS1000Z series
"""
import pyvisa
from pyvisa.constants import InterfaceType
import numpy as np
from pint import UndefinedUnitError
from . import Scope
from .. import VisaMixin, SCPI_Facet, Facet
from ..util import visa_context
from ... import u, Q_
from enum import Enum
from .. import ParamSet
from visa import ResourceManager
from enum import Enum
import time
from enum import Enum

import numpy as np
from struct import unpack
from pyvisa import ResourceManager

from .. import ParamSet, SCPI_Facet, VisaMixin
from . import Scope

_INST_PARAMS_ = ['visa_address']
_INST_VISA_INFO_ = {
Expand Down Expand Up @@ -152,4 +145,4 @@ def remote(self):


class DS1000Z(RigolScope, VisaMixin):
pass
pass
15 changes: 7 additions & 8 deletions instrumental/drivers/scopes/tektronix.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@
"""
import datetime as dt

import pyvisa
from pyvisa.constants import InterfaceType
import numpy as np
import pyvisa
from pint import UndefinedUnitError
from pyvisa.constants import InterfaceType

from . import Scope
from .. import VisaMixin, SCPI_Facet, Facet
from ..util import visa_context
from ...util import to_str
from ... import Q_, u
from ...errors import Error
from ... import u, Q_

from ...util import to_str
from .. import Facet, SCPI_Facet, VisaMixin
from ..util import visa_context
from . import Scope

MODEL_CHANNELS = {
'TDS 210': 2,
Expand Down
61 changes: 8 additions & 53 deletions instrumental/drivers/spectrometers/agilent.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,17 @@
# -*- coding: utf-8 -*-
# Copyright 2013-2015 Nate Bogdanowicz
# Copyright 2023 Dodd Gray
"""
Driver module for HP 70000 spectrum analyzer.
Based on Nate's Tektronix scope driver
"""

# import visa
# import numpy as np
# from instrumental import u, Q_
# from pint import UndefinedUnitError
# from instrumental.drivers.spectrometers import Spectrometer
# from instrumental.drivers import _get_visa_instrument

import pyvisa
from pyvisa.constants import InterfaceType
import numpy as np
from pint import UndefinedUnitError
from pyvisa.constants import VI_SUCCESS_MAX_CNT, SerialTermination

from ... import Q_, u
from .. import VisaMixin
from . import Spectrometer
from .. import VisaMixin, SCPI_Facet
#from ..util import visa_context
from ... import u, Q_

_INST_PARAMS_ = ['visa_address']
_INST_VISA_INFO_ = {
Expand All @@ -30,46 +22,13 @@ class AgilentOSA(Spectrometer, VisaMixin):
"""
A base class for HP 70000 series spectrum analyzers
"""
# def __init__(self,params=None):
# """
# Create a spectrometer object that has the given VISA name *name* and connect
# to it. You can find available instrument names using the VISA
# Instrument Manager.
# """
# if params:
# self.inst = _instrument(params)
# else:
# self.inst = _instrument(default_params)
# self.inst.read_termination = "\n" # Needed for stripping termination
# #self.inst.write("header OFF")
# self.inst.write_termination = ';'
# self.inst.write('TDF P') # set the trace data format to be decimal numbers in parameter units

_INST_PARAMS_ = ['visa_address']
_INST_VISA_INFO_ = ('AGILENT', ['86142B'])

def _initialize(self):
self._rsrc.read_termination = '\n' # Needed for stripping termination
self._rsrc.timeout = 300
#self.inst.write("header OFF")
# self._rsrc.write_termination = ';'
self.write('FORMAT:DATA REAL32') # set the trace data format to be decimal numbers in parameter units
# if self.interface_type == InterfaceType.asrl:
# terminator = self.query('RS232:trans:term?').strip()
# self._rsrc.read_termination = terminator.replace('CR', '\r').replace('LF', '\n')
# elif self.interface_type == InterfaceType.usb:
# terminator = self.query('RS232:trans:term?').strip()
# self._rsrc.read_termination = terminator.replace('CR', '\r').replace('LF', '\n')
# elif self.interface_type == InterfaceType.tcpip:
# pass
# else:
# pass
#
# self.write("header OFF")

# def instrument_presets(self):
# self.write('IP')
# self.write('TDF P') # set the trace data format to be decimal numbers in parameter units

def set_center_wavelength(self,cf):
cf_nm = cf.to(u.nm).magnitude
Expand Down Expand Up @@ -159,8 +118,6 @@ def get_spectrum(self,trace='A'):
self._rsrc.timeout = 300
return wl, amp



def get_data(self, channel=1):
"""Retrieve a trace from the scope.
Expand Down Expand Up @@ -193,17 +150,17 @@ def get_data(self, channel=1):
inst.timeout = 10000
inst.write("curve?")
inst.read_termination = None
inst.end_input = pyvisa.constants.SerialTermination.none
inst.end_input = SerialTermination.none
# TODO: Change this to be more efficient for huge datasets
with inst.ignore_warning(pyvisa.constants.VI_SUCCESS_MAX_CNT):
with inst.ignore_warning(VI_SUCCESS_MAX_CNT):
s = inst.visalib.read(inst.session, 2) # read first 2 bytes
num_bytes = int(inst.visalib.read(inst.session, int(s[0][1]))[0])
buf = ''
while len(buf) < num_bytes:
raw_bin, _ = inst.visalib.read(inst.session, num_bytes-len(buf))
buf += raw_bin
print(len(raw_bin))
inst.end_input = pyvisa.constants.SerialTermination.termination_char
inst.end_input = SerialTermination.termination_char
inst.read_termination = '\n'
inst.read() # Eat termination
inst.timeout = tmo
Expand Down Expand Up @@ -243,5 +200,3 @@ def get_data(self, channel=1):
data_y = Q_((raw_data_y - y_offset)*y_scale + y_zero, y_unit)

return data_x, data_y


28 changes: 11 additions & 17 deletions instrumental/drivers/spectrometers/ando.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,20 @@
# -*- coding: utf-8 -*-
# Copyright 2013-2015 Nate Bogdanowicz
# Copyright 2023 Dodd Gray
"""
Driver module for Yokogawa/Ando spectrum analyzers.
Based on Nate's Tektronix scope driver
"""

# import visa
# import numpy as np
# from instrumental import u, Q_
# from pint import UndefinedUnitError
# from instrumental.drivers.spectrometers import Spectrometer
# from instrumental.drivers import _get_visa_instrument
from time import sleep

import visa
from pyvisa.constants import InterfaceType
import numpy as np
from time import sleep
import pyvisa
from pint import UndefinedUnitError
from pyvisa.constants import InterfaceType

from ... import Q_, u
from .. import VisaMixin
from . import Spectrometer
from .. import VisaMixin, SCPI_Facet
#from ..util import visa_context
from ... import u, Q_

_INST_PARAMS = ['visa_address']
_INST_VISA_INFO = {
Expand Down Expand Up @@ -230,17 +224,17 @@ def get_data(self, channel=1):
inst.timeout = 10000
inst.write("curve?")
inst.read_termination = None
inst.end_input = visa.constants.SerialTermination.none
inst.end_input = pyvisa.constants.SerialTermination.none
# TODO: Change this to be more efficient for huge datasets
with inst.ignore_warning(visa.constants.VI_SUCCESS_MAX_CNT):
with inst.ignore_warning(pyvisa.constants.VI_SUCCESS_MAX_CNT):
s = inst.visalib.read(inst.session, 2) # read first 2 bytes
num_bytes = int(inst.visalib.read(inst.session, int(s[0][1]))[0])
buf = ''
while len(buf) < num_bytes:
raw_bin, _ = inst.visalib.read(inst.session, num_bytes-len(buf))
buf += raw_bin
print(len(raw_bin))
inst.end_input = visa.constants.SerialTermination.termination_char
inst.end_input = pyvisa.constants.SerialTermination.termination_char
inst.read_termination = '\n'
inst.read() # Eat termination
inst.timeout = tmo
Expand Down Expand Up @@ -403,4 +397,4 @@ def set_measurement_nsamps(self, nsamps):
nsamps : int
Number of samples used to compute measurements
"""
self.inst.write("measu:stati:weighting {}".format(nsamps))
self.inst.write("measu:stati:weighting {}".format(nsamps))
50 changes: 6 additions & 44 deletions instrumental/drivers/spectrometers/hp.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,17 @@
# -*- coding: utf-8 -*-
# Copyright 2013-2015 Nate Bogdanowicz
# Copyright 2023 Dodd Gray
"""
Driver module for HP 70000 spectrum analyzer.
Based on Nate's Tektronix scope driver
"""

# import visa
# import numpy as np
# from instrumental import u, Q_
# from pint import UndefinedUnitError
# from instrumental.drivers.spectrometers import Spectrometer
# from instrumental.drivers import _get_visa_instrument

import pyvisa
from pyvisa.constants import InterfaceType
import numpy as np
import pyvisa
from pint import UndefinedUnitError

from ... import Q_, u
from .. import VisaMixin
from . import Spectrometer
from .. import VisaMixin, SCPI_Facet
#from ..util import visa_context
from ... import u, Q_

_INST_PARAMS = ['visa_address']
_INST_VISA_INFO = {
Expand All @@ -38,39 +30,11 @@ class HPOSA(Spectrometer, VisaMixin):
"""
A base class for HP 70000 series spectrum analyzers
"""
# def __init__(self,params=None):
# """
# Create a spectrometer object that has the given VISA name *name* and connect
# to it. You can find available instrument names using the VISA
# Instrument Manager.
# """
# if params:
# self.inst = _instrument(params)
# else:
# self.inst = _instrument(default_params)
# self.inst.read_termination = "\n" # Needed for stripping termination
# #self.inst.write("header OFF")
# self.inst.write_termination = ';'
# self.inst.write('TDF P') # set the trace data format to be decimal numbers in parameter units

def _initialize(self):
self._rsrc.read_termination = '\n' # Needed for stripping termination
self._rsrc.timeout = 300
#self.inst.write("header OFF")
self._rsrc.write_termination = ';'
self.write('TDF P') # set the trace data format to be decimal numbers in parameter units
# if self.interface_type == InterfaceType.asrl:
# terminator = self.query('RS232:trans:term?').strip()
# self._rsrc.read_termination = terminator.replace('CR', '\r').replace('LF', '\n')
# elif self.interface_type == InterfaceType.usb:
# terminator = self.query('RS232:trans:term?').strip()
# self._rsrc.read_termination = terminator.replace('CR', '\r').replace('LF', '\n')
# elif self.interface_type == InterfaceType.tcpip:
# pass
# else:
# pass
#
# self.write("header OFF")

def instrument_presets(self):
self.write('IP')
Expand Down Expand Up @@ -160,8 +124,6 @@ def get_spectrum(self,trace='A'):
self._rsrc.timeout = 300
return wl, amp



def get_data(self, channel=1):
"""Retrieve a trace from the scope.
Expand Down Expand Up @@ -367,4 +329,4 @@ def set_measurement_nsamps(self, nsamps):
nsamps : int
Number of samples used to compute measurements
"""
self.inst.write("measu:stati:weighting {}".format(nsamps))
self.inst.write("measu:stati:weighting {}".format(nsamps))
Loading

0 comments on commit b577632

Please sign in to comment.