Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update usbtmc read to handle transfer_size and discard alignment bytes #465

Merged
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
6 changes: 4 additions & 2 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ PyVISA-py Changelog

- add read_stb method for TCPIP HiSLIP client PR #429
- fix usbtmc implementation to respect section 3.3 of the spec PR #449
Read now reads from usb until a "short packet" has been read
(see specification), and only expects a header on the first packet received.
Read now reads from usb until a "short packet" or if all data (`transfer_size`) PR #465
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
- add support for VI_ATTR_SUPPRESS_END_EN for USB resources PR #449

0.7.2 (07/03/2024)
Expand Down
27 changes: 18 additions & 9 deletions pyvisa_py/protocols/usbtmc.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,9 +467,10 @@ def read(self, size):
raw_read = super(USBTMC, self).read
raw_write = super(USBTMC, self).write

received = bytearray()
received_message = bytearray()

while not eom:
received_transfer = bytearray()
self._btag = (self._btag % 255) + 1

req = BulkInMessage.build_array(self._btag, recv_chunk, None)
Expand All @@ -479,21 +480,29 @@ def read(self, size):
try:
resp = raw_read(recv_chunk + header_size + max_padding)
response = BulkInMessage.from_bytes(resp)
received.extend(response.data)
while len(resp) == self.usb_recv_ep.wMaxPacketSize:
received_transfer.extend(response.data)
while (
len(resp) == self.usb_recv_ep.wMaxPacketSize
or len(received_transfer) < response.transfer_size
):
# USBTMC Section 3.3 specifies that the first usb packet
# must contain the header. the remaining packets do not need
# the header the message is finished when a "short packet"
# 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)
received.extend(resp)
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
# Truncate data to the specified length (discard padding)
# USBTMC header (12 bytes) has already truncated
received_message.extend(received_transfer[: response.transfer_size])
except (usb.core.USBError, ValueError):
# Abort failed Bulk-IN operation.
self._abort_bulk_in(self._btag)
raise

# Detect EOM only when device sends all expected bytes.
if len(response.data) >= response.transfer_size:
eom = response.transfer_attributes & 1

return bytes(received)
return bytes(received_message)
Loading