Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions can/interfaces/ixxat/canlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
__all__ = [
"VCITimeout",
"VCIError",
"VCIBusOffError",
"VCIDeviceNotFoundError",
"IXXATBus",
"vciFormatError",
Expand Down Expand Up @@ -354,6 +355,15 @@ def __check_status(result, function, arguments):
constants.CAN_ERROR_CRC: "CAN CRC error",
constants.CAN_ERROR_OTHER: "Other (unknown) CAN error",
}

CAN_STATUS_FLAGS = {
constants.CAN_STATUS_TXPEND: "transmission pending",
constants.CAN_STATUS_OVRRUN: "data overrun occurred",
constants.CAN_STATUS_ERRLIM: "error warning limit exceeded",
constants.CAN_STATUS_BUSOFF: "bus off",
constants.CAN_STATUS_ININIT: "init mode active",
constants.CAN_STATUS_BUSCERR: "bus coupling error",
}
# ----------------------------------------------------------------------------


Expand Down Expand Up @@ -644,6 +654,13 @@ def _recv_internal(self, timeout):
)
)

elif (
self._message.uMsgInfo.Bits.type == constants.CAN_MSGTYPE_STATUS
):
log.info(_format_can_status(self._message.abData[0]))
if self._message.abData[0] & constants.CAN_STATUS_BUSOFF:
raise VCIBusOffError()

elif (
self._message.uMsgInfo.Bits.type
== constants.CAN_MSGTYPE_TIMEOVR
Expand Down Expand Up @@ -761,3 +778,25 @@ def stop(self):
# the list with permanently stopped messages
_canlib.canSchedulerRemMessage(self._scheduler, self._index)
self._index = None


def _format_can_status(status_flags: int):
"""
Format a status bitfield found in CAN_MSGTYPE_STATUS messages or in dwStatus
field in CANLINESTATUS.

Valid states are defined in the CAN_STATUS_* constants in cantype.h
"""
states = []
for flag, description in CAN_STATUS_FLAGS.items():
if status_flags & flag:
states.append(description)
status_flags &= ~flag

if status_flags:
states.append("unknown state 0x{:02x}".format(status_flags))

if states:
return "CAN status message: {}".format(", ".join(states))
else:
return "Empty CAN status message"
13 changes: 12 additions & 1 deletion can/interfaces/ixxat/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@

from can import CanError

__all__ = ["VCITimeout", "VCIError", "VCIRxQueueEmptyError", "VCIDeviceNotFoundError"]
__all__ = [
"VCITimeout",
"VCIError",
"VCIRxQueueEmptyError",
"VCIBusOffError",
"VCIDeviceNotFoundError",
]


class VCITimeout(CanError):
Expand All @@ -24,5 +30,10 @@ def __init__(self):
super().__init__("Receive queue is empty")


class VCIBusOffError(VCIError):
def __init__(self):
super().__init__("Controller is in BUSOFF state")


class VCIDeviceNotFoundError(CanError):
pass