Search code examples
pythontypeerrorabstract-classcan-busuds

TypeError: Can't instantiate abstract class


I try to run code to send a UDS extended diagnostics request on CAN

from uds import Uds

PCM = Uds(resId=0x200, reqId=0x250, transportProtocol="CAN", interface="vector", channel="0", appName="CANoe")
response = PCM.send([0x10, 0x03])

But I get following Type error:

Traceback (most recent call last):
  File "C:\__work__\Karol_projects\main.py", line 26, in <module>
    PCM = Uds(resId=0x200, reqId=0x250, transportProtocol="CAN", interface="vector", channel="0", appName="CANoe")
  File "C:\__work__\Karol_projects\venv\lib\site-packages\uds\uds_communications\Uds\Uds.py", line 43, in __init__
    self.tp = tpFactory(self.__transportProtocol, configPath=configPath, **kwargs)
  File "C:\__work__\Karol_projects\venv\lib\site-packages\uds\uds_communications\TransportProtocols\TpFactory.py", line 37, in __call__
    return CanTp(configPath=configPath, **kwargs)
  File "C:\__work__\Karol_projects\venv\lib\site-packages\uds\uds_communications\TransportProtocols\Can\CanTp.py", line 96, in __init__
    self.__connection = canConnectionFactory(self.callback_onReceive,
  File "C:\__work__\Karol_projects\venv\lib\site-packages\uds\uds_communications\TransportProtocols\Can\CanConnectionFactory.py", line 52, in __call__
    CanConnectionFactory.connections[connectionKey] = CanConnection(callback, filter,
  File "C:\__work__\Karol_projects\venv\lib\site-packages\uds\uds_communications\TransportProtocols\Can\CanConnection.py", line 21, in __init__
    listener = can.Listener()
TypeError: Can't instantiate abstract class Listener with abstract method on_message_received

I have seen similar posts but anyway I can't get a proper solution. I only imported python-can and python-uds modules and had tried to send a can message. I don't have much experience with OOP especially with abstract classes.


Solution

  • python-uds is an older library, and hasn't seen new releases in 3 and half years now (version 1.0.2 was released in March 2019). python-can on the other hand, has made major strides in that time, and released a major update, version 4.0.0 in February 2022. That release is fundamentally incompatible with python-uds, causing the traceback you see.

    You have two options:

    • Downgrade python-can; when python-uds was last released, python-can had released version 3.1.1, but it may be that later 3.x.x versions also work. See the python-can release page on PyPI for the full list. You could try reading the python-can changelog to see if something stands out if the last 3.x release, 3.3.4, doesn't work but 3.1.1 does to see what the latest version that works would be. Or, just bisect the versions between 3.3.4 and 3.1.1 until you find the last compatible release.

    • Switch to a different Uds library. The py-uds project has seen more recent releases (version 0.2.0 was released in December last year and their GitHub repository shows active development continued up to February 2022 at least.