Skip to content

usb2can with Python3 dead (fix included) #219

@iurly

Description

@iurly

Hi,
I found a couple of show-stoppers with usb2can using Python3.
Here's my patch:

        # set flags on the connection
        if 'flags' in kwargs:
            enable_flags = kwargs["flags"]

        else:
            enable_flags = 0x00000008

        # code to get the serial number of the device
        if 'serial' in kwargs:
            deviceID = kwargs["serial"]
        elif channel is not None:
            deviceID = channel
        else:
            from can.interfaces.usb2can.serial_selector import serial
            deviceID = serial()

        # set baudrate in kb/s from bitrate
        # (eg:500000 bitrate must be 500)
        if 'bitrate' in kwargs:
            br = kwargs["bitrate"]

            # max rate is 1000 kbps
            baudrate = min(1000, int(br/1000))   ########## <------- This was max(...)
        # set default value
        else:
            baudrate = 500

        connector = format_connection_string(deviceID, baudrate)

        self.handle = self.can.open(connector.encode(), enable_flags)   ########### <------- here .encode() was missing

Apparently in Python 3 ctypes strings must be passed as bytearray, I don't know if this breaks compatibility with Python 2 though. The above works for me.
Oh, and the max() was clearly wrong.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions