Skip to content

Commit

Permalink
updated to support partial usbtmc message reads from device to suppor…
Browse files Browse the repository at this point in the history
…t IEEE 488.2 arbitrary block reads fixes the requested data to be send in the header instead of recv_chunk.
  • Loading branch information
Jimmyvandenbergh committed Nov 21, 2024
1 parent 4a77dd0 commit 7fc0e7e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
2 changes: 2 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ PyVISA-py Changelog
has been read (see specification), and only expects a header on the first packet received.
- fix usbtmc implementation to properly discard the alignment bytes
ensuring only the actual data (`transfer_size`) is retained in the message PR #465
- Implemented partial USBTMC message functionality that allows reading the amount of bytes
specified by host.
- add support for VI_ATTR_SUPPRESS_END_EN for USB resources PR #449

0.7.2 (07/03/2024)
Expand Down
21 changes: 10 additions & 11 deletions pyvisa_py/protocols/usbtmc.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,13 +455,6 @@ def write(self, data):
return size

def read(self, size):
header_size = 12
max_padding = 511
recv_chunk = self.usb_recv_ep.wMaxPacketSize - header_size

if size > 0 and size < recv_chunk:
recv_chunk = size

eom = False

raw_read = super(USBTMC, self).read
Expand All @@ -473,12 +466,12 @@ def read(self, size):
received_transfer = bytearray()
self._btag = (self._btag % 255) + 1

req = BulkInMessage.build_array(self._btag, recv_chunk, None)
req = BulkInMessage.build_array(self._btag, size, None)

Check warning on line 469 in pyvisa_py/protocols/usbtmc.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/protocols/usbtmc.py#L469

Added line #L469 was not covered by tests

raw_write(req)

try:
resp = raw_read(recv_chunk + header_size + max_padding)
resp = raw_read(self.usb_recv_ep.wMaxPacketSize)

Check warning on line 474 in pyvisa_py/protocols/usbtmc.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/protocols/usbtmc.py#L474

Added line #L474 was not covered by tests
response = BulkInMessage.from_bytes(resp)
received_transfer.extend(response.data)
while (
Expand All @@ -491,15 +484,21 @@ def read(self, size):
# is sent (one whose length is less than wMaxPacketSize)
# wMaxPacketSize may be incorrectly reported by certain drivers.
# Therefore, continue reading until the transfer_size is reached.
resp = raw_read(recv_chunk + header_size + max_padding)
resp = raw_read(self.usb_recv_ep.wMaxPacketSize)

Check warning on line 487 in pyvisa_py/protocols/usbtmc.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/protocols/usbtmc.py#L487

Added line #L487 was not covered by tests
received_transfer.extend(resp)

# Detect EOM only when device sends all expected bytes.
if len(received_transfer) >= response.transfer_size:
eom = response.transfer_attributes & 1
if not eom and len(received_transfer) >= size:
# Read asking for 'size' bytes from the device.
# This may be less then the device wants to send back in a message
# Therefore the request does not mean that we must receive a EOM.
# Multiple `transfers` will be required to retrieve the remaining bytes.
eom = True

Check warning on line 498 in pyvisa_py/protocols/usbtmc.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/protocols/usbtmc.py#L498

Added line #L498 was not covered by tests
# Truncate data to the specified length (discard padding)
# USBTMC header (12 bytes) has already truncated
received_message.extend(received_transfer[: response.transfer_size])
received_message.extend(received_transfer[: response.transfer_size])

Check warning on line 501 in pyvisa_py/protocols/usbtmc.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/protocols/usbtmc.py#L501

Added line #L501 was not covered by tests
except (usb.core.USBError, ValueError):
# Abort failed Bulk-IN operation.
self._abort_bulk_in(self._btag)
Expand Down

0 comments on commit 7fc0e7e

Please sign in to comment.