__init__.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. #
  2. # Copyright (C) 2014-2016 UAVCAN Development Team <uavcan.org>
  3. #
  4. # This software is distributed under the terms of the MIT License.
  5. #
  6. # Author: Ben Dyer <ben_dyer@mac.com>
  7. # Pavel Kirienko <pavel.kirienko@zubax.com>
  8. #
  9. from __future__ import division, absolute_import, print_function, unicode_literals
  10. import sys
  11. from .slcan import SLCAN
  12. from .common import DriverError, CANFrame
  13. if sys.platform.startswith('linux'):
  14. from .socketcan import SocketCAN
  15. else:
  16. SocketCAN = None
  17. __all__ = ['make_driver', 'DriverError', 'CANFrame']
  18. def make_driver(device_name, **kwargs):
  19. """Creates an instance of CAN driver.
  20. The right driver class will be selected automatically based on the device_name.
  21. :param device_name: This parameter is used to select driver class. E.g. "/dev/ttyACM0", "COM9", "can0".
  22. :param kwargs: Passed directly to the constructor.
  23. """
  24. windows_com_port = device_name.replace('\\', '').replace('.', '').lower().startswith('com')
  25. unix_tty = device_name.startswith('/dev/')
  26. if windows_com_port or unix_tty:
  27. return SLCAN(device_name, **kwargs)
  28. elif SocketCAN is not None:
  29. return SocketCAN(device_name, **kwargs)
  30. else:
  31. raise DriverError('Unrecognized device name: %r' % device_name)