diff --git a/can/bus.py b/can/bus.py index a15c37985..2b36b3c57 100644 --- a/can/bus.py +++ b/can/bus.py @@ -11,12 +11,19 @@ import threading from time import time from collections import namedtuple +from aenum import Enum, auto from .broadcastmanager import ThreadBasedCyclicSendTask LOG = logging.getLogger(__name__) -BusState = namedtuple('BusState', 'ACTIVE, PASSIVE, ERROR') + +class BusState(Enum): + """The state in which a :class:`can.BusABC` can be.""" + + ACTIVE = auto() + PASSIVE = auto() + ERROR = auto() class BusABC(object): @@ -152,7 +159,7 @@ def send(self, msg, timeout=None): for transmit queue to be ready depending on driver implementation. If timeout is exceeded, an exception will be raised. Might not be supported by all interfaces. - None blocks indefinitly. + None blocks indefinitely. :raises can.CanError: if the message could not be sent @@ -369,8 +376,7 @@ def state(self): """ Return the current state of the hardware - :return: ACTIVE, PASSIVE or ERROR - :rtype: NamedTuple + :type: can.BusState """ return BusState.ACTIVE @@ -379,7 +385,7 @@ def state(self, new_state): """ Set the new state of the hardware - :param new_state: BusState.ACTIVE, BusState.PASSIVE or BusState.ERROR + :type: can.BusState """ raise NotImplementedError("Property is not implemented.") diff --git a/can/interfaces/pcan/pcan.py b/can/interfaces/pcan/pcan.py index 6fb0e5185..e989ad6e9 100644 --- a/can/interfaces/pcan/pcan.py +++ b/can/interfaces/pcan/pcan.py @@ -97,7 +97,7 @@ def __init__(self, channel='PCAN_USBBUS1', state=BusState.ACTIVE, bitrate=500000 self.m_objPCANBasic = PCANBasic() self.m_PcanHandle = globals()[channel] - if state is BusState.ACTIVE or BusState.PASSIVE: + if state is BusState.ACTIVE or state is BusState.PASSIVE: self.state = state else: raise ArgumentError("BusState must be Active or Passive") @@ -280,7 +280,7 @@ def state(self, new_state): if new_state is BusState.ACTIVE: self.m_objPCANBasic.SetValue(self.m_PcanHandle, PCAN_LISTEN_ONLY, PCAN_PARAMETER_OFF) - if new_state is BusState.PASSIVE: + elif new_state is BusState.PASSIVE: # When this mode is set, the CAN controller does not take part on active events (eg. transmit CAN messages) # but stays in a passive mode (CAN monitor), in which it can analyse the traffic on the CAN bus used by a # PCAN channel. See also the Philips Data Sheet "SJA1000 Stand-alone CAN controller". @@ -289,6 +289,6 @@ def state(self, new_state): class PcanError(CanError): """ - TODO: add docs + A generic error on a PCAN bus. """ pass diff --git a/can/interfaces/systec/ucanbus.py b/can/interfaces/systec/ucanbus.py index f776c93ae..969db7d53 100644 --- a/can/interfaces/systec/ucanbus.py +++ b/can/interfaces/systec/ucanbus.py @@ -104,7 +104,7 @@ def __init__(self, channel, can_filters=None, **kwargs): raise ValueError("Invalid bitrate {}".format(bitrate)) state = kwargs.get('state', BusState.ACTIVE) - if state is BusState.ACTIVE or BusState.PASSIVE: + if state is BusState.ACTIVE or state is BusState.PASSIVE: self._state = state else: raise ValueError("BusState must be Active or Passive") @@ -247,11 +247,11 @@ def state(self): @state.setter def state(self, new_state): - if self._state != BusState.ERROR and (new_state == BusState.ACTIVE or new_state == BusState.PASSIVE): - # deinitialize CAN channel + if self._state is not BusState.ERROR and (new_state is BusState.ACTIVE or new_state is BusState.PASSIVE): + # close the CAN channel self._ucan.shutdown(self.channel, False) # set mode - if new_state == BusState.ACTIVE: + if new_state is BusState.ACTIVE: self._params["mode"] &= ~Mode.MODE_LISTEN_ONLY else: self._params["mode"] |= Mode.MODE_LISTEN_ONLY diff --git a/can/logger.py b/can/logger.py index 6da89de96..204eb8dfb 100644 --- a/can/logger.py +++ b/can/logger.py @@ -57,10 +57,10 @@ def main(): parser.add_argument('-b', '--bitrate', type=int, help='''Bitrate to use for the CAN bus.''') - group = parser.add_mutually_exclusive_group(required=False) - group.add_argument('--active', help="Start the bus as active, this is applied the default.", + state_group = parser.add_mutually_exclusive_group(required=False) + state_group.add_argument('--active', help="Start the bus as active, this is applied by default.", action='store_true') - group.add_argument('--passive', help="Start the bus as passive.", + state_group.add_argument('--passive', help="Start the bus as passive.", action='store_true') # print help message when no arguments wre given @@ -98,8 +98,7 @@ def main(): if results.active: bus.state = BusState.ACTIVE - - if results.passive: + elif results.passive: bus.state = BusState.PASSIVE print('Connected to {}: {}'.format(bus.__class__.__name__, bus.channel_info)) diff --git a/examples/receive_all.py b/examples/receive_all.py index 90a4c68b6..44a495de7 100755 --- a/examples/receive_all.py +++ b/examples/receive_all.py @@ -12,8 +12,7 @@ def receive_all(): #bus = can.interface.Bus(bustype='ixxat', channel=0, bitrate=250000) #bus = can.interface.Bus(bustype='vector', app_name='CANalyzer', channel=0, bitrate=250000) - bus.state = BusState.ACTIVE - #bus.state = BusState.PASSIVE + bus.state = BusState.ACTIVE # or BusState.PASSIVE try: while True: diff --git a/setup.py b/setup.py index f58493d9b..87c9d489c 100644 --- a/setup.py +++ b/setup.py @@ -100,6 +100,7 @@ python_requires=">=2.7", install_requires=[ 'wrapt~=1.10', + 'aenum', 'typing;python_version<"3.5"', 'windows-curses;platform_system=="Windows"', ],