88#
99# ------------------------------------------------------------------
1010# Author : Keneth Wagner
11+ # Last change: 2022-07-06
1112# ------------------------------------------------------------------
1213#
13- # Copyright (C) 1999-2021 PEAK-System Technik GmbH, Darmstadt
14+ # Copyright (C) 1999-2022 PEAK-System Technik GmbH, Darmstadt
1415# more Info at http://www.peak-system.com
1516
1617# Module Imports
1718from ctypes import *
1819from ctypes .util import find_library
19- from string import *
2020import platform
2121
2222import logging
282282PCAN_ATTACHED_CHANNELS = TPCANParameter (
283283 0x2B
284284) # Get information about PCAN channels attached to a system
285+ PCAN_ALLOW_ECHO_FRAMES = TPCANParameter (
286+ 0x2C
287+ ) # Echo messages reception status within a PCAN-Channel
288+ PCAN_DEVICE_PART_NUMBER = TPCANParameter (
289+ 0x2D
290+ ) # Get the part number associated to a device
285291
286292# DEPRECATED parameters
287293#
354360 33
355361) # Maximum length of the name of a device: 32 characters + terminator
356362MAX_LENGTH_VERSION_STRING = int (
357- 18
358- ) # Maximum length of a version string: 17 characters + terminator
363+ 256
364+ ) # Maximum length of a version string: 255 characters + terminator
359365
360366# PCAN message types
361367#
377383PCAN_MESSAGE_ESI = TPCANMessageType (
378384 0x10
379385) # The PCAN message represents a FD error state indicator(CAN FD transmitter was error active)
386+ PCAN_MESSAGE_ECHO = TPCANMessageType (
387+ 0x20
388+ ) # The PCAN message represents an echo CAN Frame
380389PCAN_MESSAGE_ERRFRAME = TPCANMessageType (
381390 0x40
382391) # The PCAN message represents an error frame
@@ -654,33 +663,38 @@ class PCANBasic:
654663 """PCAN-Basic API class implementation"""
655664
656665 def __init__ (self ):
657- # Loads the PCANBasic API
658- #
659666 if platform .system () == "Windows" :
660- # Loads the API on Windows
661- _dll_path = find_library ("PCANBasic" )
662- self .__m_dllBasic = windll .LoadLibrary (_dll_path ) if _dll_path else None
663- aReg = winreg .ConnectRegistry (None , winreg .HKEY_LOCAL_MACHINE )
664- try :
665- aKey = winreg .OpenKey (aReg , r"SOFTWARE\PEAK-System\PEAK-Drivers" )
666- winreg .CloseKey (aKey )
667- except OSError :
668- logger .error ("Exception: The PEAK-driver couldn't be found!" )
669- finally :
670- winreg .CloseKey (aReg )
671- elif "CYGWIN" in platform .system ():
672- self .__m_dllBasic = cdll .LoadLibrary ("PCANBasic.dll" )
673- # Unfortunately cygwin python has no winreg module, so we can't
674- # check for the registry key.
675- elif platform .system () == "Linux" :
676- # Loads the API on Linux
677- self .__m_dllBasic = cdll .LoadLibrary ("libpcanbasic.so" )
667+ load_library_func = windll .LoadLibrary
668+
669+ # look for Peak drivers in Windows registry
670+ with winreg .ConnectRegistry (None , winreg .HKEY_LOCAL_MACHINE ) as reg :
671+ try :
672+ with winreg .OpenKey (reg , r"SOFTWARE\PEAK-System\PEAK-Drivers" ):
673+ pass
674+ except OSError :
675+ raise OSError ("The PEAK-driver could not be found!" ) from None
676+ else :
677+ load_library_func = cdll .LoadLibrary
678+
679+ if platform .system () == "Windows" or "CYGWIN" in platform .system ():
680+ lib_name = "PCANBasic.dll"
678681 elif platform .system () == "Darwin" :
679- self .__m_dllBasic = cdll .LoadLibrary (find_library ("libPCBUSB.dylib" ))
682+ # PCBUSB library is a third-party software created
683+ # and maintained by the MacCAN project
684+ lib_name = "libPCBUSB.dylib"
680685 else :
681- self .__m_dllBasic = cdll .LoadLibrary ("libpcanbasic.so" )
682- if self .__m_dllBasic is None :
683- logger .error ("Exception: The PCAN-Basic DLL couldn't be loaded!" )
686+ lib_name = "libpcanbasic.so"
687+
688+ lib_path = find_library (lib_name )
689+ if not lib_path :
690+ raise OSError (f"{ lib_name } library not found." )
691+
692+ try :
693+ self .__m_dllBasic = load_library_func (lib_path )
694+ except OSError :
695+ raise OSError (
696+ f"The PCAN-Basic API could not be loaded. ({ lib_path } )"
697+ ) from None
684698
685699 # Initializes a PCAN Channel
686700 #
@@ -965,6 +979,7 @@ def GetValue(self, Channel, Parameter):
965979 or Parameter == PCAN_BITRATE_INFO_FD
966980 or Parameter == PCAN_IP_ADDRESS
967981 or Parameter == PCAN_FIRMWARE_VERSION
982+ or Parameter == PCAN_DEVICE_PART_NUMBER
968983 ):
969984 mybuffer = create_string_buffer (256 )
970985
@@ -974,6 +989,12 @@ def GetValue(self, Channel, Parameter):
974989 return (TPCANStatus (res [0 ]),)
975990 mybuffer = (TPCANChannelInformation * res [1 ])()
976991
992+ elif (
993+ Parameter == PCAN_ACCEPTANCE_FILTER_11BIT
994+ or PCAN_ACCEPTANCE_FILTER_29BIT
995+ ):
996+ mybuffer = c_int64 (0 )
997+
977998 else :
978999 mybuffer = c_int (0 )
9791000
@@ -1017,6 +1038,11 @@ def SetValue(self, Channel, Parameter, Buffer):
10171038 or Parameter == PCAN_TRACE_LOCATION
10181039 ):
10191040 mybuffer = create_string_buffer (256 )
1041+ elif (
1042+ Parameter == PCAN_ACCEPTANCE_FILTER_11BIT
1043+ or PCAN_ACCEPTANCE_FILTER_29BIT
1044+ ):
1045+ mybuffer = c_int64 (0 )
10201046 else :
10211047 mybuffer = c_int (0 )
10221048
0 commit comments