Skip to content

Commit

Permalink
Merge pull request #2304 from QCoDeS/pyvisa_1.11_fixes
Browse files Browse the repository at this point in the history
Updates to allow pyvisa 1.11 dependency
  • Loading branch information
jenshnielsen committed Oct 20, 2020
2 parents 728af1b + dd260ca commit bc6c638
Show file tree
Hide file tree
Showing 13 changed files with 46 additions and 59 deletions.
4 changes: 2 additions & 2 deletions docs_requirements.txt
Expand Up @@ -51,8 +51,8 @@ pyparsing==2.4.7
pyrsistent==0.17.3
python-dateutil==2.8.1
pytz==2020.1
PyVISA==1.10.1
PyVISA-sim-QCoDeS @ git+https://github.com/QCoDeS/pyvisa-sim.git@c23b02520e1966cd89c62e93b6db6790cb60f693
PyVISA==1.11.1
PyVISA-sim==0.4.0
pywin32==228; sys_platform == 'win32'
pywinpty==0.5.7; sys_platform == 'win32'
PyYAML==5.3.1
Expand Down
32 changes: 18 additions & 14 deletions qcodes/instrument/visa.py
Expand Up @@ -3,13 +3,14 @@
import warnings
import logging

import visa
import pyvisa as visa
import pyvisa.constants as vi_const
import pyvisa.resources

from .base import Instrument, InstrumentBase

import qcodes.utils.validators as vals
from qcodes.utils.deprecate import deprecate
from qcodes.logger.instrument_logger import get_instrument_logger
from qcodes.utils.delaykeyboardinterrupt import DelayedKeyboardInterrupt

Expand Down Expand Up @@ -52,7 +53,7 @@ def __init__(self, name: str, address: str, timeout: Union[int, float] = 5,
super().__init__(name, **kwargs)
self.visa_log = get_instrument_logger(self, VISA_LOGGER)
self.visabackend: str
self.visa_handle: visa.ResourceManager
self.visa_handle: visa.resources.MessageBasedResource
self.visalib: Optional[str]

self.add_parameter('timeout',
Expand Down Expand Up @@ -119,7 +120,11 @@ def set_address(self, address: str) -> None:
self.visabackend = 'ni'

self.visa_log.info(f'Opening PyVISA resource at address: {address}')
self.visa_handle = resource_manager.open_resource(address)
resouce = resource_manager.open_resource(address)
if not isinstance(resouce, visa.resources.MessageBasedResource):
raise TypeError("QCoDeS only support MessageBasedResource "
"Visa resources")
self.visa_handle = resouce
self._address = address

def device_clear(self) -> None:
Expand All @@ -136,12 +141,10 @@ def device_clear(self) -> None:

if isinstance(self.visa_handle, pyvisa.resources.SerialInstrument):
self.visa_handle.flush(
vi_const.VI_READ_BUF_DISCARD | vi_const.VI_WRITE_BUF_DISCARD)
vi_const.BufferOperation.discard_read_buffer_no_io | vi_const.BufferOperation.discard_write_buffer
)
else:
status_code = self.visa_handle.clear()
if status_code is not None:
self.visa_log.warning(
f"Cleared visa buffer with status code {status_code}")
self.visa_handle.clear()

def set_terminator(self, terminator: str) -> None:
r"""
Expand All @@ -158,10 +161,12 @@ def set_terminator(self, terminator: str) -> None:
if self.visabackend == 'sim':
self.visa_handle.write_termination = terminator

def _set_visa_timeout(self, timeout: Optional[Union[float, int]]) -> None:

def _set_visa_timeout(self, timeout: Optional[float]) -> None:
# according to https://pyvisa.readthedocs.io/en/latest/introduction/resources.html#timeout
# both float('+inf') and None are accepted as meaning infinite timeout
# however None does not pass the typechecking in 1.11.1
if timeout is None:
self.visa_handle.timeout = None
self.visa_handle.timeout = float('+inf')
else:
# pyvisa uses milliseconds but we use seconds
self.visa_handle.timeout = timeout * 1000.0
Expand All @@ -181,10 +186,10 @@ def close(self) -> None:
self.visa_handle.close()
super().close()

@deprecate(reason="pyvisa already checks the error code itself")
def check_error(self, ret_code: int) -> None:
"""
Default error checking, raises an error if return code ``!=0``.
Does not differentiate between warnings or specific error messages.
Override this function in your driver if you want to add specific
error messages.
Expand All @@ -209,8 +214,7 @@ def write_raw(self, cmd: str) -> None:
"""
with DelayedKeyboardInterrupt():
self.visa_log.debug(f"Writing: {cmd}")
nr_bytes_written, ret_code = self.visa_handle.write(cmd)
self.check_error(ret_code)
self.visa_handle.write(cmd)

def ask_raw(self, cmd: str) -> str:
"""
Expand Down
2 changes: 1 addition & 1 deletion qcodes/instrument_drivers/QDev/QDac.py
@@ -1,7 +1,7 @@
# QCoDeS driver for QDac. Based on Alex Johnson's work.

import time
import visa
import pyvisa as visa
import logging
import numpy as np

Expand Down
2 changes: 1 addition & 1 deletion qcodes/instrument_drivers/QDev/QDac_channels.py
@@ -1,7 +1,7 @@
# QCoDeS driver for QDac using channels

import time
import visa
import pyvisa as visa
import logging
import numpy as np

Expand Down
2 changes: 1 addition & 1 deletion qcodes/instrument_drivers/QDevil/QDevil_QDAC.py
Expand Up @@ -4,7 +4,7 @@
# Version 2.1 QDevil 2020-02-10

import time
import visa
import pyvisa as visa
import logging

from functools import partial
Expand Down
Expand Up @@ -3,7 +3,7 @@
import warnings
from time import sleep

from visa import VisaIOError
from pyvisa import VisaIOError
import numpy as np

from qcodes.instrument.visa import VisaInstrument
Expand Down
4 changes: 4 additions & 0 deletions qcodes/instrument_drivers/stahl/stahl.py
Expand Up @@ -9,6 +9,8 @@
from collections import OrderedDict
from functools import partial

from pyvisa.resources.serial import SerialInstrument

from qcodes import VisaInstrument, InstrumentChannel, ChannelList
from qcodes.utils.validators import Numbers

Expand Down Expand Up @@ -144,6 +146,8 @@ class Stahl(VisaInstrument):

def __init__(self, name: str, address: str, **kwargs):
super().__init__(name, address, terminator="\r", **kwargs)
assert isinstance(self.visa_handle, SerialInstrument)

self.visa_handle.baud_rate = 115200

instrument_info = self.parse_idn_string(
Expand Down
2 changes: 1 addition & 1 deletion qcodes/instrument_drivers/tektronix/Keithley_2600.py
Expand Up @@ -35,7 +35,7 @@ def __init__(self, name: str, address: str, channel: str,

self._channel = channel

model = self.visa_handle.ask('print(localnode.model)')
model = self.visa_handle.query('print(localnode.model)')

knownmodels = ['2601B', '2602A', '2602B', '2604B', '2611B', '2612B',
'2614B', '2635B', '2636B']
Expand Down
19 changes: 13 additions & 6 deletions qcodes/tests/test_visa.py
@@ -1,7 +1,8 @@
import warnings

import pytest
import visa
import pyvisa as visa

from qcodes.instrument.visa import VisaInstrument
from qcodes.utils.validators import Numbers

Expand All @@ -19,7 +20,7 @@ def set_address(self, address):
self.visabackend = self.visalib


class MockVisaHandle:
class MockVisaHandle(visa.resources.MessageBasedResource):
"""
mock the API needed for a visa handle that throws lots of errors:
Expand Down Expand Up @@ -51,11 +52,9 @@ def write(self, cmd):
raise ValueError('be more positive!')

if num == 0:
ret_code = visa.constants.VI_ERROR_TMO
else:
ret_code = 0
raise visa.VisaIOError(visa.constants.VI_ERROR_TMO)

return len(cmd), ret_code
return len(cmd)

def ask(self, cmd):
if self.closed:
Expand All @@ -69,6 +68,14 @@ def query(self, cmd):
raise ValueError("I'm out of fingers")
return self.state

def set_visa_attribute(
self, name, state
):
setattr(self, str(name), state)

def __del__(self):
pass


# error args for set(-10)
args1 = [
Expand Down
28 changes: 0 additions & 28 deletions qcodes/tests/test_wrong_address.py

This file was deleted.

2 changes: 1 addition & 1 deletion requirements.txt
Expand Up @@ -61,7 +61,7 @@ pyparsing~=2.4.7
pyrsistent~=0.17.3
python-dateutil~=2.8.1
pytz~=2020.1
PyVISA~=1.10.1
PyVISA~=1.11.1
pywin32==228; sys_platform == 'win32'
pywinpty~=0.5.7; sys_platform == 'win32'
pyzmq~=19.0.2
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -21,7 +21,7 @@ def readme():

install_requires = [
'numpy>=1.10',
'pyvisa>=1.9.1, <1.11',
'pyvisa>=1.9.1, <1.12',
'h5py>=2.6',
'websockets>=7.0',
'jsonschema',
Expand Down
4 changes: 2 additions & 2 deletions test_requirements.txt
Expand Up @@ -30,8 +30,8 @@ pytest-forked==1.3.0
pytest-mock==3.3.1
pytest-rerunfailures==9.1.1
pytest-xdist==2.1.0
PyVISA==1.10.1
PyVISA-sim-QCoDeS @ git+https://github.com/QCoDeS/pyvisa-sim.git@c23b02520e1966cd89c62e93b6db6790cb60f693
PyVISA==1.11.1
PyVISA-sim==0.4.0
PyYAML==5.3.1
requests==2.24.0
six==1.15.0
Expand Down

0 comments on commit bc6c638

Please sign in to comment.