Page 1 of 1

How to check if driver is loaded?

Posted: Mon 6. Jan 2020, 16:55
by Smooth23
Hi all, I'm writing a python script for raspbian that otherwise works fine. The issue I have is the software is broken if the can driver isn't loaded(meaning the usb to can device isn't physically connected to the pi). Quite simply, I wish to put the program on hold until the driver is loaded. How can I check if it is loaded?? I get the error reporting "The driver is not loaded".. full text of error:

ERROR: failed to scan directory (errno=2) '/sys/class/pcan'
ERROR: failed to open file (errno=2) '/sys/class/pcan/version'.
Traceback (most recent call last):
File "redacted", line 11, in <module>
bus = can.interface.Bus(bustype='pcan', channel='PCAN_USBBUS1', bitrate=500000)
File "/home/pi/.local/lib/python3.7/site-packages/can/interface.py", line 127, in __new__
return cls(channel, *args, **kwargs)
File "/home/pi/.local/lib/python3.7/site-packages/can/interfaces/pcan/pcan.py", line 198, in __init__
raise PcanError(self._get_formatted_error(result))
can.interfaces.pcan.pcan.PcanError: The driver is not loaded

Re: How to check if driver is loaded?

Posted: Tue 7. Jan 2020, 08:38
by PEAK-Support
you could check up in front of your Python script if the device is connected. Type lsusb in command line and see if the Device with our Vendor ID is listed.
lsusbJPG.JPG
lsusbJPG.JPG (27.38 KiB) Viewed 4922 times
You could also do this in a self written Python script.

Code: Select all

import re
import subprocess
device_re = re.compile("Bus\s+(?P<bus>\d+)\s+Device\s+(?P<device>\d+).+ID\s(?P<id>\w+:\w+)\s(?P<tag>.+)$", re.I)
df = subprocess.check_output("lsusb")
devices = []
for i in df.split('\n'):
    if i:
        info = device_re.match(i)
        if info:
            dinfo = info.groupdict()
            dinfo['device'] = '/dev/bus/usb/%s/%s' % (dinfo.pop('bus'), dinfo.pop('device'))
            devices.append(dinfo)
print devices
the result could look like this (no PEAK Interface is plugged in)

Code: Select all

[
{'device': '/dev/bus/usb/001/009', 'tag': 'Apple, Inc. Optical USB Mouse [Mitsumi]', 'id': '05ac:0304'},
{'device': '/dev/bus/usb/001/001', 'tag': 'Linux Foundation 2.0 root hub', 'id': '1d6b:0002'},
{'device': '/dev/bus/usb/001/002', 'tag': 'Intel Corp. Integrated Rate Matching Hub', 'id': '8087:0020'},
{'device': '/dev/bus/usb/001/004', 'tag': 'Microdia ', 'id': '0c45:641d'}
]
(Source: StackOverflow)

Re: How to check if driver is loaded?

Posted: Tue 7. Jan 2020, 15:15
by Smooth23
Thank you, this points me in the right direction