python-ivi / python-usbtmc

Provides a USBTMC driver for controlling instruments over USB
MIT License
162 stars 70 forks source link

Stopped working overnight? Appears rules aren't being applied. #57

Open fallingin2infinity opened 3 years ago

fallingin2infinity commented 3 years ago

Hello,

I got python-usbtmc working yesterday, and was having a good time connecting to my Keysight Ocilloscope, running IDN queries, etc. All from my Raspberry Pi 3B+. I followed everything in the readme file, except the optional Kernel bit. But this morning, it's not working!

The output from lsusb:

pi@raspberrypi:~ $ lsusb
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 005: ID 04d9:0007 Holtek Semiconductor, Inc. 
Bus 001 Device 004: ID 093a:2510 Pixart Imaging, Inc. Optical Mouse
Bus 001 Device 023: ID 0957:0588 Agilent Technologies, Inc. 
Bus 001 Device 002: ID 2109:3431 VIA Labs, Inc. Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

My usbtmc.rules file:

# USBTMC instruments

# Agilent DSO1052B
SUBSYSTEM=="usb", ACTION=="add", ATTRS{idVendor}=="0x0957", ATTRS{idProduct}=="0x0588", GROUP="usbtmc", MODE="0660"

My Python3 code:

>>> import usbtmc
>>> inst = usbtmc.Instrument(0x0957, 0x0588)
>>> print(inst.ask("*IDN") 

However, this morning the exact same code is returning:

usb.core.USBError: [Errno 13] Access denied (insufficient permissions)

The Pi was left on overnight, too - so nothing should have restarted, etc. The USB device was disconnected and reconnected in the morning. I didn't change anything, other than I probably re-started Python.

I've gone back through the readme (again, not the optional part) and I'm just not getting anywhere. When plugged in, the device is added to /dev/usbtmc0. Using ls -all, I get:

pi@raspberrypi:/dev $ ls -all | grep usbtmc*
crw-------   1 root root    180, 176 Jun 11 14:13 usbtmc0

Which doesn't look right to me. I've done some udevadm debugging to try and highlight an issue, and I'm seeing this line, which looks wrong, as I'd expect the mode to be picked up from my usbtmc.rules file:

usbtmc0: Handling device node '/dev/usbtmc0', devnum=c180:176, mode=0600, uid=0, gid=0

Does anyone have any thoughts?

chrisjbillington commented 1 year ago

I couldn't get this to work using any variation of SUBSYSTEM=="usb" alone. This worked for me:

echo 'KERNEL=="usbtmc*", MODE="0660", GROUP="plugdev"'  | sudo tee -a /etc/udev/rules.d/10-usbtmc.rules
echo 'SUBSYSTEM=="usb", MODE="0660", GROUP="plugdev"'  | sudo tee -a /etc/udev/rules.d/00-usb-permissions.rules
sudo udevadm control --reload-rules
sudo udevadm trigger

And ensuring the user was a member of the "plugdev" group (they are by default on the Ubuntu I'm using).

I believe this grants permission for all usbtmc devices regardless of vendor ID and product ID, that's fine for me - but you might wanna specify them if you want to restrict it to only one devuce. I didn't test if that works.