0

I want to give exclusive access to the input coming from a LASER barcode (point and trigger type) reader to a Python 3.6 program; that is, no other program should get input from the reader, regardless of UI focus (because the python program has already claimed the device for itself only.)

I've been able to this just fine on a Linux machine using Python 3.6 and pyusb library, but I can't replicate the funcionality in macOS, I get an error stating that I have insufficient permissions.

This actually happens in Linux too but it is easily worked-around by adding the current user to dialout group and creating udev rules file in /etc/udev/rules.d/99-usb.rules granting permissions to a user or group with this rule:

SUBSYSTEMS=="usb", ENV{DEVTYPE}=="usb_device", GROUP="dialout", MODE="0666"

How can equivalent permissions be granted on macOS?

Example code does exatcly what I want on Linux, but fails completely on macOS:

import usb.core
import usb.util
device = usb.core.find(idVendor=0x0519, idProduct=0x2017)
if device.is_kernel_driver_active(0):
    device.detach_kernel_driver(0)
    print("Kernel driver detached")
else:
    print("Kernel driver already detached")
try:
    device.set_configuration()
    device.reset()
    usb.util.claim_interface(device, 0) # error happens here
    print("Claimed device")
except Exception as e:
    print("Error when claiming device", e)
    sys.exit(1)

From what I've found on the Web, It doesn't seem like macOS has an equivalent mechanism to grant this access.

Error on macOS:

[Errno 13] Access denied (insufficient permissions)
Jonathan Acosta
  • 89
  • 1
  • 10
  • And if you run with `sudo YourScript` ? – Mark Setchell Jan 11 '19 at 16:48
  • Since it is an HID device, it is necessary to take measures related to the kernel, and if it is changed to a serial port device, will it automatically become an exclusive use device? – kunif Jan 12 '19 at 04:09
  • I forgot to mention this will have to run as a service that people won't have direct access to, so sudoing or running as root is not really an option. – Jonathan Acosta Jan 14 '19 at 16:25
  • @kunif how can it be turned from an HID to a serial port device? I'm willing to try different approaches to access the reader, just need some more pointers. – Jonathan Acosta Jan 14 '19 at 16:27
  • @Jonathan Acosta, For most scanners, setting barcode for mode change is written in the user manual etc. Please use it to change from keyboard input mode to serial port mode. – kunif Jan 14 '19 at 16:40
  • I think my scanner doesn't support that mode, I read the manual cover to cover and just found a whole bunch of configs to enable and disable barcode-related stuff :-S – Jonathan Acosta Jan 14 '19 at 19:49
  • @Jonathan Acosta, In that case, you could either create an exclusive control program that you think or switch to a scanner that supports serial port mode. – kunif Jan 16 '19 at 04:10
  • I contacted the manufacturer and they confirmed that my scanner doesn't support the mode we're talking about. They said a higher tier model, which is Blue Tooth, supports a mode called *SPP* but I'm not sure if that would solve my requirements. – Jonathan Acosta Jan 17 '19 at 15:41
  • @Jonathan Acosta, Since SPP is Serial Port Protocol (or Profile), I think that it can be used. However, since bluetooth is wireless, it is necessary to pay attention to the connection state and battery charge state. – kunif Jan 20 '19 at 15:15
  • Thanks @kunif. The model I''ve been using is actually wireless too, just not bluetooth. Again, thanks a lot for yout help! :-) – Jonathan Acosta Jan 21 '19 at 16:07

1 Answers1

0

From what I've learned since this question was originally posted, you can reserve input from any USB device, as long as it supports "serial mode", if your device can be switched to this mode then the OS can treat it differently from the default HID mode, and you'll be able to grab it's input, even exclusively.

Disclaimer:

I havent' had a chance to fully test this as of posting this answer. Just wanted to give a quick update to any one interested.

Jonathan Acosta
  • 89
  • 1
  • 10