Skip to content

Commit d603ff9

Browse files
authored
Specific Exceptions: Adapting pcan interface (#1152)
* Specific Exceptions: Adapting pcan interface Part of #1046. Also partly cleans up the docstrings in `basic.py` and uses some subtests in `test_pcan.py` (the parameter `name` was else unused in most parameterized tests). * Run black formatter * Remove wildcard import; move some dicts/lists to basic.py
1 parent b886203 commit d603ff9

File tree

3 files changed

+275
-232
lines changed

3 files changed

+275
-232
lines changed

can/interfaces/pcan/basic.py

+116-39
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,12 @@
88
#
99
# ------------------------------------------------------------------
1010
# Author : Keneth Wagner
11-
# Last change: 14.01.2021 Wagner
12-
#
13-
# Language: Python 2.7, 3.7
1411
# ------------------------------------------------------------------
1512
#
1613
# Copyright (C) 1999-2021 PEAK-System Technik GmbH, Darmstadt
1714
# more Info at http://www.peak-system.com
18-
#
1915

2016
# Module Imports
21-
#
2217
from ctypes import *
2318
from ctypes.util import find_library
2419
from string import *
@@ -553,6 +548,102 @@ class TPCANChannelInformation(Structure):
553548
] # Availability status of a PCAN-Channel
554549

555550

551+
# ///////////////////////////////////////////////////////////
552+
# Additional objects
553+
# ///////////////////////////////////////////////////////////
554+
555+
PCAN_BITRATES = {
556+
1000000: PCAN_BAUD_1M,
557+
800000: PCAN_BAUD_800K,
558+
500000: PCAN_BAUD_500K,
559+
250000: PCAN_BAUD_250K,
560+
125000: PCAN_BAUD_125K,
561+
100000: PCAN_BAUD_100K,
562+
95000: PCAN_BAUD_95K,
563+
83000: PCAN_BAUD_83K,
564+
50000: PCAN_BAUD_50K,
565+
47000: PCAN_BAUD_47K,
566+
33000: PCAN_BAUD_33K,
567+
20000: PCAN_BAUD_20K,
568+
10000: PCAN_BAUD_10K,
569+
5000: PCAN_BAUD_5K,
570+
}
571+
572+
PCAN_FD_PARAMETER_LIST = (
573+
"nom_brp",
574+
"nom_tseg1",
575+
"nom_tseg2",
576+
"nom_sjw",
577+
"data_brp",
578+
"data_tseg1",
579+
"data_tseg2",
580+
"data_sjw",
581+
)
582+
583+
PCAN_CHANNEL_NAMES = {
584+
"PCAN_NONEBUS": PCAN_NONEBUS,
585+
"PCAN_ISABUS1": PCAN_ISABUS1,
586+
"PCAN_ISABUS2": PCAN_ISABUS2,
587+
"PCAN_ISABUS3": PCAN_ISABUS3,
588+
"PCAN_ISABUS4": PCAN_ISABUS4,
589+
"PCAN_ISABUS5": PCAN_ISABUS5,
590+
"PCAN_ISABUS6": PCAN_ISABUS6,
591+
"PCAN_ISABUS7": PCAN_ISABUS7,
592+
"PCAN_ISABUS8": PCAN_ISABUS8,
593+
"PCAN_DNGBUS1": PCAN_DNGBUS1,
594+
"PCAN_PCIBUS1": PCAN_PCIBUS1,
595+
"PCAN_PCIBUS2": PCAN_PCIBUS2,
596+
"PCAN_PCIBUS3": PCAN_PCIBUS3,
597+
"PCAN_PCIBUS4": PCAN_PCIBUS4,
598+
"PCAN_PCIBUS5": PCAN_PCIBUS5,
599+
"PCAN_PCIBUS6": PCAN_PCIBUS6,
600+
"PCAN_PCIBUS7": PCAN_PCIBUS7,
601+
"PCAN_PCIBUS8": PCAN_PCIBUS8,
602+
"PCAN_PCIBUS9": PCAN_PCIBUS9,
603+
"PCAN_PCIBUS10": PCAN_PCIBUS10,
604+
"PCAN_PCIBUS11": PCAN_PCIBUS11,
605+
"PCAN_PCIBUS12": PCAN_PCIBUS12,
606+
"PCAN_PCIBUS13": PCAN_PCIBUS13,
607+
"PCAN_PCIBUS14": PCAN_PCIBUS14,
608+
"PCAN_PCIBUS15": PCAN_PCIBUS15,
609+
"PCAN_PCIBUS16": PCAN_PCIBUS16,
610+
"PCAN_USBBUS1": PCAN_USBBUS1,
611+
"PCAN_USBBUS2": PCAN_USBBUS2,
612+
"PCAN_USBBUS3": PCAN_USBBUS3,
613+
"PCAN_USBBUS4": PCAN_USBBUS4,
614+
"PCAN_USBBUS5": PCAN_USBBUS5,
615+
"PCAN_USBBUS6": PCAN_USBBUS6,
616+
"PCAN_USBBUS7": PCAN_USBBUS7,
617+
"PCAN_USBBUS8": PCAN_USBBUS8,
618+
"PCAN_USBBUS9": PCAN_USBBUS9,
619+
"PCAN_USBBUS10": PCAN_USBBUS10,
620+
"PCAN_USBBUS11": PCAN_USBBUS11,
621+
"PCAN_USBBUS12": PCAN_USBBUS12,
622+
"PCAN_USBBUS13": PCAN_USBBUS13,
623+
"PCAN_USBBUS14": PCAN_USBBUS14,
624+
"PCAN_USBBUS15": PCAN_USBBUS15,
625+
"PCAN_USBBUS16": PCAN_USBBUS16,
626+
"PCAN_PCCBUS1": PCAN_PCCBUS1,
627+
"PCAN_PCCBUS2": PCAN_PCCBUS2,
628+
"PCAN_LANBUS1": PCAN_LANBUS1,
629+
"PCAN_LANBUS2": PCAN_LANBUS2,
630+
"PCAN_LANBUS3": PCAN_LANBUS3,
631+
"PCAN_LANBUS4": PCAN_LANBUS4,
632+
"PCAN_LANBUS5": PCAN_LANBUS5,
633+
"PCAN_LANBUS6": PCAN_LANBUS6,
634+
"PCAN_LANBUS7": PCAN_LANBUS7,
635+
"PCAN_LANBUS8": PCAN_LANBUS8,
636+
"PCAN_LANBUS9": PCAN_LANBUS9,
637+
"PCAN_LANBUS10": PCAN_LANBUS10,
638+
"PCAN_LANBUS11": PCAN_LANBUS11,
639+
"PCAN_LANBUS12": PCAN_LANBUS12,
640+
"PCAN_LANBUS13": PCAN_LANBUS13,
641+
"PCAN_LANBUS14": PCAN_LANBUS14,
642+
"PCAN_LANBUS15": PCAN_LANBUS15,
643+
"PCAN_LANBUS16": PCAN_LANBUS16,
644+
}
645+
646+
556647
# ///////////////////////////////////////////////////////////
557648
# PCAN-Basic API function declarations
558649
# ///////////////////////////////////////////////////////////
@@ -601,8 +692,7 @@ def Initialize(
601692
Interrupt=c_ushort(0),
602693
):
603694

604-
"""
605-
Initializes a PCAN Channel
695+
"""Initializes a PCAN Channel
606696
607697
Parameters:
608698
Channel : A TPCANHandle representing a PCAN Channel
@@ -627,8 +717,7 @@ def Initialize(
627717
#
628718
def InitializeFD(self, Channel, BitrateFD):
629719

630-
"""
631-
Initializes a FD capable PCAN Channel
720+
"""Initializes a FD capable PCAN Channel
632721
633722
Parameters:
634723
Channel : The handle of a FD capable PCAN Channel
@@ -659,8 +748,7 @@ def InitializeFD(self, Channel, BitrateFD):
659748
#
660749
def Uninitialize(self, Channel):
661750

662-
"""
663-
Uninitializes one or all PCAN Channels initialized by CAN_Initialize
751+
"""Uninitializes one or all PCAN Channels initialized by CAN_Initialize
664752
665753
Remarks:
666754
Giving the TPCANHandle value "PCAN_NONEBUS", uninitialize all initialized channels
@@ -682,8 +770,7 @@ def Uninitialize(self, Channel):
682770
#
683771
def Reset(self, Channel):
684772

685-
"""
686-
Resets the receive and transmit queues of the PCAN Channel
773+
"""Resets the receive and transmit queues of the PCAN Channel
687774
688775
Remarks:
689776
A reset of the CAN controller is not performed
@@ -705,8 +792,7 @@ def Reset(self, Channel):
705792
#
706793
def GetStatus(self, Channel):
707794

708-
"""
709-
Gets the current status of a PCAN Channel
795+
"""Gets the current status of a PCAN Channel
710796
711797
Parameters:
712798
Channel : A TPCANHandle representing a PCAN Channel
@@ -725,8 +811,7 @@ def GetStatus(self, Channel):
725811
#
726812
def Read(self, Channel):
727813

728-
"""
729-
Reads a CAN message from the receive queue of a PCAN Channel
814+
"""Reads a CAN message from the receive queue of a PCAN Channel
730815
731816
Remarks:
732817
The return value of this method is a 3-touple, where
@@ -740,7 +825,7 @@ def Read(self, Channel):
740825
Channel : A TPCANHandle representing a PCAN Channel
741826
742827
Returns:
743-
A touple with three values
828+
A tuple with three values
744829
"""
745830
try:
746831
msg = TPCANMsg()
@@ -755,8 +840,7 @@ def Read(self, Channel):
755840
#
756841
def ReadFD(self, Channel):
757842

758-
"""
759-
Reads a CAN message from the receive queue of a FD capable PCAN Channel
843+
"""Reads a CAN message from the receive queue of a FD capable PCAN Channel
760844
761845
Remarks:
762846
The return value of this method is a 3-touple, where
@@ -770,7 +854,7 @@ def ReadFD(self, Channel):
770854
Channel : The handle of a FD capable PCAN Channel
771855
772856
Returns:
773-
A touple with three values
857+
A tuple with three values
774858
"""
775859
try:
776860
msg = TPCANMsgFD()
@@ -785,8 +869,7 @@ def ReadFD(self, Channel):
785869
#
786870
def Write(self, Channel, MessageBuffer):
787871

788-
"""
789-
Transmits a CAN message
872+
"""Transmits a CAN message
790873
791874
Parameters:
792875
Channel : A TPCANHandle representing a PCAN Channel
@@ -806,8 +889,7 @@ def Write(self, Channel, MessageBuffer):
806889
#
807890
def WriteFD(self, Channel, MessageBuffer):
808891

809-
"""
810-
Transmits a CAN message over a FD capable PCAN Channel
892+
"""Transmits a CAN message over a FD capable PCAN Channel
811893
812894
Parameters:
813895
Channel : The handle of a FD capable PCAN Channel
@@ -827,8 +909,7 @@ def WriteFD(self, Channel, MessageBuffer):
827909
#
828910
def FilterMessages(self, Channel, FromID, ToID, Mode):
829911

830-
"""
831-
Configures the reception filter
912+
"""Configures the reception filter
832913
833914
Remarks:
834915
The message filter will be expanded with every call to this function.
@@ -855,8 +936,7 @@ def FilterMessages(self, Channel, FromID, ToID, Mode):
855936
#
856937
def GetValue(self, Channel, Parameter):
857938

858-
"""
859-
Retrieves a PCAN Channel value
939+
"""Retrieves a PCAN Channel value
860940
861941
Remarks:
862942
Parameters can be present or not according with the kind
@@ -872,7 +952,7 @@ def GetValue(self, Channel, Parameter):
872952
Parameter : The TPCANParameter parameter to get
873953
874954
Returns:
875-
A touple with 2 values
955+
A tuple with 2 values
876956
"""
877957
try:
878958
if (
@@ -912,9 +992,8 @@ def GetValue(self, Channel, Parameter):
912992
#
913993
def SetValue(self, Channel, Parameter, Buffer):
914994

915-
"""
916-
Returns a descriptive text of a given TPCANStatus error
917-
code, in any desired language
995+
"""Returns a descriptive text of a given TPCANStatus error
996+
code, in any desired language
918997
919998
Remarks:
920999
Parameters can be present or not according with the kind
@@ -951,8 +1030,7 @@ def SetValue(self, Channel, Parameter, Buffer):
9511030

9521031
def GetErrorText(self, Error, Language=0):
9531032

954-
"""
955-
Configures or sets a PCAN Channel value
1033+
"""Configures or sets a PCAN Channel value
9561034
9571035
Remarks:
9581036
@@ -969,7 +1047,7 @@ def GetErrorText(self, Error, Language=0):
9691047
Language : Indicates a 'Primary language ID' (Default is Neutral(0))
9701048
9711049
Returns:
972-
A touple with 2 values
1050+
A tuple with 2 values
9731051
"""
9741052
try:
9751053
mybuffer = create_string_buffer(256)
@@ -981,8 +1059,7 @@ def GetErrorText(self, Error, Language=0):
9811059

9821060
def LookUpChannel(self, Parameters):
9831061

984-
"""
985-
Finds a PCAN-Basic channel that matches with the given parameters
1062+
"""Finds a PCAN-Basic channel that matches with the given parameters
9861063
9871064
Remarks:
9881065
@@ -995,7 +1072,7 @@ def LookUpChannel(self, Parameters):
9951072
to be matched within a PCAN-Basic channel
9961073
9971074
Returns:
998-
A touple with 2 values
1075+
A tuple with 2 values
9991076
"""
10001077
try:
10011078
mybuffer = TPCANHandle(0)

0 commit comments

Comments
 (0)