8
8
#
9
9
# ------------------------------------------------------------------
10
10
# Author : Keneth Wagner
11
+ # Last change: 2022-07-06
11
12
# ------------------------------------------------------------------
12
13
#
13
- # Copyright (C) 1999-2021 PEAK-System Technik GmbH, Darmstadt
14
+ # Copyright (C) 1999-2022 PEAK-System Technik GmbH, Darmstadt
14
15
# more Info at http://www.peak-system.com
15
16
16
17
# Module Imports
17
18
from ctypes import *
18
19
from ctypes .util import find_library
19
- from string import *
20
20
import platform
21
21
22
22
import logging
282
282
PCAN_ATTACHED_CHANNELS = TPCANParameter (
283
283
0x2B
284
284
) # 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
285
291
286
292
# DEPRECATED parameters
287
293
#
354
360
33
355
361
) # Maximum length of the name of a device: 32 characters + terminator
356
362
MAX_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
359
365
360
366
# PCAN message types
361
367
#
377
383
PCAN_MESSAGE_ESI = TPCANMessageType (
378
384
0x10
379
385
) # 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
380
389
PCAN_MESSAGE_ERRFRAME = TPCANMessageType (
381
390
0x40
382
391
) # The PCAN message represents an error frame
@@ -654,33 +663,38 @@ class PCANBasic:
654
663
"""PCAN-Basic API class implementation"""
655
664
656
665
def __init__ (self ):
657
- # Loads the PCANBasic API
658
- #
659
666
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"
678
681
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"
680
685
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
684
698
685
699
# Initializes a PCAN Channel
686
700
#
@@ -965,6 +979,7 @@ def GetValue(self, Channel, Parameter):
965
979
or Parameter == PCAN_BITRATE_INFO_FD
966
980
or Parameter == PCAN_IP_ADDRESS
967
981
or Parameter == PCAN_FIRMWARE_VERSION
982
+ or Parameter == PCAN_DEVICE_PART_NUMBER
968
983
):
969
984
mybuffer = create_string_buffer (256 )
970
985
@@ -974,6 +989,12 @@ def GetValue(self, Channel, Parameter):
974
989
return (TPCANStatus (res [0 ]),)
975
990
mybuffer = (TPCANChannelInformation * res [1 ])()
976
991
992
+ elif (
993
+ Parameter == PCAN_ACCEPTANCE_FILTER_11BIT
994
+ or PCAN_ACCEPTANCE_FILTER_29BIT
995
+ ):
996
+ mybuffer = c_int64 (0 )
997
+
977
998
else :
978
999
mybuffer = c_int (0 )
979
1000
@@ -1017,6 +1038,11 @@ def SetValue(self, Channel, Parameter, Buffer):
1017
1038
or Parameter == PCAN_TRACE_LOCATION
1018
1039
):
1019
1040
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 )
1020
1046
else :
1021
1047
mybuffer = c_int (0 )
1022
1048
0 commit comments