Esempio n. 1
0
    def __new__(cls, other, channel=None, *args, **kwargs):
        """
        Takes the same arguments as :class:`can.BusABC` with the addition of:

        :param kwargs:
            Should contain a bustype key with a valid interface name.

        :raises: NotImplementedError if the bustype isn't recognized
        """
        if 'bustype' in kwargs:
            can.rc['interface'] = kwargs['bustype']
            del kwargs['bustype']

            if can.rc['interface'] == 'socketcan':
                can.rc['interface'] = choose_socketcan_implementation()

        if 'interface' not in can.rc or 'channel' not in can.rc or can.rc[
                'interface'] is None:
            can.log.debug("Loading default configuration")
            # Load defaults
            can.rc = load_config()

        if can.rc['interface'] not in VALID_INTERFACES:
            raise NotImplementedError('Invalid CAN Bus Type - {}'.format(
                can.rc['interface']))

        # Import the correct Bus backend
        interface = can.rc['interface']
        if interface == 'kvaser':
            from can.interfaces.kvaser import KvaserBus
            cls = KvaserBus
        elif interface == 'socketcan_ctypes':
            from can.interfaces.socketcan_ctypes import SocketcanCtypes_Bus
            cls = SocketcanCtypes_Bus
        elif interface == 'socketcan_native':
            from can.interfaces.socketcan_native import SocketcanNative_Bus
            cls = SocketcanNative_Bus
        elif interface == 'serial':
            from can.interfaces.serial.serial_can import SerialBus
            cls = SerialBus
        elif interface == 'pcan':
            from can.interfaces.pcan import PcanBus
            cls = PcanBus
        elif interface == 'usb2can':
            from can.interfaces.usb2canInterface import Usb2canBus
            cls = Usb2canBus
        elif interface == 'ixxat':
            from can.interfaces.ixxat import IXXATBus
            cls = IXXATBus
        elif interface == 'virtual':
            from can.interfaces.virtual import VirtualBus
            cls = VirtualBus
        else:
            raise NotImplementedError(
                "CAN interface '{}' not supported".format(interface))

        if channel is None:
            channel = can.rc['channel']
        return cls(channel, **kwargs)
Esempio n. 2
0
    def __new__(cls, other, channel, *args, **kwargs):

        config = load_config(config={'channel': channel})

        # Import the correct implementation of CyclicSendTask
        if config['interface'] == 'socketcan_ctypes':
            from can.interfaces.socketcan.socketcan_ctypes import CyclicSendTask as _ctypesCyclicSendTask
            cls = _ctypesCyclicSendTask
        elif config['interface'] == 'socketcan_native':
            from can.interfaces.socketcan.socketcan_native import CyclicSendTask as _nativeCyclicSendTask
            cls = _nativeCyclicSendTask
        else:
            raise can.CanError(
                "Current CAN interface doesn't support CyclicSendTask")

        return cls(config['channel'], *args, **kwargs)
Esempio n. 3
0
    def __new__(cls, other, channel=None, *args, **kwargs):
        """
        Takes the same arguments as :class:`can.BusABC` with the addition of:

        :param kwargs:
            Should contain a bustype key with a valid interface name.

        :raises:
            NotImplementedError if the bustype isn't recognized
        :raises:
            ValueError if the bustype or channel isn't either passed as an argument
            or set in the can.rc config.

        """
        config = load_config(config={
            'interface': kwargs.get('bustype'),
            'channel': channel
        })

        if 'bustype' in kwargs:
            # remove the bustype so it doesn't get passed to the backend
            del kwargs['bustype']
        interface = config['interface']
        channel = config['channel']

        # Import the correct Bus backend
        try:
            (module_name, class_name) = BACKENDS[interface]
        except KeyError:
            raise NotImplementedError("CAN interface '{}' not supported".format(interface))

        try:
            module = importlib.import_module(module_name)
        except Exception as e:
            raise ImportError(
                "Cannot import module {} for CAN interface '{}': {}".format(module_name, interface, e)
            )
        try:
            cls = getattr(module, class_name)
        except Exception as e:
            raise ImportError(
                "Cannot import class {} from module {} for CAN interface '{}': {}".format(
                    class_name, module_name, interface, e
                )
            )

        return cls(channel, **kwargs)
Esempio n. 4
0
    def __new__(cls, other, channel=None, *args, **kwargs):

        # Load defaults
        can.rc = load_config()

        if 'bustype' in kwargs:
            can.rc['interface'] = kwargs['bustype']
            del kwargs['bustype']

        if can.rc['interface'] not in set([
                'kvaser', 'serial', 'pcan', 'socketcan_native',
                'socketcan_ctypes', 'socketcan'
        ]):
            raise NotImplementedError('Invalid CAN Bus Type - {}'.format(
                can.rc['interface']))

        if can.rc['interface'] == 'socketcan':
            can.rc['interface'] = choose_socketcan_implementation()

        # Import the correct backend
        if can.rc['interface'] == 'kvaser':
            from can.interfaces.kvaser import KvaserBus
            cls = KvaserBus
        elif can.rc['interface'] == 'socketcan_ctypes':
            from can.interfaces.socketcan_ctypes import SocketscanCtypes_Bus
            cls = SocketscanCtypes_Bus
        elif can.rc['interface'] == 'socketcan_native':
            from can.interfaces.socketcan_native import SocketscanNative_Bus
            cls = SocketscanNative_Bus
        elif can.rc['interface'] == 'serial':
            from can.interfaces.serial_can import SerialBus
            cls = SerialBus
        elif can.rc['interface'] == 'pcan':
            from can.interfaces.pcan import PcanBus
            cls = PcanBus
        else:
            raise NotImplementedError("CAN Interface Not Found")

        if channel is None:
            channel = can.rc['channel']
        return cls(channel, **kwargs)
Esempio n. 5
0
    def __new__(cls, other, channel, *args, **kwargs):
        if 'bustype' in kwargs:
            if kwargs['bustype'] == 'kvaser':
                can.rc['interface'] = 'kvaser'
                cls = KvaserBus
            elif kwargs['bustype'] == 'socketcan_ctypes':
                can.rc['interface'] = 'socketcan_ctypes'
            elif kwargs['bustype'] == 'socketcan_native':
                can.rc['interface'] = 'socketcan_native'
            elif kwargs['bustype'] == 'socketcan':
                try:
                    import fcntl
                    can.rc['interface'] = 'socketcan_native'
                except ImportError:
                    can.rc['interface'] = 'socketcan_ctypes'
            elif kwargs['bustype'] =='serial':
                can.rc['interface'] = 'serial'
                cls = SerialBus
            elif kwargs['bustype'] =='pcan':
                can.rc['interface'] = 'pcan'
                cls = PcanBus
            else:
                raise NotImplementedError('Invalid CAN Bus Type.')
            del kwargs['bustype']
        else:
            can.rc = load_config()

        if can.rc['interface'] == 'kvaser':
            cls = KvaserBus
        elif can.rc['interface'] == 'socketcan_ctypes':
            cls = SocketscanCtypes_Bus
        elif can.rc['interface'] == 'socketcan_native':
            cls = SocketscanNative_Bus
        elif can.rc['interface'] =='serial':
            cls = SerialBus
        elif can.rc['interface'] =='pcan':
            cls = PcanBus
        else:
            raise NotImplementedError("CAN Interface Not Found")

        return cls(channel, **kwargs)
Esempio n. 6
0
    def __new__(cls, other, channel, *args, **kwargs):
        if 'bustype' in kwargs:
            if kwargs['bustype'] == 'kvaser':
                can.rc['interface'] = 'kvaser'
                cls = KvaserBus
            elif kwargs['bustype'] == 'socketcan_ctypes':
                can.rc['interface'] = 'socketcan_ctypes'
            elif kwargs['bustype'] == 'socketcan_native':
                can.rc['interface'] = 'socketcan_native'
            elif kwargs['bustype'] == 'socketcan':
                try:
                    import fcntl
                    can.rc['interface'] = 'socketcan_native'
                except ImportError:
                    can.rc['interface'] = 'socketcan_ctypes'
            elif kwargs['bustype'] == 'serial':
                can.rc['interface'] = 'serial'
                cls = SerialBus
            elif kwargs['bustype'] == 'pcan':
                can.rc['interface'] = 'pcan'
                cls = PcanBus
            else:
                raise NotImplementedError('Invalid CAN Bus Type.')
            del kwargs['bustype']
        else:
            can.rc = load_config()

        if can.rc['interface'] == 'kvaser':
            cls = KvaserBus
        elif can.rc['interface'] == 'socketcan_ctypes':
            cls = SocketscanCtypes_Bus
        elif can.rc['interface'] == 'socketcan_native':
            cls = SocketscanNative_Bus
        elif can.rc['interface'] == 'serial':
            cls = SerialBus
        elif can.rc['interface'] == 'pcan':
            cls = PcanBus
        else:
            raise NotImplementedError("CAN Interface Not Found")

        return cls(channel, **kwargs)
Esempio n. 7
0
    def __new__(cls, other, channel, *args, **kwargs):

        config = load_config(config={'channel': channel})

        # Import the correct implementation of CyclicSendTask
        if config['interface'] == 'socketcan_ctypes':
            from can.interfaces.socketcan.socketcan_ctypes import CyclicSendTask as _ctypesCyclicSendTask
            cls = _ctypesCyclicSendTask
        elif config['interface'] == 'socketcan_native':
            from can.interfaces.socketcan.socketcan_native import CyclicSendTask as _nativeCyclicSendTask
            cls = _nativeCyclicSendTask
        # CyclicSendTask has not been fully implemented on remote interface yet.
        # Waiting for issue #80 which will change the API to make it easier for
        # interfaces other than socketcan to implement it
        #elif can.rc['interface'] == 'remote':
        #    from can.interfaces.remote import CyclicSendTask as _remoteCyclicSendTask
        #    cls = _remoteCyclicSendTask
        else:
            raise can.CanError(
                "Current CAN interface doesn't support CyclicSendTask")

        return cls(config['channel'], *args, **kwargs)
Esempio n. 8
0
    def __new__(cls, other, channel=None, *args, **kwargs):

        # Load defaults
        can.rc = load_config()

        if 'bustype' in kwargs:
            can.rc['interface'] = kwargs['bustype']
            del kwargs['bustype']

        if can.rc['interface'] not in set(['kvaser', 'serial', 'pcan', 'socketcan_native', 'socketcan_ctypes', 'socketcan']):
            raise NotImplementedError('Invalid CAN Bus Type - {}'.format(can.rc['interface']))

        if can.rc['interface'] == 'socketcan':
            can.rc['interface'] = choose_socketcan_implementation()

        # Import the correct backend
        if can.rc['interface'] == 'kvaser':
            from can.interfaces.kvaser import KvaserBus
            cls = KvaserBus
        elif can.rc['interface'] == 'socketcan_ctypes':
            from can.interfaces.socketcan_ctypes import SocketscanCtypes_Bus
            cls = SocketscanCtypes_Bus
        elif can.rc['interface'] == 'socketcan_native':
            from can.interfaces.socketcan_native import SocketscanNative_Bus
            cls = SocketscanNative_Bus
        elif can.rc['interface'] == 'serial':
            from can.interfaces.serial_can import SerialBus
            cls = SerialBus
        elif can.rc['interface'] == 'pcan':
            from can.interfaces.pcan import PcanBus
            cls = PcanBus
        else:
            raise NotImplementedError("CAN Interface Not Found")

        if channel is None:
            channel = can.rc['channel']
        return cls(channel, **kwargs)
Esempio n. 9
0
    def __new__(cls, other, channel, *args, **kwargs):

        # If can.rc doesn't look valid: load default
        if 'interface' not in can.rc or 'channel' not in can.rc:
            can.log.debug("Loading default configuration")
            can.rc = load_config()

        print(can.rc)
        if can.rc['interface'] not in VALID_INTERFACES:
            raise NotImplementedError('Invalid CAN Bus Type - {}'.format(
                can.rc['interface']))

        # Import the correct implementation of CyclicSendTask
        if can.rc['interface'] == 'socketcan_ctypes':
            from can.interfaces.socketcan_ctypes import CyclicSendTask as _ctypesCyclicSendTask
            cls = _ctypesCyclicSendTask
        elif can.rc['interface'] == 'socketcan_native':
            from can.interfaces.socketcan_native import CyclicSendTask as _nativeCyclicSendTask
            cls = _nativeCyclicSendTask
        else:
            can.log.info(
                "Current CAN interface doesn't support CyclicSendTask")

        return cls(channel, *args, **kwargs)
Esempio n. 10
0
"""
can is an object-orient Controller Area Network interface module.

Modules include:

    :mod:`can.message`
        defines the :class:`~can.Message` class which is the 
        lowest level of OO access to the library.

"""
import logging
log = logging.getLogger('can')


class CanError(IOError):
    pass

from can.CAN import BufferedReader, Listener, Printer, CSVWriter, SqliteWriter, set_logging_level
from can.message import Message
from can.bus import BusABC
from can.notifier import Notifier
from can.broadcastmanager import send_periodic, CyclicSendTaskABC, MultiRateCyclicSendTaskABC
from can.util import load_config

log.debug("Loading can configuration")
rc = load_config()
log.debug("RC: {}".format(rc))

from can.interfaces import interface