Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*.pyc

build
_build
dist
*.egg-info

Expand Down
2 changes: 1 addition & 1 deletion doc/exec.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
except ImportError:
from io import StringIO

from sphinx.util.compat import Directive
from docutils.parsers.rst import Directive
from docutils import nodes, statemachine

class ExecDirective(Directive):
Expand Down
29 changes: 25 additions & 4 deletions ivi/interface/pyvisa.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,26 @@ class PyVisaInstrument:
"PyVisa wrapper instrument interface client"
def __init__(self, resource, *args, **kwargs):
if type(resource) is str:
self.instrument = visa_instrument_opener(resource, *args, **kwargs)
i = visa_instrument_opener(resource, *args, **kwargs)
# Support for "TCPIPx::aaa.bbb.ccc.ddd::ppppp::SOCKET" resources:
# These have no separate control channel, thus a termination character
# is always needed. Default newline is good for most instruments.
if "socket" in resource.lower():
if not hasattr(i, "read_termination") or not i.read_termination:
i.read_termination = "\n"
if not hasattr(i, "write_termination") or not i.write_termination:
i.write_termination = "\n"
# Setting up self.write_termination as a shortcut to the respetive
# VISA instrument class property value. This might speed up the
# self.write() method further below, although not sure by how much.
if hasattr(i, "write_termination"):
self.write_termination = i.write_termination
else:
self.write_termination = ""
# For compatibility with old style PyVISA
if not hasattr(self.instrument, 'assert_trigger'):
self.instrument.assert_trigger = self.instrument.trigger
if not hasattr(i, 'assert_trigger'):
i.assert_trigger = i.trigger
self.instrument = i
else:
self.instrument = resource
self.buffer = io.BytesIO()
Expand Down Expand Up @@ -104,7 +120,12 @@ def write(self, message, encoding = 'utf-8'):
for message_i in message:
self.write(message_i, encoding)
return
self.write_raw(str(message).encode(encoding))
# Support "TCPIPx::::::SOCKET" resources, see __init__
if self.write_termination:
message = str(message) + self.write_termination
else:
message = str(message)
self.write_raw(message.encode(encoding))

def read(self, num=-1, encoding = 'utf-8'):
"Read string from instrument"
Expand Down
33 changes: 20 additions & 13 deletions ivi/ivi.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ def count(self):

class IviContainer(PropertyCollection):
def __init__(self, *args, **kwargs):
super(IviContainer, self).__init__(*args, **kwargs)
super(IviContainer, self).__init__()

def _add_attribute(self, name, attr, doc = None):
cur_obj = self
Expand Down Expand Up @@ -1615,7 +1615,7 @@ def __init__(self, resource = None, id_query = False, reset = False, *args, **kw
# process out args for initialize
kw = {}
for k in ('range_check', 'query_instr_status', 'cache', 'simulate', 'record_coercions',
'interchange_check', 'driver_setup', 'prefer_pyvisa'):
'interchange_check', 'driver_setup', 'prefer_pyvisa', 'pyvisa_opts'):
if k in kwargs:
kw[k] = kwargs.pop(k)

