Skip to content

Specific Exceptions: Adapting pcan interface #1152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 7, 2021
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
155 changes: 116 additions & 39 deletions can/interfaces/pcan/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,12 @@
#
# ------------------------------------------------------------------
# Author : Keneth Wagner
# Last change: 14.01.2021 Wagner
#
# Language: Python 2.7, 3.7
# ------------------------------------------------------------------
#
# Copyright (C) 1999-2021 PEAK-System Technik GmbH, Darmstadt
# more Info at http://www.peak-system.com
#

# Module Imports
#
from ctypes import *
from ctypes.util import find_library
from string import *
Expand Down Expand Up @@ -553,6 +548,102 @@ class TPCANChannelInformation(Structure):
] # Availability status of a PCAN-Channel


# ///////////////////////////////////////////////////////////
# Additional objects
# ///////////////////////////////////////////////////////////

PCAN_BITRATES = {
1000000: PCAN_BAUD_1M,
800000: PCAN_BAUD_800K,
500000: PCAN_BAUD_500K,
250000: PCAN_BAUD_250K,
125000: PCAN_BAUD_125K,
100000: PCAN_BAUD_100K,
95000: PCAN_BAUD_95K,
83000: PCAN_BAUD_83K,
50000: PCAN_BAUD_50K,
47000: PCAN_BAUD_47K,
33000: PCAN_BAUD_33K,
20000: PCAN_BAUD_20K,
10000: PCAN_BAUD_10K,
5000: PCAN_BAUD_5K,
}

PCAN_FD_PARAMETER_LIST = (
"nom_brp",
"nom_tseg1",
"nom_tseg2",
"nom_sjw",
"data_brp",
"data_tseg1",
"data_tseg2",
"data_sjw",
)

PCAN_CHANNEL_NAMES = {
"PCAN_NONEBUS": PCAN_NONEBUS,
"PCAN_ISABUS1": PCAN_ISABUS1,
"PCAN_ISABUS2": PCAN_ISABUS2,
"PCAN_ISABUS3": PCAN_ISABUS3,
"PCAN_ISABUS4": PCAN_ISABUS4,
"PCAN_ISABUS5": PCAN_ISABUS5,
"PCAN_ISABUS6": PCAN_ISABUS6,
"PCAN_ISABUS7": PCAN_ISABUS7,
"PCAN_ISABUS8": PCAN_ISABUS8,
"PCAN_DNGBUS1": PCAN_DNGBUS1,
"PCAN_PCIBUS1": PCAN_PCIBUS1,
"PCAN_PCIBUS2": PCAN_PCIBUS2,
"PCAN_PCIBUS3": PCAN_PCIBUS3,
"PCAN_PCIBUS4": PCAN_PCIBUS4,
"PCAN_PCIBUS5": PCAN_PCIBUS5,
"PCAN_PCIBUS6": PCAN_PCIBUS6,
"PCAN_PCIBUS7": PCAN_PCIBUS7,
"PCAN_PCIBUS8": PCAN_PCIBUS8,
"PCAN_PCIBUS9": PCAN_PCIBUS9,
"PCAN_PCIBUS10": PCAN_PCIBUS10,
"PCAN_PCIBUS11": PCAN_PCIBUS11,
"PCAN_PCIBUS12": PCAN_PCIBUS12,
"PCAN_PCIBUS13": PCAN_PCIBUS13,
"PCAN_PCIBUS14": PCAN_PCIBUS14,
"PCAN_PCIBUS15": PCAN_PCIBUS15,
"PCAN_PCIBUS16": PCAN_PCIBUS16,
"PCAN_USBBUS1": PCAN_USBBUS1,
"PCAN_USBBUS2": PCAN_USBBUS2,
"PCAN_USBBUS3": PCAN_USBBUS3,
"PCAN_USBBUS4": PCAN_USBBUS4,
"PCAN_USBBUS5": PCAN_USBBUS5,
"PCAN_USBBUS6": PCAN_USBBUS6,
"PCAN_USBBUS7": PCAN_USBBUS7,
"PCAN_USBBUS8": PCAN_USBBUS8,
"PCAN_USBBUS9": PCAN_USBBUS9,
"PCAN_USBBUS10": PCAN_USBBUS10,
"PCAN_USBBUS11": PCAN_USBBUS11,
"PCAN_USBBUS12": PCAN_USBBUS12,
"PCAN_USBBUS13": PCAN_USBBUS13,
"PCAN_USBBUS14": PCAN_USBBUS14,
"PCAN_USBBUS15": PCAN_USBBUS15,
"PCAN_USBBUS16": PCAN_USBBUS16,
"PCAN_PCCBUS1": PCAN_PCCBUS1,
"PCAN_PCCBUS2": PCAN_PCCBUS2,
"PCAN_LANBUS1": PCAN_LANBUS1,
"PCAN_LANBUS2": PCAN_LANBUS2,
"PCAN_LANBUS3": PCAN_LANBUS3,
"PCAN_LANBUS4": PCAN_LANBUS4,
"PCAN_LANBUS5": PCAN_LANBUS5,
"PCAN_LANBUS6": PCAN_LANBUS6,
"PCAN_LANBUS7": PCAN_LANBUS7,
"PCAN_LANBUS8": PCAN_LANBUS8,
"PCAN_LANBUS9": PCAN_LANBUS9,
"PCAN_LANBUS10": PCAN_LANBUS10,
"PCAN_LANBUS11": PCAN_LANBUS11,
"PCAN_LANBUS12": PCAN_LANBUS12,
"PCAN_LANBUS13": PCAN_LANBUS13,
"PCAN_LANBUS14": PCAN_LANBUS14,
"PCAN_LANBUS15": PCAN_LANBUS15,
"PCAN_LANBUS16": PCAN_LANBUS16,
}


