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

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
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/faq.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
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