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

gs_usb command-line support (and documentation updates and stability fixes) #1790

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

BenGardiner
Copy link

with these changes it is now possible to use the gs_usb driver with e.g. can.logger from the command line (like many other interfaces) with python -m can.logger -i gs_usb -c 0 -b 500000

if index is None and address is None and bus is None:
index = channel

self.index = None
Copy link
Collaborator

Choose a reason for hiding this comment

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

Make these additional index and bitrate attributes private (add '_'-prefix).

devs = GsUsb.scan()
gs_usb = devs[self.index]
try:
gs_usb.set_bitrate(self.bitrate)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this really necessary? Seems weird to call these methods in shutdown(). The comment does not really explain much.

Copy link
Author

Choose a reason for hiding this comment

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

Yeah... it unfortunately is. Right now every second time python-can gs_usb interface tries to use the device there is a USBError thrown.

Traceback (most recent call last):
  File "C:\Users\Ben Gardiner\src\python-can\can\interfaces\gs_usb.py", line 65, in __init__
    self.gs_usb.start()
  File "C:\Users\Public\miniconda3\Lib\site-packages\gs_usb\gs_usb.py", line 50, in start
    self.gs_usb.reset()
  File "C:\Users\Public\miniconda3\Lib\site-packages\usb\core.py", line 961, in reset
    self._ctx.backend.reset_device(self._ctx.handle)
  File "C:\Users\Public\miniconda3\Lib\site-packages\usb\backend\libusb0.py", line 618, in reset_device
    _check(_lib.usb_reset(dev_handle))
  File "C:\Users\Public\miniconda3\Lib\site-packages\usb\backend\libusb0.py", line 447, in _check
    raise USBError(errmsg, ret)
usb.core.USBError: [Errno None] b'libusb0-dll:err [reset] could not reset device, win error: A device attached to the system is not functioning.\r\n\n'

I don't fully understand why though so the hamfisted way to fix it is to do the second open (start) that we know will fail in the shutdown method.

@@ -8,13 +8,14 @@ and candleLight USB CAN interfaces.

Install: ``pip install "python-can[gs_usb]"``

Usage: pass device ``index`` (starting from 0) if using automatic device detection:
Usage: pass device ``index`` or ``channel`` (starting from 0) if using automatic device detection:

::

import can

bus = can.Bus(interface="gs_usb", channel=dev.product, index=0, bitrate=250000)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Not related to your change, but dev is referenced before assignment here. Maybe you could improve this, too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants