Skip to content

Commit

Permalink
Merge pull request #516 from hardbyte/release-3.1.0
Browse files Browse the repository at this point in the history
Release 3.1.0
  • Loading branch information
hardbyte authored Feb 23, 2019
2 parents 4b93f21 + 0348433 commit 3dcfcbb
Show file tree
Hide file tree
Showing 75 changed files with 3,874 additions and 281 deletions.
31 changes: 31 additions & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,34 @@
Version 3.1.0
====

Major features
--------------

Two new interfaces this release:

- SYSTEC contributed by @idaniel86 in PR #466
- CANalyst-II contributed by @smeng9 in PR #476


Other notable changes
---------------------

- #477 The kvaser interface now supports bus statistics via a custom bus method.
- #434 neovi now supports receiving own messages
- #490 Adding option to override the neovi library name
- #488 Allow simultaneous access to IXXAT cards
- #447 Improvements to serial interface:
* to allow receiving partial messages
* to fix issue with DLC of remote frames
* addition of unit tests
- #497 Small API changes to `Message` and added unit tests
- #471 Fix CAN FD issue in kvaser interface
- #462 Fix `Notifier` issue with asyncio
- #481 Fix PCAN support on OSX
- #455 Fix to `Message` initializer
- Small bugfixes and improvements


Version 3.0.0
====

Expand Down
4 changes: 4 additions & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@ Boris Wenzlaff
Pierre-Luc Tessier Gagné
Felix Divo <[email protected]>
Kristian Sloth Lauszus <[email protected]>
Shaoyu Meng <[email protected]>
Alexander Mueller<[email protected]>
Jan Goeteyn
"ykzheng" <[email protected]>
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ Example usage
receive_own_messages=True)
# send a message
message = can.Message(arbitration_id=123, extended_id=True,
message = can.Message(arbitration_id=123, is_extended_id=True,
data=[0x11, 0x22, 0x33])
bus.send(message, timeout=0.2)
Expand Down
3 changes: 2 additions & 1 deletion can/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import logging

__version__ = "3.0.0"
__version__ = "3.1.0"

log = logging.getLogger('can')

Expand All @@ -21,6 +21,7 @@ class CanError(IOError):
"""
pass


from .listener import Listener, BufferedReader, RedirectReader
try:
from .listener import AsyncBufferedReader
Expand Down
4 changes: 2 additions & 2 deletions can/broadcastmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,15 @@ def modify_data(self, message):


class MultiRateCyclicSendTaskABC(CyclicSendTaskABC):
"""Exposes more of the full power of the TX_SETUP opcode.
"""A Cyclic send task that supports switches send frequency after a set time.
"""

def __init__(self, channel, message, count, initial_period, subsequent_period):
"""
Transmits a message `count` times at `initial_period` then continues to
transmit message at `subsequent_period`.
:param can.interface.Bus channel:
:param channel: See interface specific documentation.
:param can.Message message:
:param int count:
:param float initial_period:
Expand Down
6 changes: 4 additions & 2 deletions can/bus.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ def send_periodic(self, msg, period, duration=None, store_task=True):
- the (optional) duration expires
- the Bus instance goes out of scope
- the Bus instance is shutdown
- :meth:`Bus.stop_all_periodic_tasks()` is called
- the task's :meth:`Task.stop()` method is called.
- :meth:`BusABC.stop_all_periodic_tasks()` is called
- the task's :meth:`CyclicTask.stop()` method is called.
:param can.Message msg:
Message to transmit
Expand Down Expand Up @@ -368,6 +368,7 @@ def __exit__(self, exc_type, exc_val, exc_tb):
def state(self):
"""
Return the current state of the hardware
:return: ACTIVE, PASSIVE or ERROR
:rtype: NamedTuple
"""
Expand All @@ -377,6 +378,7 @@ def state(self):
def state(self, new_state):
"""
Set the new state of the hardware
:param new_state: BusState.ACTIVE, BusState.PASSIVE or BusState.ERROR
"""
raise NotImplementedError("Property is not implemented.")
Expand Down
7 changes: 3 additions & 4 deletions can/interface.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# coding: utf-8

"""
This module contains the base implementation of `can.Bus` as well
as a list of all avalibale backends and some implemented
This module contains the base implementation of :class:`can.BusABC` as well
as a list of all available backends and some implemented
CyclicSendTasks.
"""

Expand All @@ -11,7 +11,6 @@
import sys
import importlib
import logging
import re

import can
from .bus import BusABC
Expand Down Expand Up @@ -145,7 +144,7 @@ def detect_available_configs(interfaces=None):
- `None` to search in all known interfaces.
:rtype: list[dict]
:return: an iterable of dicts, each suitable for usage in
the constructor of :class:`can.interface.Bus`.
the constructor of :class:`can.BusABC`.
"""

# Figure out where to search
Expand Down
4 changes: 3 additions & 1 deletion can/interfaces/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
'virtual': ('can.interfaces.virtual', 'VirtualBus'),
'neovi': ('can.interfaces.ics_neovi', 'NeoViBus'),
'vector': ('can.interfaces.vector', 'VectorBus'),
'slcan': ('can.interfaces.slcan', 'slcanBus')
'slcan': ('can.interfaces.slcan', 'slcanBus'),
'canalystii': ('can.interfaces.canalystii', 'CANalystIIBus'),
'systec': ('can.interfaces.systec', 'UcanBus')
}

