From df1b832f39092eb8bf244538351dc19fa5247f8e Mon Sep 17 00:00:00 2001 From: sohail chatoor Date: Wed, 25 Apr 2018 19:13:03 +0200 Subject: [PATCH 01/17] added the RUDAT 13G 90 driver --- .../Minicircuits/RUDAT_13G_90.py | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 qcodes/instrument_drivers/Minicircuits/RUDAT_13G_90.py diff --git a/qcodes/instrument_drivers/Minicircuits/RUDAT_13G_90.py b/qcodes/instrument_drivers/Minicircuits/RUDAT_13G_90.py new file mode 100644 index 000000000000..7a273f756e77 --- /dev/null +++ b/qcodes/instrument_drivers/Minicircuits/RUDAT_13G_90.py @@ -0,0 +1,65 @@ +import os + +try: + import clr +except ImportError: + raise ImportError("""Module clr not found. Please obtain it by + running 'pip install pythonnet' + in a qcodes environment terminal""") + +from qcodes import Instrument + + +class RUDAT_13G_90(Instrument): + + PATH_TO_DRIVER = r'mcl_RUDAT64' + + def __init__(self, name, driver_path=None, serial_number=None, **kwargs): + super().__init__(name, **kwargs) + + if os.name != 'nt': + raise ImportError("""This driver only works in Windows.""") + try: + if driver_path is None: + clr.AddReference(self.PATH_TO_DRIVER) + else: + clr.AddReference(driver_path) + except ImportError: + raise ImportError( + f"""Load of {self.PATH_TO_DRIVER}.dll not possible. Make sure + the dll file is not blocked by Windows. To unblock right-click + the dll to open proporties and check the 'unblock' checkmark + in the bottom. Check that your python installation is 64bit.""" + ) + + import mcl_RUDAT64 + self._instrument = mcl_RUDAT64.USB_RUDAT() + + self._serial_number = serial_number + if self._serial_number is None: + ret = self._instrument.Get_Available_SN_List("") + self._serial_number = ret[1] + + self._instrument.Connect(serial_number) + print("connected to ", self.get_idn()) + + self.add_parameter( + "attenuation", + set_cmd=":SETATT={}", + get_cmd=":ATT?", + get_parser=float + ) + + def _send_scpi(self, string): + return self._instrument.Send_SCPI(string, "") + + def write_raw(self, cmd): + self._send_scpi(cmd) + + def ask_raw(self, cmd): + ret = self._send_scpi(cmd) + return ret[1] + + def get_idn(self): + model_name = self.ask(":MN?") + return f"{model_name} SN: {self._serial_number}" From d16cee157f82b12759b06bb45aee96d80eeab03e Mon Sep 17 00:00:00 2001 From: sohail chatoor Date: Thu, 26 Apr 2018 18:14:14 +0200 Subject: [PATCH 02/17] the new driver uses usbhid instead of the binary dll from microcircuits: Thanks Dominik! --- .../Minicircuits/RUDAT_13G_90.py | 135 ++++++++++++------ 1 file changed, 91 insertions(+), 44 deletions(-) diff --git a/qcodes/instrument_drivers/Minicircuits/RUDAT_13G_90.py b/qcodes/instrument_drivers/Minicircuits/RUDAT_13G_90.py index 7a273f756e77..2841375c1c08 100644 --- a/qcodes/instrument_drivers/Minicircuits/RUDAT_13G_90.py +++ b/qcodes/instrument_drivers/Minicircuits/RUDAT_13G_90.py @@ -1,47 +1,30 @@ -import os - -try: - import clr -except ImportError: - raise ImportError("""Module clr not found. Please obtain it by - running 'pip install pythonnet' - in a qcodes environment terminal""") +import pywinusb.hid as hid +import time +import struct from qcodes import Instrument class RUDAT_13G_90(Instrument): + vendor_id = 0x20ce + product_id = 0x0023 - PATH_TO_DRIVER = r'mcl_RUDAT64' + def __init__(self, name, timeout=2): + super().__init__(name) + self._timeout = timeout - def __init__(self, name, driver_path=None, serial_number=None, **kwargs): - super().__init__(name, **kwargs) + devs = hid.HidDeviceFilter( + product_id=RUDAT_13G_90.product_id, + vendor_id=RUDAT_13G_90.vendor_id + ).get_devices() - if os.name != 'nt': - raise ImportError("""This driver only works in Windows.""") - try: - if driver_path is None: - clr.AddReference(self.PATH_TO_DRIVER) - else: - clr.AddReference(driver_path) - except ImportError: - raise ImportError( - f"""Load of {self.PATH_TO_DRIVER}.dll not possible. Make sure - the dll file is not blocked by Windows. To unblock right-click - the dll to open proporties and check the 'unblock' checkmark - in the bottom. Check that your python installation is 64bit.""" - ) - - import mcl_RUDAT64 - self._instrument = mcl_RUDAT64.USB_RUDAT() - - self._serial_number = serial_number - if self._serial_number is None: - ret = self._instrument.Get_Available_SN_List("") - self._serial_number = ret[1] - - self._instrument.Connect(serial_number) - print("connected to ", self.get_idn()) + if len(devs) == 0: + raise RuntimeError("No instruments found!") + + self._data_buffer = None + self._device = devs[0] # We assume we only have one instrument + self._device.open() + self._device.set_raw_data_handler(self._handler) self.add_parameter( "attenuation", @@ -50,16 +33,80 @@ def __init__(self, name, driver_path=None, serial_number=None, **kwargs): get_parser=float ) - def _send_scpi(self, string): - return self._instrument.Send_SCPI(string, "") + self.add_parameter( + "startup_attenuation", + set_cmd=":STARTUPATT:VALUE:{}", + get_cmd=":STARTUPATT:VALUE?", + get_parser=float + ) + + self.add_parameter( + "hop_points", + get_cmd="HOP:POINTS?", + get_parser=int + ) + + print("connected to ", self.get_idn()) + + def _handler(self, data): + self._data_buffer = data + + def _get_data_buffer(self): + data = self._data_buffer + self._data_buffer = None + return data + + def _get_device_reply(self): + tries_per_second = 5 + number_of_tries = tries_per_second * self._timeout + + response = None + for _ in range(number_of_tries): + time.sleep(1 / tries_per_second) + response = self._get_data_buffer() + if response is not None: + break + + if response is None: + raise TimeoutError("") + + reply_string = "" + for char in response[2:]: + if char == 0x00: + break + + reply_string += chr(char) + + return reply_string + + def get_model(self): + data = struct.pack("BB65x", 0, 40) + self._device.send_output_report(data) + return self._get_device_reply() + + def get_serial(self): + data = struct.pack("BB65x", 0, 41) + self._device.send_output_report(data) + return self._get_device_reply() + + def get_idn(self): + model_name = self.get_model() + serial_number = self.get_serial() + return f"{model_name} SN: {serial_number}" + + def _send_scpi(self, scpi_str): + n = len(scpi_str) + m = 65 - n + data = struct.pack(f"BB{n}s{m}x", 0, 1, scpi_str.encode("ascii")) + self._device.send_output_report(data) + + try: + return self._get_device_reply() + except TimeoutError: + raise TimeoutError(f"Timeout while sending command {scpi_str}") def write_raw(self, cmd): self._send_scpi(cmd) def ask_raw(self, cmd): - ret = self._send_scpi(cmd) - return ret[1] - - def get_idn(self): - model_name = self.ask(":MN?") - return f"{model_name} SN: {self._serial_number}" + return self._send_scpi(cmd) From a3a27f6e7219f2c6d5cdc67e46d79ba3be9aabe1 Mon Sep 17 00:00:00 2001 From: sohail chatoor Date: Mon, 30 Apr 2018 12:40:48 +0200 Subject: [PATCH 03/17] refactored usb specific code in a mixin class --- .../Minicircuits/RUDAT_13G_90.py | 100 ++++++------------ .../Minicircuits/USBHIDMixin.py | 85 +++++++++++++++ 2 files changed, 120 insertions(+), 65 deletions(-) create mode 100644 qcodes/instrument_drivers/Minicircuits/USBHIDMixin.py diff --git a/qcodes/instrument_drivers/Minicircuits/RUDAT_13G_90.py b/qcodes/instrument_drivers/Minicircuits/RUDAT_13G_90.py index 2841375c1c08..28555049cd13 100644 --- a/qcodes/instrument_drivers/Minicircuits/RUDAT_13G_90.py +++ b/qcodes/instrument_drivers/Minicircuits/RUDAT_13G_90.py @@ -1,30 +1,34 @@ -import pywinusb.hid as hid -import time import struct from qcodes import Instrument +from .USBHIDMixin import USBHIDMixin -class RUDAT_13G_90(Instrument): + +class RUDAT_13G_90(USBHIDMixin, Instrument): + packet_size = 64 vendor_id = 0x20ce product_id = 0x0023 - def __init__(self, name, timeout=2): - super().__init__(name) - self._timeout = timeout + def __init__(self, name, instance_id=None, timeout=2, *args, **kwargs): - devs = hid.HidDeviceFilter( - product_id=RUDAT_13G_90.product_id, - vendor_id=RUDAT_13G_90.vendor_id - ).get_devices() + super().__init__( + name=name, + instance_id=instance_id, + timeout=timeout, + *args, + **kwargs + ) - if len(devs) == 0: - raise RuntimeError("No instruments found!") + self.add_parameter( + "model_name", + get_cmd=":MN?" + ) - self._data_buffer = None - self._device = devs[0] # We assume we only have one instrument - self._device.open() - self._device.set_raw_data_handler(self._handler) + self.add_parameter( + "serial_number", + get_cmd=":SN?" + ) self.add_parameter( "attenuation", @@ -46,65 +50,31 @@ def __init__(self, name, timeout=2): get_parser=int ) - print("connected to ", self.get_idn()) + self._usb_endpoint = 0 + self._end_of_message = 0x00 - def _handler(self, data): - self._data_buffer = data - - def _get_data_buffer(self): - data = self._data_buffer - self._data_buffer = None - return data - - def _get_device_reply(self): - tries_per_second = 5 - number_of_tries = tries_per_second * self._timeout - - response = None - for _ in range(number_of_tries): - time.sleep(1 / tries_per_second) - response = self._get_data_buffer() - if response is not None: - break + def _send_scpi(self, scpi_str): + str_len = len(scpi_str) + command_number = 1 + packed_data = struct.pack( + f"B{str_len}s", command_number, scpi_str.encode("ascii") + ) - if response is None: - raise TimeoutError("") + try: + response = self.ask_hid(self._usb_endpoint, packed_data) + except TimeoutError: + raise TimeoutError(f"Timeout while sending command {scpi_str}") reply_string = "" - for char in response[2:]: - if char == 0x00: + # clip the first byte off because it reflects the command + for char in response[1:]: + if char == self._end_of_message: break reply_string += chr(char) return reply_string - def get_model(self): - data = struct.pack("BB65x", 0, 40) - self._device.send_output_report(data) - return self._get_device_reply() - - def get_serial(self): - data = struct.pack("BB65x", 0, 41) - self._device.send_output_report(data) - return self._get_device_reply() - - def get_idn(self): - model_name = self.get_model() - serial_number = self.get_serial() - return f"{model_name} SN: {serial_number}" - - def _send_scpi(self, scpi_str): - n = len(scpi_str) - m = 65 - n - data = struct.pack(f"BB{n}s{m}x", 0, 1, scpi_str.encode("ascii")) - self._device.send_output_report(data) - - try: - return self._get_device_reply() - except TimeoutError: - raise TimeoutError(f"Timeout while sending command {scpi_str}") - def write_raw(self, cmd): self._send_scpi(cmd) diff --git a/qcodes/instrument_drivers/Minicircuits/USBHIDMixin.py b/qcodes/instrument_drivers/Minicircuits/USBHIDMixin.py new file mode 100644 index 000000000000..9d0170d2a2df --- /dev/null +++ b/qcodes/instrument_drivers/Minicircuits/USBHIDMixin.py @@ -0,0 +1,85 @@ +""" +A mixin module for USB Human Interface Device instruments +""" +import pywinusb.hid as hid +import time +import struct + + +class USBHIDMixin: + # The following class attributes are set by subclasses + packet_size = 0 + vendor_id = 0x0000 + product_id = 0x0000 + + def __init__(self, instance_id=None, timeout=2, *args, **kwargs): + super().__init__(*args, **kwargs) + + devs = hid.HidDeviceFilter( + product_id=self.product_id, + vendor_id=self.vendor_id, + instance_id=instance_id + ).get_devices() + + if len(devs) == 0: + raise RuntimeError("No instruments found!") + elif len(devs) > 1: + raise RuntimeError("Multiple HID devices detected! Please supply " + "a instance id") + + self._device = devs[0] # We assume we only have one instrument + self._device.open() + self._data_buffer = None + self._timeout = timeout + self._device.set_raw_data_handler(self._handler) + + def _handler(self, data): + self._data_buffer = data + + def _get_data_buffer(self): + data = self._data_buffer + self._data_buffer = None + return data + + def send_hid(self, feature_id, data): + data_len = len(data) + pad_len = self.packet_size - data_len + + if pad_len < 0: + raise ValueError(f"Length of data exceeds {self.packet_size} B") + + packed_data = struct.pack(f"B{data_len}s{pad_len}x", feature_id, data) + result = self._device.send_output_report(packed_data) + if not result: + raise RuntimeError("Communication with device failed") + + def ask_hid(self, feature_id, data): + self.send_hid(feature_id, data) + + tries_per_second = 5 + number_of_tries = tries_per_second * self._timeout + + response = None + for _ in range(number_of_tries): + time.sleep(1 / tries_per_second) + response = self._get_data_buffer() + if response is not None: + break + + if response is None: + raise TimeoutError("") + # The first bit is the feature id + return response[1:] + + @classmethod + def enumerate_devices(cls): + """ + This method returns the 'instance_id's of all connected decives for with + the given product and vendor IDs. + """ + devs = hid.HidDeviceFilter( + porduct_id=cls.product_id, + vendor_id=cls.vendor_id + ).get_devices() + + return [dev.instance_id for dev in devs] From cdfba4015d450d43c806aee9bc4eac3f4ce82c18 Mon Sep 17 00:00:00 2001 From: sohail chatoor Date: Mon, 30 Apr 2018 14:54:38 +0200 Subject: [PATCH 04/17] added comments, docstrings and type hints. --- .../Minicircuits/RUDAT_13G_90.py | 42 ++++++++++++++++-- .../Minicircuits/USBHIDMixin.py | 43 +++++++++++++++---- 2 files changed, 72 insertions(+), 13 deletions(-) diff --git a/qcodes/instrument_drivers/Minicircuits/RUDAT_13G_90.py b/qcodes/instrument_drivers/Minicircuits/RUDAT_13G_90.py index 28555049cd13..4ba3e14c5d8a 100644 --- a/qcodes/instrument_drivers/Minicircuits/RUDAT_13G_90.py +++ b/qcodes/instrument_drivers/Minicircuits/RUDAT_13G_90.py @@ -1,4 +1,5 @@ import struct +from typing import Dict from qcodes import Instrument @@ -6,11 +7,21 @@ class RUDAT_13G_90(USBHIDMixin, Instrument): + """ + Args: + name (str) + instance_id (str): The id of the instrument we want to connect. If + there is only one instrument then this is an optional argument. + If we have more then one instrument, quarry their ID's by calling + the class method 'enumerate_devices' + timeout (float): Specify a timeout for this instrument + """ packet_size = 64 vendor_id = 0x20ce product_id = 0x0023 - def __init__(self, name, instance_id=None, timeout=2, *args, **kwargs): + def __init__(self, name: str, instance_id: str=None, timeout: float=2, + *args, **kwargs) ->None: super().__init__( name=name, @@ -30,6 +41,11 @@ def __init__(self, name, instance_id=None, timeout=2, *args, **kwargs): get_cmd=":SN?" ) + self.add_parameter( + "firmware", + get_cmd=":FIRMWARE?" + ) + self.add_parameter( "attenuation", set_cmd=":SETATT={}", @@ -53,7 +69,15 @@ def __init__(self, name, instance_id=None, timeout=2, *args, **kwargs): self._usb_endpoint = 0 self._end_of_message = 0x00 - def _send_scpi(self, scpi_str): + self.connect_message() + + def _send_scpi(self, scpi_str: str) ->str: + """ + Send a SCPI command to the instrument + + Args: + scpi_str (string) + """ str_len = len(scpi_str) command_number = 1 packed_data = struct.pack( @@ -75,8 +99,18 @@ def _send_scpi(self, scpi_str): return reply_string - def write_raw(self, cmd): + def write_raw(self, cmd: str)->None: self._send_scpi(cmd) - def ask_raw(self, cmd): + def ask_raw(self, cmd: str)->str: return self._send_scpi(cmd) + + def get_idn(self) -> Dict[str, str]: + model = self.model_name() + serial = self.serial_number() + firmware = self.firmware() + + return {'vendor': 'Mini-Circuits', + 'model': model, + 'serial': serial, + 'firmware': firmware} diff --git a/qcodes/instrument_drivers/Minicircuits/USBHIDMixin.py b/qcodes/instrument_drivers/Minicircuits/USBHIDMixin.py index 9d0170d2a2df..f2d200f7d2b9 100644 --- a/qcodes/instrument_drivers/Minicircuits/USBHIDMixin.py +++ b/qcodes/instrument_drivers/Minicircuits/USBHIDMixin.py @@ -7,12 +7,21 @@ class USBHIDMixin: + """ + Args: + instance_id (str): The id of the instrument we want to connect. If + there is only one instrument then this is an optional argument. + If we have more then one instrument, quarry their ID's by calling + the class method 'enumerate_devices' + timeout (float): Specify a timeout for this instrument + """ # The following class attributes are set by subclasses packet_size = 0 vendor_id = 0x0000 product_id = 0x0000 - def __init__(self, instance_id=None, timeout=2, *args, **kwargs): + def __init__(self, instance_id: str=None, timeout: float=2, *args, + **kwargs) ->None: super().__init__(*args, **kwargs) devs = hid.HidDeviceFilter( @@ -27,21 +36,29 @@ def __init__(self, instance_id=None, timeout=2, *args, **kwargs): raise RuntimeError("Multiple HID devices detected! Please supply " "a instance id") - self._device = devs[0] # We assume we only have one instrument + self._device = devs[0] self._device.open() self._data_buffer = None self._timeout = timeout self._device.set_raw_data_handler(self._handler) - def _handler(self, data): + def _handler(self, data: bytes) ->None: self._data_buffer = data - def _get_data_buffer(self): + def _get_data_buffer(self)->bytearray: data = self._data_buffer self._data_buffer = None return data - def send_hid(self, feature_id, data): + def send_hid(self, feature_id: int, data: bytes): + """ + Send binary data to the human interface device + + Args: + feature_id (int): The 'address' of the device we want to send the + meassage to + data (bytearray) + """ data_len = len(data) pad_len = self.packet_size - data_len @@ -53,11 +70,19 @@ def send_hid(self, feature_id, data): if not result: raise RuntimeError("Communication with device failed") - def ask_hid(self, feature_id, data): + def ask_hid(self, feature_id: int, data: bytes) ->bytes: + """ + Send binary data to the human interface device and wait for a reply + + Args: + feature_id (int): The 'address' of the device we want to send the + meassage to + data (bytearray) + """ self.send_hid(feature_id, data) tries_per_second = 5 - number_of_tries = tries_per_second * self._timeout + number_of_tries = int(tries_per_second * self._timeout) response = None for _ in range(number_of_tries): @@ -74,8 +99,8 @@ def ask_hid(self, feature_id, data): @classmethod def enumerate_devices(cls): """ - This method returns the 'instance_id's of all connected decives for with - the given product and vendor IDs. + This method returns the 'instance_id's of all connected devices for + with the given product and vendor IDs. """ devs = hid.HidDeviceFilter( porduct_id=cls.product_id, From b950fb050c8df50653031bb1ae54feadc64152fc Mon Sep 17 00:00:00 2001 From: sohail chatoor Date: Mon, 30 Apr 2018 15:45:23 +0200 Subject: [PATCH 05/17] added pywinusb to the environment file --- environment.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/environment.yml b/environment.yml index 351724628e35..641ef8f7298b 100644 --- a/environment.yml +++ b/environment.yml @@ -19,3 +19,5 @@ dependencies: - pytest-runner - spyder - pyzmq + - pip: + - pywinusb From 8d7de1271017526c140d145d63853ef060293b3e Mon Sep 17 00:00:00 2001 From: sohail chatoor Date: Mon, 30 Apr 2018 15:57:33 +0200 Subject: [PATCH 06/17] 1) removed pywinusb from the environment file 2) Test that we are under windows when loading this driver. 3) If the pywinusb module is not found, then prompt the user to install this. --- environment.yml | 2 -- .../instrument_drivers/Minicircuits/USBHIDMixin.py | 12 +++++++++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/environment.yml b/environment.yml index 641ef8f7298b..351724628e35 100644 --- a/environment.yml +++ b/environment.yml @@ -19,5 +19,3 @@ dependencies: - pytest-runner - spyder - pyzmq - - pip: - - pywinusb diff --git a/qcodes/instrument_drivers/Minicircuits/USBHIDMixin.py b/qcodes/instrument_drivers/Minicircuits/USBHIDMixin.py index f2d200f7d2b9..52bd6c52fd6f 100644 --- a/qcodes/instrument_drivers/Minicircuits/USBHIDMixin.py +++ b/qcodes/instrument_drivers/Minicircuits/USBHIDMixin.py @@ -1,7 +1,7 @@ """ A mixin module for USB Human Interface Device instruments """ -import pywinusb.hid as hid +import os import time import struct @@ -24,6 +24,16 @@ def __init__(self, instance_id: str=None, timeout: float=2, *args, **kwargs) ->None: super().__init__(*args, **kwargs) + if os.name != 'nt': + raise ImportError("""This driver only works in Windows.""") + + try: + import pywinusb.hid as hid + except ImportError: + raise ImportError("pywinusb is not installed. Please install " + "it by typing 'pip install pywinusb' in a" + "qcodes environment terminal") + devs = hid.HidDeviceFilter( product_id=self.product_id, vendor_id=self.vendor_id, From b82666a6389ea7769a364a750feec7b46abd7b3f Mon Sep 17 00:00:00 2001 From: sohail chatoor Date: Mon, 30 Apr 2018 16:19:29 +0200 Subject: [PATCH 07/17] Instatiating the driver under non-nt should properly raise an error while CI should pass. --- .../Minicircuits/USBHIDMixin.py | 32 +++++++++++++------ 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/qcodes/instrument_drivers/Minicircuits/USBHIDMixin.py b/qcodes/instrument_drivers/Minicircuits/USBHIDMixin.py index 52bd6c52fd6f..506e3631ccb8 100644 --- a/qcodes/instrument_drivers/Minicircuits/USBHIDMixin.py +++ b/qcodes/instrument_drivers/Minicircuits/USBHIDMixin.py @@ -5,6 +5,13 @@ import time import struct +try: + import pywinusb.hid as hid +except ImportError: + # We will raise a proper error when we attempt to instantiate a driver. + # Raising an exception here will cause CI to fail under Linux + hid = None + class USBHIDMixin: """ @@ -20,19 +27,24 @@ class USBHIDMixin: vendor_id = 0x0000 product_id = 0x0000 - def __init__(self, instance_id: str=None, timeout: float=2, *args, - **kwargs) ->None: - super().__init__(*args, **kwargs) + @staticmethod + def _check_hid_import(): if os.name != 'nt': raise ImportError("""This driver only works in Windows.""") + + if hid is None: + + raise ImportError( + "pywinusb is not installed. Please install it by typing " + "'pip install pywinusb' in a qcodes environment terminal" + ) - try: - import pywinusb.hid as hid - except ImportError: - raise ImportError("pywinusb is not installed. Please install " - "it by typing 'pip install pywinusb' in a" - "qcodes environment terminal") + def __init__(self, instance_id: str=None, timeout: float=2, *args, + **kwargs) ->None: + super().__init__(*args, **kwargs) + + self._check_hid_import() devs = hid.HidDeviceFilter( product_id=self.product_id, @@ -112,6 +124,8 @@ def enumerate_devices(cls): This method returns the 'instance_id's of all connected devices for with the given product and vendor IDs. """ + cls._check_hid_import() + devs = hid.HidDeviceFilter( porduct_id=cls.product_id, vendor_id=cls.vendor_id From 4b1b09f79effc9bff3a8b56b34ed051a65ed8ca7 Mon Sep 17 00:00:00 2001 From: sohail chatoor Date: Mon, 30 Apr 2018 16:23:37 +0200 Subject: [PATCH 08/17] Instatiating the driver under non-nt should properly raise an error while CI should pass. --- qcodes/instrument_drivers/Minicircuits/USBHIDMixin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qcodes/instrument_drivers/Minicircuits/USBHIDMixin.py b/qcodes/instrument_drivers/Minicircuits/USBHIDMixin.py index 506e3631ccb8..869abb13fb24 100644 --- a/qcodes/instrument_drivers/Minicircuits/USBHIDMixin.py +++ b/qcodes/instrument_drivers/Minicircuits/USBHIDMixin.py @@ -32,7 +32,7 @@ def _check_hid_import(): if os.name != 'nt': raise ImportError("""This driver only works in Windows.""") - + if hid is None: raise ImportError( From c810c2366c537c842cc8fed42f6e5f0f8f82bea7 Mon Sep 17 00:00:00 2001 From: sohail chatoor Date: Tue, 1 May 2018 11:59:23 +0200 Subject: [PATCH 09/17] Major change: Instead of Mixin class, make a subclass of Instrument --- .../Minicircuits/RUDAT_13G_90.py | 63 +++++++++---------- .../{USBHIDMixin.py => USBHIDInstrument.py} | 41 ++++++------ 2 files changed, 49 insertions(+), 55 deletions(-) rename qcodes/instrument_drivers/Minicircuits/{USBHIDMixin.py => USBHIDInstrument.py} (80%) diff --git a/qcodes/instrument_drivers/Minicircuits/RUDAT_13G_90.py b/qcodes/instrument_drivers/Minicircuits/RUDAT_13G_90.py index 4ba3e14c5d8a..99e639b60bd3 100644 --- a/qcodes/instrument_drivers/Minicircuits/RUDAT_13G_90.py +++ b/qcodes/instrument_drivers/Minicircuits/RUDAT_13G_90.py @@ -1,12 +1,10 @@ import struct from typing import Dict -from qcodes import Instrument +from .USBHIDInstrument import USBHIDInstrument -from .USBHIDMixin import USBHIDMixin - -class RUDAT_13G_90(USBHIDMixin, Instrument): +class RUDAT_13G_90(USBHIDInstrument): """ Args: name (str) @@ -16,20 +14,13 @@ class RUDAT_13G_90(USBHIDMixin, Instrument): the class method 'enumerate_devices' timeout (float): Specify a timeout for this instrument """ - packet_size = 64 vendor_id = 0x20ce product_id = 0x0023 def __init__(self, name: str, instance_id: str=None, timeout: float=2, *args, **kwargs) ->None: - super().__init__( - name=name, - instance_id=instance_id, - timeout=timeout, - *args, - **kwargs - ) + super().__init__(name, instance_id, timeout, *args, **kwargs) self.add_parameter( "model_name", @@ -67,43 +58,45 @@ def __init__(self, name: str, instance_id: str=None, timeout: float=2, ) self._usb_endpoint = 0 - self._end_of_message = 0x00 + self._end_of_message = b"\x00" + self.packet_size = 64 self.connect_message() - def _send_scpi(self, scpi_str: str) ->str: + def _pack_string(self, scpi_str: str) ->bytes: """ - Send a SCPI command to the instrument + Pack a string to a binary format such that it can be send to the + HID. Args: - scpi_str (string) + scpi_str (str) """ str_len = len(scpi_str) + pad_len = self.packet_size - str_len + + if pad_len < 0: + raise ValueError(f"Length of data exceeds {self.packet_size} B") + command_number = 1 packed_data = struct.pack( - f"B{str_len}s", command_number, scpi_str.encode("ascii") + f"BB{str_len}s{pad_len}x", + self._usb_endpoint, command_number, scpi_str.encode("ascii") ) - try: - response = self.ask_hid(self._usb_endpoint, packed_data) - except TimeoutError: - raise TimeoutError(f"Timeout while sending command {scpi_str}") - - reply_string = "" - # clip the first byte off because it reflects the command - for char in response[1:]: - if char == self._end_of_message: - break - - reply_string += chr(char) + return packed_data - return reply_string - - def write_raw(self, cmd: str)->None: - self._send_scpi(cmd) + def _unpack_string(self, response: bytes) ->str: + """ + Unpack data from the instrument to a string - def ask_raw(self, cmd: str)->str: - return self._send_scpi(cmd) + Args: + response (bytes) + """ + usb_end_point, command_number, reply_data = struct.unpack( + f"BB{self.packet_size-1}s", bytes(response) + ) + span = reply_data.find(self._end_of_message) + return str(reply_data[:span]) def get_idn(self) -> Dict[str, str]: model = self.model_name() diff --git a/qcodes/instrument_drivers/Minicircuits/USBHIDMixin.py b/qcodes/instrument_drivers/Minicircuits/USBHIDInstrument.py similarity index 80% rename from qcodes/instrument_drivers/Minicircuits/USBHIDMixin.py rename to qcodes/instrument_drivers/Minicircuits/USBHIDInstrument.py index 869abb13fb24..9312bae11207 100644 --- a/qcodes/instrument_drivers/Minicircuits/USBHIDMixin.py +++ b/qcodes/instrument_drivers/Minicircuits/USBHIDInstrument.py @@ -3,7 +3,6 @@ """ import os import time -import struct try: import pywinusb.hid as hid @@ -12,8 +11,10 @@ # Raising an exception here will cause CI to fail under Linux hid = None +from qcodes import Instrument -class USBHIDMixin: + +class USBHIDInstrument(Instrument): """ Args: instance_id (str): The id of the instrument we want to connect. If @@ -23,7 +24,6 @@ class USBHIDMixin: timeout (float): Specify a timeout for this instrument """ # The following class attributes are set by subclasses - packet_size = 0 vendor_id = 0x0000 product_id = 0x0000 @@ -40,11 +40,11 @@ def _check_hid_import(): "'pip install pywinusb' in a qcodes environment terminal" ) - def __init__(self, instance_id: str=None, timeout: float=2, *args, + def __init__(self, name, instance_id: str=None, timeout: float=2, *args, **kwargs) ->None: - super().__init__(*args, **kwargs) self._check_hid_import() + super().__init__(name, *args, **kwargs) devs = hid.HidDeviceFilter( product_id=self.product_id, @@ -60,19 +60,25 @@ def __init__(self, instance_id: str=None, timeout: float=2, *args, self._device = devs[0] self._device.open() - self._data_buffer = None + self._data_buffer: bytes = None self._timeout = timeout self._device.set_raw_data_handler(self._handler) def _handler(self, data: bytes) ->None: self._data_buffer = data - def _get_data_buffer(self)->bytearray: + def _get_data_buffer(self)->bytes: data = self._data_buffer self._data_buffer = None return data - def send_hid(self, feature_id: int, data: bytes): + def _pack_string(self, scpi_str: str) -> bytes: + raise NotImplementedError("Please subclass") + + def _unpack_string(self, response: bytes) ->str: + raise NotImplementedError("Please subclass") + + def write_raw(self, scpi_str: str): """ Send binary data to the human interface device @@ -81,18 +87,13 @@ def send_hid(self, feature_id: int, data: bytes): meassage to data (bytearray) """ - data_len = len(data) - pad_len = self.packet_size - data_len + data = self._pack_string(scpi_str) - if pad_len < 0: - raise ValueError(f"Length of data exceeds {self.packet_size} B") - - packed_data = struct.pack(f"B{data_len}s{pad_len}x", feature_id, data) - result = self._device.send_output_report(packed_data) + result = self._device.send_output_report(data) if not result: raise RuntimeError("Communication with device failed") - def ask_hid(self, feature_id: int, data: bytes) ->bytes: + def ask_raw(self, scpi_str: str) ->str: """ Send binary data to the human interface device and wait for a reply @@ -101,11 +102,11 @@ def ask_hid(self, feature_id: int, data: bytes) ->bytes: meassage to data (bytearray) """ - self.send_hid(feature_id, data) + self.write_raw(scpi_str) tries_per_second = 5 number_of_tries = int(tries_per_second * self._timeout) - + response = None for _ in range(number_of_tries): time.sleep(1 / tries_per_second) @@ -115,8 +116,8 @@ def ask_hid(self, feature_id: int, data: bytes) ->bytes: if response is None: raise TimeoutError("") - # The first bit is the feature id - return response[1:] + + return self._unpack_string(response) @classmethod def enumerate_devices(cls): From 1ea7301510ed3a7b91349277f7aa26180f2ffd8e Mon Sep 17 00:00:00 2001 From: sohail chatoor Date: Tue, 1 May 2018 12:23:46 +0200 Subject: [PATCH 10/17] pymy finally stops complaining --- .../instrument_drivers/Minicircuits/RUDAT_13G_90.py | 4 ++-- .../Minicircuits/USBHIDInstrument.py | 11 +++++++---- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/qcodes/instrument_drivers/Minicircuits/RUDAT_13G_90.py b/qcodes/instrument_drivers/Minicircuits/RUDAT_13G_90.py index 99e639b60bd3..42bbf25423a1 100644 --- a/qcodes/instrument_drivers/Minicircuits/RUDAT_13G_90.py +++ b/qcodes/instrument_drivers/Minicircuits/RUDAT_13G_90.py @@ -18,9 +18,9 @@ class RUDAT_13G_90(USBHIDInstrument): product_id = 0x0023 def __init__(self, name: str, instance_id: str=None, timeout: float=2, - *args, **kwargs) ->None: + **kwargs) ->None: - super().__init__(name, instance_id, timeout, *args, **kwargs) + super().__init__(name, instance_id, timeout, **kwargs) self.add_parameter( "model_name", diff --git a/qcodes/instrument_drivers/Minicircuits/USBHIDInstrument.py b/qcodes/instrument_drivers/Minicircuits/USBHIDInstrument.py index 9312bae11207..03a6b4e81b13 100644 --- a/qcodes/instrument_drivers/Minicircuits/USBHIDInstrument.py +++ b/qcodes/instrument_drivers/Minicircuits/USBHIDInstrument.py @@ -11,7 +11,7 @@ # Raising an exception here will cause CI to fail under Linux hid = None -from qcodes import Instrument +from qcodes.instrument.base import Instrument class USBHIDInstrument(Instrument): @@ -40,11 +40,11 @@ def _check_hid_import(): "'pip install pywinusb' in a qcodes environment terminal" ) - def __init__(self, name, instance_id: str=None, timeout: float=2, *args, + def __init__(self, name, instance_id: str=None, timeout: float=2, **kwargs) ->None: + super().__init__(name, **kwargs) self._check_hid_import() - super().__init__(name, *args, **kwargs) devs = hid.HidDeviceFilter( product_id=self.product_id, @@ -106,7 +106,7 @@ def ask_raw(self, scpi_str: str) ->str: tries_per_second = 5 number_of_tries = int(tries_per_second * self._timeout) - + response = None for _ in range(number_of_tries): time.sleep(1 / tries_per_second) @@ -119,6 +119,9 @@ def ask_raw(self, scpi_str: str) ->str: return self._unpack_string(response) + def close(self): + self._device.close() + @classmethod def enumerate_devices(cls): """ From f409758e1d2a84ba2762961e0fb6a48e5be67e8f Mon Sep 17 00:00:00 2001 From: sohail chatoor Date: Tue, 1 May 2018 13:18:57 +0200 Subject: [PATCH 11/17] hopefully, the "code quality" is now up to par. --- .../Minicircuits/RUDAT_13G_90.py | 12 ++++++------ .../Minicircuits/USBHIDInstrument.py | 16 ++++++---------- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/qcodes/instrument_drivers/Minicircuits/RUDAT_13G_90.py b/qcodes/instrument_drivers/Minicircuits/RUDAT_13G_90.py index 42bbf25423a1..6da5a73d71ec 100644 --- a/qcodes/instrument_drivers/Minicircuits/RUDAT_13G_90.py +++ b/qcodes/instrument_drivers/Minicircuits/RUDAT_13G_90.py @@ -63,15 +63,15 @@ def __init__(self, name: str, instance_id: str=None, timeout: float=2, self.connect_message() - def _pack_string(self, scpi_str: str) ->bytes: + def _pack_string(self, cmd: str) ->bytes: """ Pack a string to a binary format such that it can be send to the HID. Args: - scpi_str (str) + cmd (str) """ - str_len = len(scpi_str) + str_len = len(cmd) pad_len = self.packet_size - str_len if pad_len < 0: @@ -80,7 +80,7 @@ def _pack_string(self, scpi_str: str) ->bytes: command_number = 1 packed_data = struct.pack( f"BB{str_len}s{pad_len}x", - self._usb_endpoint, command_number, scpi_str.encode("ascii") + self._usb_endpoint, command_number, cmd.encode("ascii") ) return packed_data @@ -92,11 +92,11 @@ def _unpack_string(self, response: bytes) ->str: Args: response (bytes) """ - usb_end_point, command_number, reply_data = struct.unpack( + _, _, reply_data = struct.unpack( f"BB{self.packet_size-1}s", bytes(response) ) span = reply_data.find(self._end_of_message) - return str(reply_data[:span]) + return reply_data[:span].decode("ascii") def get_idn(self) -> Dict[str, str]: model = self.model_name() diff --git a/qcodes/instrument_drivers/Minicircuits/USBHIDInstrument.py b/qcodes/instrument_drivers/Minicircuits/USBHIDInstrument.py index 03a6b4e81b13..cec4ee430094 100644 --- a/qcodes/instrument_drivers/Minicircuits/USBHIDInstrument.py +++ b/qcodes/instrument_drivers/Minicircuits/USBHIDInstrument.py @@ -78,31 +78,27 @@ def _pack_string(self, scpi_str: str) -> bytes: def _unpack_string(self, response: bytes) ->str: raise NotImplementedError("Please subclass") - def write_raw(self, scpi_str: str): + def write_raw(self, cmd: str) ->None: """ Send binary data to the human interface device Args: - feature_id (int): The 'address' of the device we want to send the - meassage to - data (bytearray) + cmd (str) """ - data = self._pack_string(scpi_str) + data = self._pack_string(cmd) result = self._device.send_output_report(data) if not result: raise RuntimeError("Communication with device failed") - def ask_raw(self, scpi_str: str) ->str: + def ask_raw(self, cmd: str) ->str: """ Send binary data to the human interface device and wait for a reply Args: - feature_id (int): The 'address' of the device we want to send the - meassage to - data (bytearray) + cmd (str) """ - self.write_raw(scpi_str) + self.write_raw(cmd) tries_per_second = 5 number_of_tries = int(tries_per_second * self._timeout) From eb1b9b72e06afcacffc2e34a7434d817cb060300 Mon Sep 17 00:00:00 2001 From: sohail chatoor Date: Tue, 1 May 2018 13:26:35 +0200 Subject: [PATCH 12/17] now does CI stop complaining about code quality? --- qcodes/instrument_drivers/Minicircuits/USBHIDInstrument.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/qcodes/instrument_drivers/Minicircuits/USBHIDInstrument.py b/qcodes/instrument_drivers/Minicircuits/USBHIDInstrument.py index cec4ee430094..439bade79e7c 100644 --- a/qcodes/instrument_drivers/Minicircuits/USBHIDInstrument.py +++ b/qcodes/instrument_drivers/Minicircuits/USBHIDInstrument.py @@ -42,6 +42,7 @@ def _check_hid_import(): def __init__(self, name, instance_id: str=None, timeout: float=2, **kwargs) ->None: + #super().__init__(*args, **kwargs) super().__init__(name, **kwargs) self._check_hid_import() @@ -72,7 +73,7 @@ def _get_data_buffer(self)->bytes: self._data_buffer = None return data - def _pack_string(self, scpi_str: str) -> bytes: + def _pack_string(self, cmd: str) -> bytes: raise NotImplementedError("Please subclass") def _unpack_string(self, response: bytes) ->str: From b71ddd206bc0631c87c74ba88a4d1753d7aef21f Mon Sep 17 00:00:00 2001 From: sohail chatoor Date: Tue, 1 May 2018 13:32:31 +0200 Subject: [PATCH 13/17] now does CI stop complaining about code quality? --- qcodes/instrument_drivers/Minicircuits/USBHIDInstrument.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qcodes/instrument_drivers/Minicircuits/USBHIDInstrument.py b/qcodes/instrument_drivers/Minicircuits/USBHIDInstrument.py index 439bade79e7c..428c03212139 100644 --- a/qcodes/instrument_drivers/Minicircuits/USBHIDInstrument.py +++ b/qcodes/instrument_drivers/Minicircuits/USBHIDInstrument.py @@ -73,7 +73,7 @@ def _get_data_buffer(self)->bytes: self._data_buffer = None return data - def _pack_string(self, cmd: str) -> bytes: + def _pack_string(self, cmd: str) ->bytes: raise NotImplementedError("Please subclass") def _unpack_string(self, response: bytes) ->str: From 6e2ace65754b296cb1edb2b73b4d5eb96c2fe37e Mon Sep 17 00:00:00 2001 From: sohail chatoor Date: Tue, 1 May 2018 13:59:37 +0200 Subject: [PATCH 14/17] Reverted to uising a mixin; this turned out to be a superficial change --- qcodes/instrument_drivers/Minicircuits/RUDAT_13G_90.py | 5 +++-- .../Minicircuits/{USBHIDInstrument.py => USBHIDMixin.py} | 3 +-- 2 files changed, 4 insertions(+), 4 deletions(-) rename qcodes/instrument_drivers/Minicircuits/{USBHIDInstrument.py => USBHIDMixin.py} (97%) diff --git a/qcodes/instrument_drivers/Minicircuits/RUDAT_13G_90.py b/qcodes/instrument_drivers/Minicircuits/RUDAT_13G_90.py index 6da5a73d71ec..314593bb553e 100644 --- a/qcodes/instrument_drivers/Minicircuits/RUDAT_13G_90.py +++ b/qcodes/instrument_drivers/Minicircuits/RUDAT_13G_90.py @@ -1,10 +1,11 @@ import struct from typing import Dict -from .USBHIDInstrument import USBHIDInstrument +from .USBHIDMixin import USBHIDMixin +from qcodes.instrument.base import Instrument -class RUDAT_13G_90(USBHIDInstrument): +class RUDAT_13G_90(USBHIDMixin, Instrument): """ Args: name (str) diff --git a/qcodes/instrument_drivers/Minicircuits/USBHIDInstrument.py b/qcodes/instrument_drivers/Minicircuits/USBHIDMixin.py similarity index 97% rename from qcodes/instrument_drivers/Minicircuits/USBHIDInstrument.py rename to qcodes/instrument_drivers/Minicircuits/USBHIDMixin.py index 428c03212139..ed63ad6579bc 100644 --- a/qcodes/instrument_drivers/Minicircuits/USBHIDInstrument.py +++ b/qcodes/instrument_drivers/Minicircuits/USBHIDMixin.py @@ -14,7 +14,7 @@ from qcodes.instrument.base import Instrument -class USBHIDInstrument(Instrument): +class USBHIDMixin(Instrument): """ Args: instance_id (str): The id of the instrument we want to connect. If @@ -42,7 +42,6 @@ def _check_hid_import(): def __init__(self, name, instance_id: str=None, timeout: float=2, **kwargs) ->None: - #super().__init__(*args, **kwargs) super().__init__(name, **kwargs) self._check_hid_import() From 476f7fcada1968701b229ac3d54f077e0f79692b Mon Sep 17 00:00:00 2001 From: sohail chatoor Date: Tue, 1 May 2018 14:35:29 +0200 Subject: [PATCH 15/17] Made a base RUDAT_13G_90 class. To create a USB driver, simply mixin the MiniCircuitsHIDMixin --- .../Minicircuits/RUDAT_13G_90.py | 60 +++-------------- .../Minicircuits/USBHIDMixin.py | 64 ++++++++++++++++++- 2 files changed, 71 insertions(+), 53 deletions(-) diff --git a/qcodes/instrument_drivers/Minicircuits/RUDAT_13G_90.py b/qcodes/instrument_drivers/Minicircuits/RUDAT_13G_90.py index 314593bb553e..b6ebc1e18244 100644 --- a/qcodes/instrument_drivers/Minicircuits/RUDAT_13G_90.py +++ b/qcodes/instrument_drivers/Minicircuits/RUDAT_13G_90.py @@ -1,27 +1,18 @@ -import struct from typing import Dict -from .USBHIDMixin import USBHIDMixin +from .USBHIDMixin import MiniCircuitsHIDMixin from qcodes.instrument.base import Instrument -class RUDAT_13G_90(USBHIDMixin, Instrument): +class RUDAT_13G_90(Instrument): """ Args: name (str) - instance_id (str): The id of the instrument we want to connect. If - there is only one instrument then this is an optional argument. - If we have more then one instrument, quarry their ID's by calling - the class method 'enumerate_devices' - timeout (float): Specify a timeout for this instrument """ - vendor_id = 0x20ce - product_id = 0x0023 - def __init__(self, name: str, instance_id: str=None, timeout: float=2, - **kwargs) ->None: + def __init__(self, name: str, **kwargs) ->None: - super().__init__(name, instance_id, timeout, **kwargs) + super().__init__(name, **kwargs) self.add_parameter( "model_name", @@ -58,47 +49,8 @@ def __init__(self, name: str, instance_id: str=None, timeout: float=2, get_parser=int ) - self._usb_endpoint = 0 - self._end_of_message = b"\x00" - self.packet_size = 64 - self.connect_message() - def _pack_string(self, cmd: str) ->bytes: - """ - Pack a string to a binary format such that it can be send to the - HID. - - Args: - cmd (str) - """ - str_len = len(cmd) - pad_len = self.packet_size - str_len - - if pad_len < 0: - raise ValueError(f"Length of data exceeds {self.packet_size} B") - - command_number = 1 - packed_data = struct.pack( - f"BB{str_len}s{pad_len}x", - self._usb_endpoint, command_number, cmd.encode("ascii") - ) - - return packed_data - - def _unpack_string(self, response: bytes) ->str: - """ - Unpack data from the instrument to a string - - Args: - response (bytes) - """ - _, _, reply_data = struct.unpack( - f"BB{self.packet_size-1}s", bytes(response) - ) - span = reply_data.find(self._end_of_message) - return reply_data[:span].decode("ascii") - def get_idn(self) -> Dict[str, str]: model = self.model_name() serial = self.serial_number() @@ -108,3 +60,7 @@ def get_idn(self) -> Dict[str, str]: 'model': model, 'serial': serial, 'firmware': firmware} + + +class RUDAT_13G_90_USB(MiniCircuitsHIDMixin, RUDAT_13G_90): + pass diff --git a/qcodes/instrument_drivers/Minicircuits/USBHIDMixin.py b/qcodes/instrument_drivers/Minicircuits/USBHIDMixin.py index ed63ad6579bc..7da49a7aa68f 100644 --- a/qcodes/instrument_drivers/Minicircuits/USBHIDMixin.py +++ b/qcodes/instrument_drivers/Minicircuits/USBHIDMixin.py @@ -3,6 +3,7 @@ """ import os import time +import struct try: import pywinusb.hid as hid @@ -43,7 +44,6 @@ def _check_hid_import(): def __init__(self, name, instance_id: str=None, timeout: float=2, **kwargs) ->None: - super().__init__(name, **kwargs) self._check_hid_import() devs = hid.HidDeviceFilter( @@ -64,6 +64,8 @@ def __init__(self, name, instance_id: str=None, timeout: float=2, self._timeout = timeout self._device.set_raw_data_handler(self._handler) + super().__init__(name, **kwargs) + def _handler(self, data: bytes) ->None: self._data_buffer = data @@ -132,3 +134,63 @@ def enumerate_devices(cls): ).get_devices() return [dev.instance_id for dev in devs] + + +class MiniCircuitsHIDMixin(USBHIDMixin): + """ + The specific implementation for mini circuit human interface devices + + Args: + name (str) + instance_id (str): The id of the instrument we want to connect. If + there is only one instrument then this is an optional argument. + If we have more then one instrument, quarry their ID's by calling + the class method 'enumerate_devices' + timeout (float): Specify a timeout for this instrument + """ + vendor_id = 0x20ce + product_id = 0x0023 + + def __init__(self, name: str, instance_id: str=None, timeout: float=2, + **kwargs) ->None: + + self._usb_endpoint = 0 + self._end_of_message = b"\x00" + self.packet_size = 64 + + super().__init__(name, instance_id, timeout, **kwargs) + + def _pack_string(self, cmd: str) ->bytes: + """ + Pack a string to a binary format such that it can be send to the + HID. + + Args: + cmd (str) + """ + str_len = len(cmd) + pad_len = self.packet_size - str_len + + if pad_len < 0: + raise ValueError(f"Length of data exceeds {self.packet_size} B") + + command_number = 1 + packed_data = struct.pack( + f"BB{str_len}s{pad_len}x", + self._usb_endpoint, command_number, cmd.encode("ascii") + ) + + return packed_data + + def _unpack_string(self, response: bytes) ->str: + """ + Unpack data from the instrument to a string + + Args: + response (bytes) + """ + _, _, reply_data = struct.unpack( + f"BB{self.packet_size-1}s", bytes(response) + ) + span = reply_data.find(self._end_of_message) + return reply_data[:span].decode("ascii") \ No newline at end of file From 3ced4ce7e46afed6081ce507c93546991bf5d31a Mon Sep 17 00:00:00 2001 From: sohail chatoor Date: Tue, 1 May 2018 14:38:35 +0200 Subject: [PATCH 16/17] forgot newline --- qcodes/instrument_drivers/Minicircuits/USBHIDMixin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qcodes/instrument_drivers/Minicircuits/USBHIDMixin.py b/qcodes/instrument_drivers/Minicircuits/USBHIDMixin.py index 7da49a7aa68f..19704035d474 100644 --- a/qcodes/instrument_drivers/Minicircuits/USBHIDMixin.py +++ b/qcodes/instrument_drivers/Minicircuits/USBHIDMixin.py @@ -193,4 +193,4 @@ def _unpack_string(self, response: bytes) ->str: f"BB{self.packet_size-1}s", bytes(response) ) span = reply_data.find(self._end_of_message) - return reply_data[:span].decode("ascii") \ No newline at end of file + return reply_data[:span].decode("ascii") From 04fa42053fc1fecd58ed87ac92ed21dd6f09ee46 Mon Sep 17 00:00:00 2001 From: sohail chatoor Date: Tue, 1 May 2018 15:02:40 +0200 Subject: [PATCH 17/17] vendor and product ID's are now set in the driver instead of the mixin --- qcodes/instrument_drivers/Minicircuits/RUDAT_13G_90.py | 3 ++- qcodes/instrument_drivers/Minicircuits/USBHIDMixin.py | 2 -- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/qcodes/instrument_drivers/Minicircuits/RUDAT_13G_90.py b/qcodes/instrument_drivers/Minicircuits/RUDAT_13G_90.py index b6ebc1e18244..e44b749b0d82 100644 --- a/qcodes/instrument_drivers/Minicircuits/RUDAT_13G_90.py +++ b/qcodes/instrument_drivers/Minicircuits/RUDAT_13G_90.py @@ -63,4 +63,5 @@ def get_idn(self) -> Dict[str, str]: class RUDAT_13G_90_USB(MiniCircuitsHIDMixin, RUDAT_13G_90): - pass + vendor_id = 0x20ce + product_id = 0x0023 diff --git a/qcodes/instrument_drivers/Minicircuits/USBHIDMixin.py b/qcodes/instrument_drivers/Minicircuits/USBHIDMixin.py index 19704035d474..b4e417ee47bf 100644 --- a/qcodes/instrument_drivers/Minicircuits/USBHIDMixin.py +++ b/qcodes/instrument_drivers/Minicircuits/USBHIDMixin.py @@ -148,8 +148,6 @@ class MiniCircuitsHIDMixin(USBHIDMixin): the class method 'enumerate_devices' timeout (float): Specify a timeout for this instrument """ - vendor_id = 0x20ce - product_id = 0x0023 def __init__(self, name: str, instance_id: str=None, timeout: float=2, **kwargs) ->None: