8000 expand info/debug/warning msgs if USB device `serial_number` not readable by jllanfranchi · Pull Request #423 · pyvisa/pyvisa-py · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

expand info/debug/warning msgs if USB device serial_number not readable #423

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

Merged
merged 4 commits into from
Apr 1, 2025
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
8 changes: 7 additions & 1 deletion docs/source/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,13 @@ details.

On Unix system, one may have to modify udev rules to allow non-root access to
the device you are trying to connect to. The following tutorial describes how
to do it http://ask.xmodulo.com/change-usb-device-permission-linux.html.
to do it https://www.xmodulo.com/change-usb-device-permission-linux.html.

Note that USBTMC devices show up as both /dev/usbtmcN and /dev/bus/usb/NNN/NNN.
Both instances need to have suitable permissions for pyvisa with pyvisa-py to
be able to communicate. If only the /dev/usbtmcN has permissions then you get::

WARNING Found a device whose serial number cannot be read

On Windows, you may have to uninstall the USBTMC-specific driver installed by
Windows and re-install a generic driver. Please check `libusb's guide`_ for more
Expand Down
39 changes: 36 additions & 3 deletions pyvisa_py/usb.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
"""

import errno
import logging
import os
import sys
import traceback
from typing import Any, List, Tuple, Type, Union

from pyvisa import attributes, constants
Expand Down Expand Up @@ -272,9 +276,9 @@

try:
serial = dev.serial_number
except (NotImplementedError, ValueError):
except (NotImplementedError, ValueError) as err:

Check warning on line 279 in pyvisa_py/usb.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/usb.py#L279

Added line #L279 was not covered by tests
msg = (
"Found a device whose serial number cannot be read."
"Found a USB INSTR device whose serial number cannot be read."
" The partial VISA resource name is: " + fmt
)
LOGGER.warning(
Expand All @@ -287,6 +291,35 @@
"usb_interface_number": intfc,
},
)
logging_level = LOGGER.getEffectiveLevel()

Check warning on line 294 in pyvisa_py/usb.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/usb.py#L294

Added line #L294 was not covered by tests
if logging_level <= logging.DEBUG:
LOGGER.debug("Error while reading serial number", exc_info=err)

Check warning on line 296 in pyvisa_py/usb.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/usb.py#L296

Added line #L296 was not covered by tests
elif logging_level <= logging.INFO:
if exc_strs := traceback.format_exception_only(err):
LOGGER.info(

Check warning on line 299 in pyvisa_py/usb.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/usb.py#L299

Added line #L299 was not covered by tests
"Error raised from underlying module (pyusb): %s",
exc_strs[0].strip(),
)

# Check permissions on Linux
if sys.platform.startswith("linux"):
dev_path = f"/dev/bus/usb/{dev.bus:03d}/{dev.address:03d}"

Check warning on line 306 in pyvisa_py/usb.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/usb.py#L306

Added line #L306 was not covered by tests
if os.path.exists(dev_path) and not os.access(dev_path, os.O_RDWR):
missing_perms = []

Check warning on line 308 in pyvisa_py/usb.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/usb.py#L308

Added line #L308 was not covered by tests
if not os.access(dev_path, os.O_RDONLY):
missing_perms.append("read from")

Check warning on line 310 in pyvisa_py/usb.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/usb.py#L310

Added line #L310 was not covered by tests
if not os.access(dev_path, os.O_WRONLY):
missing_perms.append("write to")
missing_perms_str = " or ".join(missing_perms)
LOGGER.warning(

Check warning on line 314 in pyvisa_py/usb.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/usb.py#L312-L314

Added lines #L312 - L314 were not covered by tests
"User does not have permission to %s %s, so the above "
"USB INSTR device cannot be used by pyvisa; see"
" https://pyvisa.readthedocs.io/projects/pyvisa-py/en/latest/installation.html"
" for more info.",
missing_perms_str,
dev_path,
)

continue

out.append(
Expand Down Expand Up @@ -331,7 +364,7 @@
serial = dev.serial_number
except (NotImplementedError, ValueError, usb.USBError):
msg = (
"Found a device whose serial number cannot be read."
"Found a USB RAW device whose serial number cannot be read."
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can't we run into the same permissions issue here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do. I thought pyvisa didn't care about RAW devices (because it doesn't control them), but I realize now that assumption may be incorrect.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

USB RAW are weird because there are supported by NI but are not part of the IVI specs. If we can provide more information we should though.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I observed in pyvisa issue Linux host and getting permissions to talk to a USBTMC device
USBTMC devices show up as both /dev/usbtmcN and /dev/bus/usb/NNN/NNN
Both instances need to have suitable permissions for pyvisa with pyvisa-py to be able to communicate

If only the /dev/usbtmcN has permissions then you get WARNING Found a device whose serial number cannot be read

" The partial VISA resource name is: " + fmt
)
LOGGER.warning(
Expand Down
Loading
0