BACKENDS.update({
Expand Down
168 changes: 168 additions & 0 deletions can/interfaces/canalystii.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
from ctypes import *
import logging
import platform
from can import BusABC, Message

logger = logging.getLogger(__name__)


class VCI_INIT_CONFIG(Structure):
_fields_ = [("AccCode", c_int32),
("AccMask", c_int32),
("Reserved", c_int32),
("Filter", c_ubyte),
("Timing0", c_ubyte),
("Timing1", c_ubyte),
("Mode", c_ubyte)]


class VCI_CAN_OBJ(Structure):
_fields_ = [("ID", c_uint),
("TimeStamp", c_int),
("TimeFlag", c_byte),
("SendType", c_byte),
("RemoteFlag", c_byte),
("ExternFlag", c_byte),
("DataLen", c_byte),
("Data", c_ubyte * 8),
("Reserved", c_byte * 3)]


VCI_USBCAN2 = 4

STATUS_OK = 0x01
STATUS_ERR = 0x00

TIMING_DICT = {
5000: (0xBF, 0xFF),
10000: (0x31, 0x1C),
20000: (0x18, 0x1C),
33330: (0x09, 0x6F),
40000: (0x87, 0xFF),
50000: (0x09, 0x1C),
66660: (0x04, 0x6F),
80000: (0x83, 0xFF),
83330: (0x03, 0x6F),
100000: (0x04, 0x1C),
125000: (0x03, 0x1C),
200000: (0x81, 0xFA),
250000: (0x01, 0x1C),
400000: (0x80, 0xFA),
500000: (0x00, 0x1C),
666000: (0x80, 0xB6),
800000: (0x00, 0x16),
1000000: (0x00, 0x14),
}

try:
if platform.system() == "Windows":
CANalystII = WinDLL("./ControlCAN.dll")
else:
CANalystII = CDLL("./libcontrolcan.so")
logger.info("Loaded CANalystII library")
except OSError as e:
CANalystII = None
logger.info("Cannot load CANalystII library")


class CANalystIIBus(BusABC):
def __init__(self, channel, device=0, baud=None, Timing0=None, Timing1=None, can_filters=None):
"""
:param channel: channel number
:param device: device number
:param baud: baud rate
:param Timing0: customize the timing register if baudrate is not specified
:param Timing1:
:param can_filters: filters for packet
"""
super(CANalystIIBus, self).__init__(channel, can_filters)

if isinstance(channel, (list, tuple)):
self.channels = channel
elif isinstance(channel, int):
self.channels = [channel]
else:
# Assume comma separated string of channels
self.channels = [int(ch.strip()) for ch in channel.split(',')]

self.device = device

self.channel_info = "CANalyst-II: device {}, channels {}".format(self.device, self.channels)

if baud is not None:
try:
Timing0, Timing1 = TIMING_DICT[baud]
except KeyError:
raise ValueError("Baudrate is not supported")

if Timing0 is None or Timing1 is None:
raise ValueError("Timing registers are not set")

self.init_config = VCI_INIT_CONFIG(0, 0xFFFFFFFF, 0, 1, Timing0, Timing1, 0)

if CANalystII.VCI_OpenDevice(VCI_USBCAN2, self.device, 0) == STATUS_ERR:
logger.error("VCI_OpenDevice Error")

for channel in self.channels:
if CANalystII.VCI_InitCAN(VCI_USBCAN2, self.device, channel, byref(self.init_config)) == STATUS_ERR:
logger.error("VCI_InitCAN Error")
self.shutdown()
return

if CANalystII.VCI_StartCAN(VCI_USBCAN2, self.device, channel) == STATUS_ERR:
logger.error("VCI_StartCAN Error")
self.shutdown()
return

def send(self, msg, timeout=None):
"""
:param msg: message to send
:param timeout: timeout is not used here
:return:
"""
extern_flag = 1 if msg.is_extended_id else 0
raw_message = VCI_CAN_OBJ(msg.arbitration_id, 0, 0, 1, msg.is_remote_frame, extern_flag, msg.dlc, (c_ubyte * 8)(*msg.data), (c_byte * 3)(*[0, 0, 0]))

if msg.channel is not None:
channel = msg.channel
elif len(self.channels) == 1:
channel = self.channels[0]
else:
raise ValueError(
"msg.channel must be set when using multiple channels.")

CANalystII.VCI_Transmit(VCI_USBCAN2, self.device, channel, byref(raw_message), 1)

def _recv_internal(self, timeout=None):
"""
:param timeout: float in seconds
:return:
"""
raw_message = VCI_CAN_OBJ()

timeout = -1 if timeout is None else int(timeout * 1000)

if CANalystII.VCI_Receive(VCI_USBCAN2, self.device, self.channels[0], byref(raw_message), 1, timeout) <= STATUS_ERR:
return None, False
else:
return (
Message(
timestamp=raw_message.TimeStamp if raw_message.TimeFlag else 0.0,
arbitration_id=raw_message.ID,
is_remote_frame=raw_message.RemoteFlag,
channel=0,
dlc=raw_message.DataLen,
data=raw_message.Data,
),
False,
)

def flush_tx_buffer(self):
for channel in self.channels:
CANalystII.VCI_ClearBuffer(VCI_USBCAN2, self.device, channel)

def shutdown(self):
CANalystII.VCI_CloseDevice(VCI_USBCAN2, self.device)
15 changes: 13 additions & 2 deletions can/interfaces/ics_neovi/neovi_bus.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ def __init__(self, channel, can_filters=None, **config):
:type channel: int or str or list(int) or list(str)
:param list can_filters:
See :meth:`can.BusABC.set_filters` for details.
:param bool receive_own_messages:
If transmitted messages should also be received by this bus.
:param bool use_system_timestamp:
Use system timestamp for can messages instead of the hardware time
stamp
Expand All @@ -89,6 +91,8 @@ def __init__(self, channel, can_filters=None, **config):
:param int data_bitrate:
Which bitrate to use for data phase in CAN FD.
Defaults to arbitration bitrate.
:param override_library_name:
Absolute path or relative path to the library including filename.
"""
if ics is None:
raise ImportError('Please install python-ics')
Expand All @@ -99,6 +103,9 @@ def __init__(self, channel, can_filters=None, **config):
logger.info("CAN Filters: {}".format(can_filters))
logger.info("Got configuration of: {}".format(config))

if 'override_library_name' in config:
ics.override_library_name(config.get('override_library_name'))

if isinstance(channel, (list, tuple)):
self.channels = channel
elif isinstance(channel, int):
Expand Down Expand Up @@ -127,6 +134,7 @@ def __init__(self, channel, can_filters=None, **config):
self._use_system_timestamp = bool(
config.get('use_system_timestamp', False)
)
self._receive_own_messages = config.get('receive_own_messages', True)

self.channel_info = '%s %s CH:%s' % (
self.dev.Name,
Expand Down Expand Up @@ -222,6 +230,9 @@ def _process_msg_queue(self, timeout=0.1):
for ics_msg in messages:
if ics_msg.NetworkID not in self.channels:
continue
is_tx = bool(ics_msg.StatusBitField & ics.SPY_STATUS_TX_MSG)
if not self._receive_own_messages and is_tx:
continue
self.rx_buffer.append(ics_msg)
if errors:
logger.warning("%d error(s) found" % errors)
Expand Down Expand Up @@ -262,7 +273,7 @@ def _ics_msg_to_message(self, ics_msg):
arbitration_id=ics_msg.ArbIDOrHeader,
data=data,
dlc=ics_msg.NumberBytesData,
extended_id=bool(
is_extended_id=bool(
ics_msg.StatusBitField & ics.SPY_STATUS_XTD_FRAME
),
is_fd=is_fd,
Expand All @@ -283,7 +294,7 @@ def _ics_msg_to_message(self, ics_msg):
arbitration_id=ics_msg.ArbIDOrHeader,
data=ics_msg.Data[:ics_msg.NumberBytesData],
dlc=ics_msg.NumberBytesData,
extended_id=bool(
is_extended_id=bool(
ics_msg.StatusBitField & ics.SPY_STATUS_XTD_FRAME
),
is_fd=is_fd,
Expand Down
Loading

0 comments on commit 3dcfcbb

Please sign in to comment.