From a9459b67381d890efeb0afcc68df4e661dd19cd9 Mon Sep 17 00:00:00 2001 From: Andrea Zanotti Date: Fri, 18 Oct 2019 16:39:20 +0200 Subject: [PATCH 1/2] adding usb_control_in functionality --- pyvisa-py/highlevel.py | 27 +++++++++++++++++++++++++++ pyvisa-py/protocols/usbtmc.py | 28 +++++++++++++++++++++++++++- pyvisa-py/usb.py | 21 +++++++++++++++++++++ 3 files changed, 75 insertions(+), 1 deletion(-) diff --git a/pyvisa-py/highlevel.py b/pyvisa-py/highlevel.py index 9a88c066..51597ffe 100644 --- a/pyvisa-py/highlevel.py +++ b/pyvisa-py/highlevel.py @@ -510,3 +510,30 @@ def disable_event(self, session, event_type, mechanism): def discard_events(self, session, event_type, mechanism): # TODO: implement this for GPIB finalization pass + + def usb_control_in(self, session, request_type_bitmap_field, request_id, request_value, + index, length=0): + """Performs a USB control pipe transfer from the device. + + Corresponds to viUsbControlIn function of the VISA library. + + :param session: Unique logical identifier to a session. + :param request_type_bitmap_field: bmRequestType parameter of the setup stage of a USB control transfer. + :param request_id: bRequest parameter of the setup stage of a USB control transfer. + :param request_value: wValue parameter of the setup stage of a USB control transfer. + :param index: wIndex parameter of the setup stage of a USB control transfer. + This is usually the index of the interface or endpoint. + :param length: wLength parameter of the setup stage of a USB control transfer. + This value also specifies the size of the data buffer to receive the data from the + optional data stage of the control transfer. + :return: - The data buffer that receives the data from the optional data stage of the control transfer + - return value of the library call. + :rtype: - bytes + - :class:`pyvisa.constants.StatusCode` + """ + try: + sess = self.sessions[session] + except KeyError: + return StatusCode.error_invalid_object + + return sess.control_transfer(request_type_bitmap_field, request_id, request_value, index, length) \ No newline at end of file diff --git a/pyvisa-py/protocols/usbtmc.py b/pyvisa-py/protocols/usbtmc.py index 0d1fe290..cfe5533e 100644 --- a/pyvisa-py/protocols/usbtmc.py +++ b/pyvisa-py/protocols/usbtmc.py @@ -248,7 +248,6 @@ def write(self, data): :param data: bytes to be sent to the instrument :type data: bytes """ - try: return self.usb_send_ep.write(data) except usb.core.USBError as e: @@ -431,3 +430,30 @@ def read(self, size): eom = response.transfer_attributes & 1 return bytes(received) + + def control_transfer(self, request_type_bitmap_field, request_id, request_value, + index, length): + """Performs a USB control pipe transfer from the device. + + :param request_type_bitmap_field: bmRequestType parameter of the setup stage of a USB control transfer. + :param request_id: bRequest parameter of the setup stage of a USB control transfer. + :param request_value: wValue parameter of the setup stage of a USB control transfer. + :param index: wIndex parameter of the setup stage of a USB control transfer. + This is usually the index of the interface or endpoint. + :param length: wLength parameter of the setup stage of a USB control transfer. + This value also specifies the size of the data buffer to receive the data from the + optional data stage of the control transfer. + :return: - The data buffer that receives the data from the optional data stage of the control transfer + :rtype: - bytes + """ + + self.usb_dev.ctrl_transfer(request_type_bitmap_field, + request_id, + request_value, + index, + length, + timeout=self.timeout) + + #TODO satisfy the second half of the problem -i.e. sending buffer the optional data stage + # return self.read(length) does not work + return bytes() \ No newline at end of file diff --git a/pyvisa-py/usb.py b/pyvisa-py/usb.py index 7bac90fd..8504ec7f 100644 --- a/pyvisa-py/usb.py +++ b/pyvisa-py/usb.py @@ -144,6 +144,27 @@ def write(self, data): return count, StatusCode.success + def control_transfer(self, request_type_bitmap_field, request_id, request_value, index, length): + """Performs a USB control pipe transfer from the device. + + :param request_type_bitmap_field: bmRequestType parameter of the setup stage of a USB control transfer. + :param request_id: bRequest parameter of the setup stage of a USB control transfer. + :param request_value: wValue parameter of the setup stage of a USB control transfer. + :param index: wIndex parameter of the setup stage of a USB control transfer. + This is usually the index of the interface or endpoint. + :param length: wLength parameter of the setup stage of a USB control transfer. + This value also specifies the size of the data buffer to receive the data from the + optional data stage of the control transfer. + + :return: - The data buffer that receives the data from the optional data stage of the control transfer + - return value of the library call. + :rtype: - bytes + - :class:`pyvisa.constants.StatusCode` + """ + ret_val = self.interface.control_transfer(request_type_bitmap_field, request_id, + request_value, index, length) + return ret_val, StatusCode.success + def close(self): self.interface.close() From 6df1e82f89ed85f86f313ea73b768d54f3ad5485 Mon Sep 17 00:00:00 2001 From: Andrea Zanotti Date: Wed, 20 Nov 2019 11:45:06 +0100 Subject: [PATCH 2/2] adding blank lines at end of file --- pyvisa-py/highlevel.py | 3 ++- pyvisa-py/protocols/usbtmc.py | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/pyvisa-py/highlevel.py b/pyvisa-py/highlevel.py index 51597ffe..18ce4869 100644 --- a/pyvisa-py/highlevel.py +++ b/pyvisa-py/highlevel.py @@ -536,4 +536,5 @@ def usb_control_in(self, session, request_type_bitmap_field, request_id, request except KeyError: return StatusCode.error_invalid_object - return sess.control_transfer(request_type_bitmap_field, request_id, request_value, index, length) \ No newline at end of file + return sess.control_transfer(request_type_bitmap_field, request_id, request_value, index, length) + diff --git a/pyvisa-py/protocols/usbtmc.py b/pyvisa-py/protocols/usbtmc.py index cfe5533e..516d37af 100644 --- a/pyvisa-py/protocols/usbtmc.py +++ b/pyvisa-py/protocols/usbtmc.py @@ -456,4 +456,5 @@ def control_transfer(self, request_type_bitmap_field, request_id, request_value, #TODO satisfy the second half of the problem -i.e. sending buffer the optional data stage # return self.read(length) does not work - return bytes() \ No newline at end of file + return bytes() +