Пример #1
0
    def __init__(self,
                 channel='PCAN_USBBUS1',
                 state=BusState.ACTIVE,
                 bitrate=500000,
                 *args,
                 **kwargs):
        """A PCAN USB interface to CAN.

        On top of the usual :class:`~can.Bus` methods provided,
        the PCAN interface includes the :meth:`~can.interface.pcan.PcanBus.flash`
        and :meth:`~can.interface.pcan.PcanBus.status` methods.

        :param str channel:
            The can interface name. An example would be 'PCAN_USBBUS1'
            Default is 'PCAN_USBBUS1'

        :param can.bus.BusState state:
            BusState of the channel.
            Default is ACTIVE

        :param int bitrate:
            Bitrate of channel in bit/s.
            Default is 500 kbit/s.

        """
        self.channel_info = channel
        pcan_bitrate = pcan_bitrate_objs.get(bitrate, PCAN_BAUD_500K)

        hwtype = PCAN_TYPE_ISA
        ioport = 0x02A0
        interrupt = 11

        self.m_objPCANBasic = PCANBasic()
        self.m_PcanHandle = globals()[channel]

        if state is BusState.ACTIVE or BusState.PASSIVE:
            self._state = state
        else:
            raise ArgumentError("BusState must be Active or Passive")

        result = self.m_objPCANBasic.Initialize(self.m_PcanHandle,
                                                pcan_bitrate, hwtype, ioport,
                                                interrupt)

        if result != PCAN_ERROR_OK:
            raise PcanError(self._get_formatted_error(result))

        if HAS_EVENTS:
            self._recv_event = CreateEvent(None, 0, 0, None)
            result = self.m_objPCANBasic.SetValue(self.m_PcanHandle,
                                                  PCAN_RECEIVE_EVENT,
                                                  self._recv_event)
            if result != PCAN_ERROR_OK:
                raise PcanError(self._get_formatted_error(result))

        super(PcanBus, self).__init__(channel=channel,
                                      state=state,
                                      bitrate=bitrate,
                                      *args,
                                      **kwargs)
Пример #2
0
    def __init__(self, channel, *args, **kwargs):
        """A PCAN USB interface to CAN.

        On top of the usual :class:`~can.Bus` methods provided,
        the PCAN interface includes the `flash()` and `status()` methods.

        :param str channel:
            The can interface name. An example would be PCAN_USBBUS1

        :param int bitrate:
            Bitrate of channel in bit/s.
            Default is 500 Kbs

        """
        if channel is None or channel == '':
            raise ArgumentError("Must specify a PCAN channel")
        else:
            self.channel_info = channel

        bitrate = kwargs.get('bitrate', 500000)
        pcan_bitrate = pcan_bitrate_objs.get(bitrate, PCAN_BAUD_500K)

        hwtype = PCAN_TYPE_ISA
        ioport = 0x02A0
        interrupt = 11

        self.m_objPCANBasic = PCANBasic()
        self.m_PcanHandle = globals()[channel]

        result = self.m_objPCANBasic.Initialize(self.m_PcanHandle,
                                                pcan_bitrate, hwtype, ioport,
                                                interrupt)

        if result != PCAN_ERROR_OK:
            raise PcanError(self._get_formatted_error(result))

        if HAS_EVENTS:
            self._recv_event = CreateEvent(None, 0, 0, None)
            result = self.m_objPCANBasic.SetValue(self.m_PcanHandle,
                                                  PCAN_RECEIVE_EVENT,
                                                  self._recv_event)
            if result != PCAN_ERROR_OK:
                raise PcanError(self._get_formatted_error(result))

        super(PcanBus, self).__init__(*args, **kwargs)
