From 4b658d07b32bc83abb60faa742c18262c9a3b4e1 Mon Sep 17 00:00:00 2001 From: tamenol <37591107+tamenol@users.noreply.github.com> Date: Thu, 14 Nov 2019 10:40:09 +0100 Subject: [PATCH 1/4] Added status_string method to return simple status strings status_string returns the current status of the bus in a single word --- can/interfaces/pcan/pcan.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/can/interfaces/pcan/pcan.py b/can/interfaces/pcan/pcan.py index 5c7b2b9d7..e1833035d 100644 --- a/can/interfaces/pcan/pcan.py +++ b/can/interfaces/pcan/pcan.py @@ -528,6 +528,32 @@ def _detect_available_configs(): ) return channels + def status_string(self): + """ + Query the PCAN bus status. + :rtype: string + :return: The status in string. + """ + status = self.status() + if status == PCAN_ERROR_OK: + return "OK" + elif status == PCAN_ERROR_XMTFULL: + return "XMTFULL" + elif status == PCAN_ERROR_OVERRUN: + return "OVERRUN" + elif status == PCAN_ERROR_BUSLIGHT: + return "BUSLIGHT" + elif status == PCAN_ERROR_BUSHEAVY: + return "BUSHEAVY" + elif status == PCAN_ERROR_BUSWARNING: + return "BUSWARNING" + elif status == PCAN_ERROR_BUSPASSIVE: + return "BUSPASSIVE" + elif status == PCAN_ERROR_BUSOFF: + return "BUSOFF" + else: + return "NonImplementedError" + class PcanError(CanError): """ From 6b9e4a73d48c6c27fb14726144fcbe8b6536ae2d Mon Sep 17 00:00:00 2001 From: tamenol <37591107+tamenol@users.noreply.github.com> Date: Tue, 4 Feb 2020 10:34:18 +0100 Subject: [PATCH 2/4] Return None when error code is not implemented; added return type hint --- can/interfaces/pcan/pcan.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/can/interfaces/pcan/pcan.py b/can/interfaces/pcan/pcan.py index e1833035d..480e8d1e1 100644 --- a/can/interfaces/pcan/pcan.py +++ b/can/interfaces/pcan/pcan.py @@ -528,10 +528,9 @@ def _detect_available_configs(): ) return channels - def status_string(self): + def status_string(self) -> Optional[str]: """ Query the PCAN bus status. - :rtype: string :return: The status in string. """ status = self.status() @@ -552,7 +551,7 @@ def status_string(self): elif status == PCAN_ERROR_BUSOFF: return "BUSOFF" else: - return "NonImplementedError" + return None class PcanError(CanError): From 890128f40da623299f153d7e0fed1de028f2d417 Mon Sep 17 00:00:00 2001 From: tamenol <37591107+tamenol@users.noreply.github.com> Date: Tue, 4 Feb 2020 11:07:15 +0100 Subject: [PATCH 3/4] added string status to dictionary in .basic --- can/interfaces/pcan/basic.py | 32 ++++++++++++++++++++++++++++++++ can/interfaces/pcan/pcan.py | 20 +++----------------- 2 files changed, 35 insertions(+), 17 deletions(-) diff --git a/can/interfaces/pcan/basic.py b/can/interfaces/pcan/basic.py index b10e5404e..33bb075c2 100644 --- a/can/interfaces/pcan/basic.py +++ b/can/interfaces/pcan/basic.py @@ -400,6 +400,38 @@ PCAN_TYPE_DNG_SJA = TPCANType(0x05) # PCAN-Dongle SJA1000 PCAN_TYPE_DNG_SJA_EPP = TPCANType(0x06) # PCAN-Dongle EPP SJA1000 +# string description of the error codes +PCAN_DICT_STATUS = { + PCAN_ERROR_OK: "OK", + PCAN_ERROR_XMTFULL: "XMTFULL", + PCAN_ERROR_OVERRUN: "OVERRUN", + PCAN_ERROR_BUSLIGHT: "BUSLIGHT", + PCAN_ERROR_BUSHEAVY: "BUSHEAVY", + PCAN_ERROR_BUSWARNING: "BUSWARNING", + PCAN_ERROR_BUSPASSIVE: "BUSPASSIVE", + PCAN_ERROR_BUSOFF: "BUSOFF", + PCAN_ERROR_ANYBUSERR: "ANYBUSERR", + PCAN_ERROR_QRCVEMPTY: "QRCVEMPTY", + PCAN_ERROR_QOVERRUN: "QOVERRUN", + PCAN_ERROR_QXMTFULL: "QXMTFULL", + PCAN_ERROR_REGTEST: "ERR_REGTEST", + PCAN_ERROR_NODRIVER: "NODRIVER", + PCAN_ERROR_HWINUSE: "HWINUSE", + PCAN_ERROR_NETINUSE: "NETINUSE", + PCAN_ERROR_ILLHW: "ILLHW", + PCAN_ERROR_ILLNET: "ILLNET", + PCAN_ERROR_ILLCLIENT: "ILLCLIENT", + PCAN_ERROR_ILLHANDLE: "ILLHANDLE", + PCAN_ERROR_RESOURCE: "ERR_RESOURCE", + PCAN_ERROR_ILLPARAMTYPE: "ILLPARAMTYPE", + PCAN_ERROR_ILLPARAMVAL: "ILLPARAMVAL", + PCAN_ERROR_UNKNOWN: "UNKNOWN", + PCAN_ERROR_ILLDATA: "ILLDATA", + PCAN_ERROR_CAUTION: "CAUTION", + PCAN_ERROR_INITIALIZE: "ERR_INITIALIZE", + PCAN_ERROR_ILLOPERATION: "ILLOPERATION" +} + class TPCANMsg(Structure): """ diff --git a/can/interfaces/pcan/pcan.py b/can/interfaces/pcan/pcan.py index 480e8d1e1..14d8dc774 100644 --- a/can/interfaces/pcan/pcan.py +++ b/can/interfaces/pcan/pcan.py @@ -5,6 +5,7 @@ import logging import time +from typing import Optional from can import CanError, Message, BusABC from can.bus import BusState from can.util import len2dlc, dlc2len @@ -533,23 +534,8 @@ def status_string(self) -> Optional[str]: Query the PCAN bus status. :return: The status in string. """ - status = self.status() - if status == PCAN_ERROR_OK: - return "OK" - elif status == PCAN_ERROR_XMTFULL: - return "XMTFULL" - elif status == PCAN_ERROR_OVERRUN: - return "OVERRUN" - elif status == PCAN_ERROR_BUSLIGHT: - return "BUSLIGHT" - elif status == PCAN_ERROR_BUSHEAVY: - return "BUSHEAVY" - elif status == PCAN_ERROR_BUSWARNING: - return "BUSWARNING" - elif status == PCAN_ERROR_BUSPASSIVE: - return "BUSPASSIVE" - elif status == PCAN_ERROR_BUSOFF: - return "BUSOFF" + if self.status() in PCAN_DICT_STATUS: + return PCAN_DICT_STATUS[self.status()] else: return None From 87b8b4a5945539f41d7fd0e14a8562b0f0e24c07 Mon Sep 17 00:00:00 2001 From: tamenol <37591107+tamenol@users.noreply.github.com> Date: Wed, 5 Feb 2020 09:26:57 +0100 Subject: [PATCH 4/4] black formatting --- can/interfaces/pcan/basic.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/can/interfaces/pcan/basic.py b/can/interfaces/pcan/basic.py index 33bb075c2..0c5dab4fe 100644 --- a/can/interfaces/pcan/basic.py +++ b/can/interfaces/pcan/basic.py @@ -429,7 +429,7 @@ PCAN_ERROR_ILLDATA: "ILLDATA", PCAN_ERROR_CAUTION: "CAUTION", PCAN_ERROR_INITIALIZE: "ERR_INITIALIZE", - PCAN_ERROR_ILLOPERATION: "ILLOPERATION" + PCAN_ERROR_ILLOPERATION: "ILLOPERATION", }