# ///////////////////////////////////////////////////////////
# PCAN-Basic API function declarations
# ///////////////////////////////////////////////////////////
Expand Down Expand Up @@ -601,8 +692,7 @@ def Initialize(
Interrupt=c_ushort(0),
):

"""
Initializes a PCAN Channel
"""Initializes a PCAN Channel

Parameters:
Channel : A TPCANHandle representing a PCAN Channel
Expand All @@ -627,8 +717,7 @@ def Initialize(
#
def InitializeFD(self, Channel, BitrateFD):

"""
Initializes a FD capable PCAN Channel
"""Initializes a FD capable PCAN Channel

Parameters:
Channel : The handle of a FD capable PCAN Channel
Expand Down Expand Up @@ -659,8 +748,7 @@ def InitializeFD(self, Channel, BitrateFD):
#
def Uninitialize(self, Channel):

"""
Uninitializes one or all PCAN Channels initialized by CAN_Initialize
"""Uninitializes one or all PCAN Channels initialized by CAN_Initialize

Remarks:
Giving the TPCANHandle value "PCAN_NONEBUS", uninitialize all initialized channels
Expand All @@ -682,8 +770,7 @@ def Uninitialize(self, Channel):
#
def Reset(self, Channel):

"""
Resets the receive and transmit queues of the PCAN Channel
"""Resets the receive and transmit queues of the PCAN Channel

Remarks:
A reset of the CAN controller is not performed
Expand All @@ -705,8 +792,7 @@ def Reset(self, Channel):
#
def GetStatus(self, Channel):

"""
Gets the current status of a PCAN Channel
"""Gets the current status of a PCAN Channel

Parameters:
Channel : A TPCANHandle representing a PCAN Channel
Expand All @@ -725,8 +811,7 @@ def GetStatus(self, Channel):
#
def Read(self, Channel):

"""
Reads a CAN message from the receive queue of a PCAN Channel
"""Reads a CAN message from the receive queue of a PCAN Channel

Remarks:
The return value of this method is a 3-touple, where
Expand All @@ -740,7 +825,7 @@ def Read(self, Channel):
Channel : A TPCANHandle representing a PCAN Channel

Returns:
A touple with three values
A tuple with three values
"""
try:
msg = TPCANMsg()
Expand All @@ -755,8 +840,7 @@ def Read(self, Channel):
#
def ReadFD(self, Channel):

"""
Reads a CAN message from the receive queue of a FD capable PCAN Channel
"""Reads a CAN message from the receive queue of a FD capable PCAN Channel

Remarks:
The return value of this method is a 3-touple, where
Expand All @@ -770,7 +854,7 @@ def ReadFD(self, Channel):
Channel : The handle of a FD capable PCAN Channel

Returns:
A touple with three values
A tuple with three values
"""
try:
msg = TPCANMsgFD()
Expand All @@ -785,8 +869,7 @@ def ReadFD(self, Channel):
#
def Write(self, Channel, MessageBuffer):

"""
Transmits a CAN message
"""Transmits a CAN message

Parameters:
Channel : A TPCANHandle representing a PCAN Channel
Expand All @@ -806,8 +889,7 @@ def Write(self, Channel, MessageBuffer):
#
def WriteFD(self, Channel, MessageBuffer):

"""
Transmits a CAN message over a FD capable PCAN Channel
"""Transmits a CAN message over a FD capable PCAN Channel

Parameters:
Channel : The handle of a FD capable PCAN Channel
Expand All @@ -827,8 +909,7 @@ def WriteFD(self, Channel, MessageBuffer):
#
def FilterMessages(self, Channel, FromID, ToID, Mode):

"""
Configures the reception filter
"""Configures the reception filter

Remarks:
The message filter will be expanded with every call to this function.
Expand All @@ -855,8 +936,7 @@ def FilterMessages(self, Channel, FromID, ToID, Mode):
#
def GetValue(self, Channel, Parameter):

"""
Retrieves a PCAN Channel value
"""Retrieves a PCAN Channel value

Remarks:
Parameters can be present or not according with the kind
Expand All @@ -872,7 +952,7 @@ def GetValue(self, Channel, Parameter):
Parameter : The TPCANParameter parameter to get

Returns:
A touple with 2 values
A tuple with 2 values
"""
try:
if (
Expand Down Expand Up @@ -912,9 +992,8 @@ def GetValue(self, Channel, Parameter):
#
def SetValue(self, Channel, Parameter, Buffer):

"""
Returns a descriptive text of a given TPCANStatus error
code, in any desired language
"""Returns a descriptive text of a given TPCANStatus error
code, in any desired language

Remarks:
Parameters can be present or not according with the kind
Expand Down Expand Up @@ -951,8 +1030,7 @@ def SetValue(self, Channel, Parameter, Buffer):

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

"""
Configures or sets a PCAN Channel value
"""Configures or sets a PCAN Channel value

Remarks:

Expand All @@ -969,7 +1047,7 @@ def GetErrorText(self, Error, Language=0):
Language : Indicates a 'Primary language ID' (Default is Neutral(0))

Returns:
A touple with 2 values
A tuple with 2 values
"""
try:
mybuffer = create_string_buffer(256)
Expand All @@ -981,8 +1059,7 @@ def GetErrorText(self, Error, Language=0):

def LookUpChannel(self, Parameters):

"""
Finds a PCAN-Basic channel that matches with the given parameters
"""Finds a PCAN-Basic channel that matches with the given parameters

Remarks:

Expand All @@ -995,7 +1072,7 @@ def LookUpChannel(self, Parameters):
to be matched within a PCAN-Basic channel

Returns:
A touple with 2 values
A tuple with 2 values
"""
try:
mybuffer = TPCANHandle(0)
Expand Down
Loading