Expand Down Expand Up @@ -1696,6 +1696,7 @@ def __init__(self, resource = None, id_query = False, reset = False, *args, **kw

Example resource strings::

'TCPIP0::10.0.0.1::12345::SOCKET' where 12345 is a TCP port
'TCPIP::10.0.0.1::INSTR'
'TCPIP0::10.0.0.1::INSTR'
'TCPIP::10.0.0.1::gpib,5::INSTR'
Expand Down Expand Up @@ -1753,6 +1754,8 @@ def __init__(self, resource = None, id_query = False, reset = False, *args, **kw
+-------------------------+----------------------+---------------------+
| Prefer PyVISA | False | prefer_pyvisa |
+-------------------------+----------------------+---------------------+
| PyVISA Options | {} | pyvisa_opts |
+-------------------------+----------------------+---------------------+

Each IVI specific driver defines it own meaning and valid values for the
Driver Setup attribute. Many specific drivers ignore the value of the
Expand Down Expand Up @@ -1823,6 +1826,7 @@ def __del__(self):
def _initialize(self, resource = None, id_query = False, reset = False, **keywargs):
"Opens an I/O session to the instrument."

self._pyvisa_opts = {}
# decode options
for op in keywargs:
val = keywargs[op]
Expand All @@ -1842,6 +1846,8 @@ def _initialize(self, resource = None, id_query = False, reset = False, **keywar
self._driver_operation_driver_setup = val
elif op == 'prefer_pyvisa':
self._prefer_pyvisa = bool(val)
elif op == 'pyvisa_opts':
self._pyvisa_opts.update(val)
else:
raise UnknownOptionException('Invalid option')

Expand All @@ -1853,6 +1859,7 @@ def _initialize(self, resource = None, id_query = False, reset = False, **keywar
elif type(resource) == str:
# parse VISA resource string
# valid resource strings:
# TCPIP0::10.0.0.1::12345::SOCKET
# TCPIP::10.0.0.1::INSTR
# TCPIP0::10.0.0.1::INSTR
# TCPIP::10.0.0.1::gpib,5::INSTR
Expand All @@ -1870,11 +1877,11 @@ def _initialize(self, resource = None, id_query = False, reset = False, **keywar
# ASRL::COM1,9600,8n1::INSTR
# ASRL::/dev/ttyUSB0,9600::INSTR
# ASRL::/dev/ttyUSB0,9600,8n1::INSTR
m = re.match('^(?P<prefix>(?P<type>TCPIP|USB|GPIB|ASRL)\d*)(::(?P<arg1>[^\s:]+))?(::(?P<arg2>[^\s:]+(\[.+\])?))?(::(?P<arg3>[^\s:]+))?(::(?P<arg4>[^\s:]+))?(::(?P<suffix>INSTR))$', resource, re.I)
m = re.match('^(?P<prefix>(?P<type>TCPIP|USB|GPIB|ASRL)\d*)(::(?P<arg1>[^\s:]+))?(::(?P<arg2>[^\s:]+(\[.+\])?))?(::(?P<arg3>[^\s:]+))?(::(?P<arg4>[^\s:]+))?(::(?P<suffix>INSTR|SOCKET))$', resource, re.I)
if m is None:
if 'pyvisa' in globals():
# connect with PyVISA
self._interface = pyvisa.PyVisaInstrument(resource)
self._interface = pyvisa.PyVisaInstrument(resource, **self._pyvisa_opts)
else:
raise IOException('Invalid resource string')
else:
Expand All @@ -1889,58 +1896,58 @@ def _initialize(self, resource = None, id_query = False, reset = False, **keywar
# TCP connection
if self._prefer_pyvisa and 'pyvisa' in globals():
# connect with PyVISA
self._interface = pyvisa.PyVisaInstrument(resource)
self._interface = pyvisa.PyVisaInstrument(resource, **self._pyvisa_opts)
elif 'vxi11' in globals():
# connect with VXI-11
self._interface = vxi11.Instrument(resource)
elif 'pyvisa' in globals():
# connect with PyVISA
self._interface = pyvisa.PyVisaInstrument(resource)
self._interface = pyvisa.PyVisaInstrument(resource, **self._pyvisa_opts)
else:
raise IOException('Cannot use resource type %s' % res_type)
elif res_type == 'USB':
# USB connection
if self._prefer_pyvisa and 'pyvisa' in globals():
# connect with PyVISA
self._interface = pyvisa.PyVisaInstrument(resource)
self._interface = pyvisa.PyVisaInstrument(resource, **self._pyvisa_opts)
elif 'usbtmc' in globals():
# connect with USBTMC
self._interface = usbtmc.Instrument(resource)
elif 'pyvisa' in globals():
# connect with PyVISA
self._interface = pyvisa.PyVisaInstrument(resource)
self._interface = pyvisa.PyVisaInstrument(resource, **self._pyvisa_opts)
else:
raise IOException('Cannot use resource type %s' % res_type)
elif res_type == 'GPIB':
# GPIB connection
if self._prefer_pyvisa and 'pyvisa' in globals():
# connect with PyVISA
self._interface = pyvisa.PyVisaInstrument(resource)
self._interface = pyvisa.PyVisaInstrument(resource, **self._pyvisa_opts)
elif 'linuxgpib' in globals():
# connect with linux-gpib
self._interface = linuxgpib.LinuxGpibInstrument(resource)
elif 'pyvisa' in globals():
# connect with PyVISA
self._interface = pyvisa.PyVisaInstrument(resource)
self._interface = pyvisa.PyVisaInstrument(resource, **self._pyvisa_opts)
else:
raise IOException('Cannot use resource type %s' % res_type)
elif res_type == 'ASRL':
# Serial connection
if self._prefer_pyvisa and 'pyvisa' in globals():
# connect with PyVISA
self._interface = pyvisa.PyVisaInstrument(resource)
self._interface = pyvisa.PyVisaInstrument(resource, **self._pyvisa_opts)
elif 'pyserial' in globals():
# connect with PySerial
self._interface = pyserial.SerialInstrument(resource)
elif 'pyvisa' in globals():
# connect with PyVISA
self._interface = pyvisa.PyVisaInstrument(resource)
self._interface = pyvisa.PyVisaInstrument(resource, **self._pyvisa_opts)
else:
raise IOException('Cannot use resource type %s' % res_type)

elif 'pyvisa' in globals():
# connect with PyVISA
self._interface = pyvisa.PyVisaInstrument(resource)
self._interface = pyvisa.PyVisaInstrument(resource, **self._pyvisa_opts)
else:
raise IOException('Unknown resource type %s' % res_type)

Expand Down