Пример #3
0
    def __init__(self,
                 channel='PCAN_USBBUS1',
                 state=BusState.ACTIVE,
                 bitrate=500000,
                 *args,
                 **kwargs):
        """A PCAN USB interface to CAN.

        On top of the usual :class:`~can.Bus` methods provided,
        the PCAN interface includes the :meth:`~can.interface.pcan.PcanBus.flash`
        and :meth:`~can.interface.pcan.PcanBus.status` methods.

        :param str channel:
            The can interface name. An example would be 'PCAN_USBBUS1'
            Default is 'PCAN_USBBUS1'

        :param can.bus.BusState state:
            BusState of the channel.
            Default is ACTIVE

        :param int bitrate:
            Bitrate of channel in bit/s.
            Default is 500 kbit/s.
            Ignored if using CanFD.

        :param bool fd:
            Should the Bus be initialized in CAN-FD mode.

        :param int f_clock:
            Clock rate in Hz.
            Any of the following:
            20000000, 24000000, 30000000, 40000000, 60000000, 80000000.
            Ignored if not using CAN-FD.
            Pass either f_clock or f_clock_mhz.

        :param int f_clock_mhz:
            Clock rate in MHz.
            Any of the following:
            20, 24, 30, 40, 60, 80.
            Ignored if not using CAN-FD.
            Pass either f_clock or f_clock_mhz.

        :param int nom_brp:
            Clock prescaler for nominal time quantum.
            In the range (1..1024)
            Ignored if not using CAN-FD.

        :param int nom_tseg1:
            Time segment 1 for nominal bit rate,
            that is, the number of quanta from (but not including)
            the Sync Segment to the sampling point.
            In the range (1..256).
            Ignored if not using CAN-FD.

        :param int nom_tseg2:
            Time segment 2 for nominal bit rate,
            that is, the number of quanta from the sampling
            point to the end of the bit.
            In the range (1..128).
            Ignored if not using CAN-FD.

        :param int nom_sjw:
            Synchronization Jump Width for nominal bit rate.
            Decides the maximum number of time quanta
            that the controller can resynchronize every bit.
            In the range (1..128).
            Ignored if not using CAN-FD.

        :param int data_brp:
            Clock prescaler for fast data time quantum.
            In the range (1..1024)
            Ignored if not using CAN-FD.

        :param int data_tseg1:
            Time segment 1 for fast data bit rate,
            that is, the number of quanta from (but not including)
            the Sync Segment to the sampling point.
            In the range (1..32).
            Ignored if not using CAN-FD.

        :param int data_tseg2:
            Time segment 2 for fast data bit rate,
            that is, the number of quanta from the sampling
            point to the end of the bit.
            In the range (1..16).
            Ignored if not using CAN-FD.

        :param int data_sjw:
            Synchronization Jump Width for fast data bit rate.
            Decides the maximum number of time quanta
            that the controller can resynchronize every bit.
            In the range (1..16).
            Ignored if not using CAN-FD.

        """
        self.channel_info = channel
        self.fd = kwargs.get('fd', False)
        pcan_bitrate = pcan_bitrate_objs.get(bitrate, PCAN_BAUD_500K)

        hwtype = PCAN_TYPE_ISA
        ioport = 0x02A0
        interrupt = 11

        self.m_objPCANBasic = PCANBasic()
        self.m_PcanHandle = globals()[channel]

        if state is BusState.ACTIVE or state is BusState.PASSIVE:
            self.state = state
        else:
            raise ArgumentError("BusState must be Active or Passive")

        if self.fd:
            f_clock_val = kwargs.get('f_clock', None)
            if f_clock_val is None:
                f_clock = "{}={}".format('f_clock_mhz',
                                         kwargs.get('f_clock_mhz', None))
            else:
                f_clock = "{}={}".format('f_clock',
                                         kwargs.get('f_clock', None))

            fd_parameters_values = [f_clock] + [
                "{}={}".format(key, kwargs.get(key, None))
                for key in pcan_fd_parameter_list
                if kwargs.get(key, None) is not None
            ]

            self.fd_bitrate = ' ,'.join(fd_parameters_values).encode("ascii")

            result = self.m_objPCANBasic.InitializeFD(self.m_PcanHandle,
                                                      self.fd_bitrate)
        else:
            result = self.m_objPCANBasic.Initialize(self.m_PcanHandle,
                                                    pcan_bitrate, hwtype,
                                                    ioport, interrupt)

        if result != PCAN_ERROR_OK:
            raise PcanError(self._get_formatted_error(result))

        if HAS_EVENTS:
            self._recv_event = CreateEvent(None, 0, 0, None)
            result = self.m_objPCANBasic.SetValue(self.m_PcanHandle,
                                                  PCAN_RECEIVE_EVENT,
                                                  self._recv_event)
            if result != PCAN_ERROR_OK:
                raise PcanError(self._get_formatted_error(result))

        super(PcanBus, self).__init__(channel=channel,
                                      state=state,
                                      bitrate=bitrate,
                                      *args,
                                      **kwargs)
