Skip to content

Commit

Permalink
Updating to use libusb0 instead of libusb1
Browse files Browse the repository at this point in the history
  • Loading branch information
slightlynybbled committed May 13, 2022
1 parent 3a1a547 commit 1fee842
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 7 deletions.
3 changes: 3 additions & 0 deletions di2008/__init__.py
Expand Up @@ -3,6 +3,9 @@
DigitalDirection
from di2008.version import __version__

import os
os.environ['PYUSB_DEBUG'] = 'debug'

__all__ = ['AnalogPort', 'RatePort', 'CountPort', 'DigitalPort', 'Di2008',
'AnalogPortError', 'DigitalPortError', 'PortNotValidError',
'DigitalDirection',
Expand Down
14 changes: 10 additions & 4 deletions di2008/instrument.py
Expand Up @@ -9,6 +9,7 @@
from time import sleep
from typing import List

import usb.backend.libusb0 as libusb0
import usb.core
import usb.util

Expand All @@ -20,7 +21,10 @@


def _discover_auto() -> 'usb.core.Device':
available_devices = [d for d in usb.core.find(find_all=True, idVendor=0x0683, idProduct=0x2008)]
available_devices = [d for d in usb.core.find(find_all=True,
idVendor=0x0683,
idProduct=0x2008,
backend=libusb0.get_backend())]
if len(available_devices) == 0:
raise AttributeError('there are no DI-2008 devices attached to the PC that are in USB mode')

Expand All @@ -40,13 +44,15 @@ def _discover_auto() -> 'usb.core.Device':

return device


def _discover_by_esn(serial_number: str) -> 'usb.core.Device':
buffering_time = 0.05
correct_device = None

available_devices = [d for d in usb.core.find(find_all=True,
idVendor=0x0683,
idProduct=0x2008)]
idProduct=0x2008,
backend=libusb1.get_backend())]
dev_strings = [str(d) for d in available_devices]
_logger.debug(f'DI-2008 instruments detected on: {", ".join(dev_strings)}')

Expand Down Expand Up @@ -833,10 +839,10 @@ def _run(self):
try:
response = self._device.read(ENDPOINT_BULK_IN, 64, timeout=timeout_ms)
self._parse_received(response)
except usb.core.USBTimeoutError:
except (usb.core.USBTimeoutError, usb.core.USBError):
break
except Exception as e:
print(f'unknown error: {e}')
self._logger.warning(f'unknown error: {e}')
break

self._maintain_send_queue()
Expand Down
2 changes: 1 addition & 1 deletion di2008/version.py
@@ -1 +1 @@
__version__ = '0.4.5'
__version__ = '0.4.7'
2 changes: 1 addition & 1 deletion examples/analog_reads.py
Expand Up @@ -10,11 +10,11 @@
daq = Di2008(loglevel=loglevel)

channels = [
RatePort(5000),
AnalogPort(1, analog_range=10.0, filter='average', loglevel=loglevel),
AnalogPort(2, analog_range=10.0, loglevel=loglevel),
AnalogPort(3, analog_range=10.0, loglevel=loglevel),
AnalogPort(8, analog_range=10.0, loglevel=loglevel),
RatePort(5000),
]

daq.create_scan_list(channels)
Expand Down
24 changes: 24 additions & 0 deletions examples/backend_test.py
@@ -0,0 +1,24 @@
import coloredlogs
import logging
import ctypes

print('ctypes.WinDLL', ctypes.WinDLL('libusb-1.0.dll'))

import os
os.environ['PYUSB_DEBUG'] = 'debug'
# os.environ['PYUSB_LOG_FILENAME'] = 'C:\\_code\\di2008\\examples\\usblog.txt'

coloredlogs.install(level=logging.DEBUG)

import usb.core
import usb.util
import usb.backend.libusb1 as libusb1

devices = [d for d in usb.core.find(find_all=True,
idVendor=0x0683,
idProduct=0x2008)]

[print(d) for d in devices]

device = devices[0]
device.set_configuration()
23 changes: 22 additions & 1 deletion examples/lib_usb_testing.py
@@ -1,15 +1,36 @@
import logging
import os
from time import sleep

import usb.core
import usb.util
import usb.backend.libusb0 as libusb

logging.basicConfig(level=logging.DEBUG)
usb_logger = logging.getLogger('usb.core')
usb_logger.setLevel(logging.DEBUG)

devices = [d for d in usb.core.find(find_all=True, idVendor=0x0683, idProduct=0x2008)]
# for p in os.environ.get('PATH').split(';'):
# print(f'path: "{p}"')

backend = libusb.get_backend()
print('backend:', backend)
devices = [d for d in usb.core.find(find_all=True,
idVendor=0x0683,
idProduct=0x2008,
backend=backend,
)]

print(devices)
device = devices[0]
print(device)
print(type(device))
device.set_configuration(1)

# send "stop" command just in case the device happens to be scanning
device.write(0x1, 'stop')
sleep(0.2)

response = device.read(0x81, 64)
response = ''.join([chr(b) for b in response if b != 0])
print(f'response when nothing was sent: "{response}"')
Expand Down

0 comments on commit 1fee842

Please sign in to comment.