Пример #4
0
    def __init__(
        self,
        channel="PCAN_USBBUS1",
        state=BusState.ACTIVE,
        bitrate=500000,
        *args,
        **kwargs,
    ):
        """A PCAN USB interface to CAN.

        On top of the usual :class:`~can.Bus` methods provided,
        the PCAN interface includes the :meth:`~can.interface.pcan.PcanBus.flash`
        and :meth:`~can.interface.pcan.PcanBus.status` methods.

        :param str channel:
            The can interface name. An example would be 'PCAN_USBBUS1'.
            Alternatively the value can be an int with the numerical value.
            Default is 'PCAN_USBBUS1'

        :param can.bus.BusState state:
            BusState of the channel.
            Default is ACTIVE

        :param int bitrate:
            Bitrate of channel in bit/s.
            Default is 500 kbit/s.
            Ignored if using CanFD.

        :param bool fd:
            Should the Bus be initialized in CAN-FD mode.

        :param int f_clock:
            Clock rate in Hz.
            Any of the following:
            20000000, 24000000, 30000000, 40000000, 60000000, 80000000.
            Ignored if not using CAN-FD.
            Pass either f_clock or f_clock_mhz.

        :param int f_clock_mhz:
            Clock rate in MHz.
            Any of the following:
            20, 24, 30, 40, 60, 80.
            Ignored if not using CAN-FD.
            Pass either f_clock or f_clock_mhz.

        :param int nom_brp:
            Clock prescaler for nominal time quantum.
            In the range (1..1024)
            Ignored if not using CAN-FD.

        :param int nom_tseg1:
            Time segment 1 for nominal bit rate,
            that is, the number of quanta from (but not including)
            the Sync Segment to the sampling point.
            In the range (1..256).
            Ignored if not using CAN-FD.

        :param int nom_tseg2:
            Time segment 2 for nominal bit rate,
            that is, the number of quanta from the sampling
            point to the end of the bit.
            In the range (1..128).
            Ignored if not using CAN-FD.

        :param int nom_sjw:
            Synchronization Jump Width for nominal bit rate.
            Decides the maximum number of time quanta
            that the controller can resynchronize every bit.
            In the range (1..128).
            Ignored if not using CAN-FD.

        :param int data_brp:
            Clock prescaler for fast data time quantum.
            In the range (1..1024)
            Ignored if not using CAN-FD.

        :param int data_tseg1:
            Time segment 1 for fast data bit rate,
            that is, the number of quanta from (but not including)
            the Sync Segment to the sampling point.
            In the range (1..32).
            Ignored if not using CAN-FD.

        :param int data_tseg2:
            Time segment 2 for fast data bit rate,
            that is, the number of quanta from the sampling
            point to the end of the bit.
            In the range (1..16).
            Ignored if not using CAN-FD.

        :param int data_sjw:
            Synchronization Jump Width for fast data bit rate.
            Decides the maximum number of time quanta
            that the controller can resynchronize every bit.
            In the range (1..16).
            Ignored if not using CAN-FD.

        """
        self.channel_info = str(channel)
        self.fd = kwargs.get("fd", False)
        pcan_bitrate = PCAN_BITRATES.get(bitrate, PCAN_BAUD_500K)

        hwtype = PCAN_TYPE_ISA
        ioport = 0x02A0
        interrupt = 11

        if not isinstance(channel, int):
            channel = PCAN_CHANNEL_NAMES[channel]

        self.m_objPCANBasic = PCANBasic()
        self.m_PcanHandle = channel

        self.check_api_version()

        if state is BusState.ACTIVE or state is BusState.PASSIVE:
            self.state = state
        else:
            raise ValueError("BusState must be Active or Passive")

        if self.fd:
            f_clock_val = kwargs.get("f_clock", None)
            if f_clock_val is None:
                f_clock = "{}={}".format("f_clock_mhz", kwargs.get("f_clock_mhz", None))
            else:
                f_clock = "{}={}".format("f_clock", kwargs.get("f_clock", None))

            fd_parameters_values = [f_clock] + [
                "{}={}".format(key, kwargs.get(key, None))
                for key in PCAN_FD_PARAMETER_LIST
                if kwargs.get(key, None) is not None
            ]

            self.fd_bitrate = " ,".join(fd_parameters_values).encode("ascii")

            result = self.m_objPCANBasic.InitializeFD(
                self.m_PcanHandle, self.fd_bitrate
            )
        else:
            result = self.m_objPCANBasic.Initialize(
                self.m_PcanHandle, pcan_bitrate, hwtype, ioport, interrupt
            )

        if result != PCAN_ERROR_OK:
            raise PcanCanInitializationError(self._get_formatted_error(result))

        result = self.m_objPCANBasic.SetValue(
            self.m_PcanHandle, PCAN_ALLOW_ERROR_FRAMES, PCAN_PARAMETER_ON
        )

        if result != PCAN_ERROR_OK:
            if platform.system() != "Darwin":
                raise PcanCanInitializationError(self._get_formatted_error(result))
            else:
                # TODO Remove Filter when MACCan actually supports it:
                #  https://github.com/mac-can/PCBUSB-Library/
                log.debug(
                    "Ignoring error. PCAN_ALLOW_ERROR_FRAMES is still unsupported by OSX Library PCANUSB v0.10"
                )

        if HAS_EVENTS:
            self._recv_event = CreateEvent(None, 0, 0, None)
            result = self.m_objPCANBasic.SetValue(
                self.m_PcanHandle, PCAN_RECEIVE_EVENT, self._recv_event
            )
            if result != PCAN_ERROR_OK:
                raise PcanCanInitializationError(self._get_formatted_error(result))

        super().__init__(channel=channel, state=state, bitrate=bitrate, *args, **kwargs)