UNLOCK THE PYTHON-POWER OF YOUR CAN APPLICATIONS WITH THE SLSS CANANALYSER PYTHON CLASS
The SLSS CANAnalyser Python Class* is a game-changer for developers looking to harness the potential of CAN communication with minimal effort. Designed with simplicity and efficiency in mind, this interface allows users to seamlessly integrate their applications with the powerful SLSS CANAnalyser software, removing the complexity traditionally associated with CAN protocol implementations.
INTUITIVE AND EASY-TO-USE DESIGN
One of the standout features of this Python interface is its intuitive design, which empowers developers of all skill levels to quickly start sending and receiving CAN messages. Whether you’re new to CAN communication or a seasoned expert, this class makes it remarkably straightforward to build custom solutions. With just a few lines of code, you can establish a connection, transmit messages, and process incoming data, giving you the flexibility to focus on creating innovative applications rather than wrestling with technical hurdles.
ADVANTAGES OVER COMPETING SOLUTIONS
Compared to competing solutions, the SLSS CANAnalyser Python Class offers unparalleled ease of use and adaptability. Many existing tools are burdened with steep learning curves, limited documentation, or proprietary restrictions that can slow down development. In contrast, this Python interface provides clear, human-readable logging and debug outputs, keeping developers informed at every stage of the process. Its open, flexible design ensures that it can be customized to suit the unique needs of your projects, all while maintaining a level of transparency that fosters confidence and control.
OVERVIEW OF THE AVAILABLE CLASS FUNCTIONS
The class functions are largely based on the standard TCP/IP commands and have only been converted into Python-compliant semantics and extended by some internal convenience functions. The following overview provides an insight into all available functions.
IDEAS AND EXAMPLES
Below, we present a selection of practical examples that demonstrate how to leverage this class for common use cases. These examples are designed to be easily adaptable, helping you kickstart your own CAN-based applications with minimal effort. From establishing a connection and sending messages to processing incoming data, these scenarios showcase the versatility of the SLSS CANAnalyser TCP/IP Class and its ability to streamline your development workflow.
The SLSS CANAnalyser TCP/IP Python Class* and the listed examples are also available for download in the download area*.
Basic: Set up the TCP/IP Interface connection, configure basic settings and search for a connected Dongle
""" Example script for establishing a connection to the SLSS CANAnalyser software and for manipulating some basic settings in the program. This example serves as an introduction to using the SLSS CANAnalyser TCP/IP Class. IMPORTANT: Please ensure that the TCP/IP interface is activated in the SLSS CANAnalyser. """ __author__ = "Sebastian Langer" __company__ = "Sebastian Langer Software Solutions UG (haftungsbeschraenkt), SeRoSys Technology LLC" __credits__ = "Sebastian Langer" __dev_date__ = "2024.09.15" __version__ = "1.0.0.0" __email__ = "info@langer-sebastian.de, slanger@serosys-tech.com" __status__ = "production" __copyright__ = "Sebastian Langer @ Sebastian Langer Software Solutions UG (haftungsbeschraenkt)" """ USER SETTINGS AND SLSS CANANALYSER INITIALIZATION """ from cls.cl_SLSS_CANAnalyser_TCPIP_Interface import CANAnalyserTCPIP # import the class to the current script slss_com = CANAnalyserTCPIP("127.0.0.1", 49836, True, True) # Create an instance using the local host IP (this computer) and port 49836 # (must match the port set in the SLSS CANAnalyser) with immediate establishment # of the connection and activated log output. """ SLSS CANANALYSER COMMANDS """ slss_com.set_FD_custom_state("AB", False) # deactivate the FD custom settings slss_com.set_FD_active_state("AB", False) # set FD active state to False to use standard CAN 2.0 only slss_com.set_speed("A", "500") # set CAN speed of channel A to 500kbit/s slss_com.set_speed("B", "250") # set CAN speed of channel B to 250kbit/s slss_com.set_mode("AB", "normal") # set bus interaction mode for both bus channels to normal slss_com.unmute_CAN("A") # un-mute CAN channel A slss_com.mute_CAN("B") # mute CAN channel B slss_com.search_module() # search for connected module if "not connected" in slss_com.get_connection(): # check if a dongle connection could be established print("ERROR: No Dongle connection could be established --> execution aborted!") exit(1) else: print("Well done! The settings have been set and the connection to the Dongle has been successfully established!")
Basic: Set up CAN FD custom settings via the TCP/IP interface
""" Example script for establishing a connection to the SLSS CANAnalyser software and demonstrates a specified COM port connection and custom CAN FD configuration as well as showing TCP/IP monitored CAN data. This example serves as an introduction to using the SLSS CANAnalyser TCP/IP Class. IMPORTANT: Please ensure that the TCP/IP interface is activated in the SLSS CANAnalyser. """ __author__ = "Ron Zlotnik" __company__ = "Sebastian Langer Software Solutions UG (haftungsbeschraenkt), SeRoSys Technology LLC" __credits__ = "Sebastian Langer and Ron Zlotnik" __dev_date__ = "2025.01.20" __version__ = "1.0.0.0" __email__ = "info@langer-sebastian.de, slanger@serosys-tech.com, rzlotnik@serosys-tech.com" __status__ = "production" __copyright__ = "Sebastian Langer @ Sebastian Langer Software Solutions UG (haftungsbeschraenkt)" """ USER SETTINGS AND SLSS CANANALYSER INITIALIZATION """ from cls.cl_SLSS_CANAnalyser_TCPIP_Interface import CANAnalyserTCPIP # import the class to the current script slss_com = CANAnalyserTCPIP("127.0.0.1", 49836, True, True) # Create an instance using the local host IP (this computer) and port 49836 # (must match the port set in the SLSS CANAnalyser) with immediate establishment # of the connection and activated log output. """ SLSS CANANALYSER COMMANDS """ slss_com.disconnect_module() # Begins by disconnecting a dongle (in case one is already connected) slss_com.set_FD_custom_state("AB", True) # Activate the FD custom settings slss_com.set_FD_custom_settings("AB", "80", 127, 32, 32, 12, 1, 1, 15, 4, 4) # Configuration parameters for the custom FD settings slss_com.set_FD_active_state("AB", True) # set FD active state to True to use CAN FD slss_com.set_speed("A", "1000") # set CAN speed of channel A to 1000kbit/s slss_com.set_speed("B", "500") # set CAN speed of channel B to 500kbit/s slss_com.set_mode("AB", "normal") # set bus interaction mode for both bus channels to normal slss_com.mute_CAN("AB") # mute CAN channels A and B to send CAN data over TCP/IP slss_com.connect_module(3) # Connect to Dongle which has is assigned to COM port 3 slss_com.set_runstate("play") if "not connected" in slss_com.get_connection(): # check if a dongle connection could be established print("ERROR: No Dongle connection could be established --> execution aborted!") exit(1) else: print("Well done! The settings have been set and the connection to the Dongle has been successfully established!")
Basic: Start a measurement recording and save the log file after a certain time has elapsed
""" Example script for establishing a connection to the SLSS CANAnalyser software and demonstrates instructing a logging function following by a timer, stopping the log and exporting it to a .CSV file. This example serves as an introduction to using the SLSS CANAnalyser TCP/IP Class. IMPORTANT: Please ensure that the TCP/IP interface is activated in the SLSS CANAnalyser. """ __author__ = "Ron Zlotnik" __company__ = "Sebastian Langer Software Solutions UG (haftungsbeschraenkt), SeRoSys Technology LLC" __credits__ = "Sebastian Langer and Ron Zlotnik" __dev_date__ = "2025.01.20" __version__ = "1.0.0.0" __email__ = "info@langer-sebastian.de, slanger@serosys-tech.com, rzlotnik@serosys-tech.com" __status__ = "production" __copyright__ = "Sebastian Langer @ Sebastian Langer Software Solutions UG (haftungsbeschraenkt)" """ USER SETTINGS AND SLSS CANANALYSER INITIALIZATION """ import time from cls.cl_SLSS_CANAnalyser_TCPIP_Interface import CANAnalyserTCPIP # import the class to the current script slss_com = CANAnalyserTCPIP("langersebastian.no-ip.org", 49600, True, True) # Create an instance using the local host IP (this computer) and port 49836 # (must match the port set in the SLSS CANAnalyser) with immediate establishment # of the connection and activated log output. """ SLSS CANANALYSER COMMANDS """ slss_com.disconnect_module() # Begins by disconnecting a dongle (in case one is already connected) slss_com.set_FD_custom_state("AB", False) # Deactivate the FD custom settings slss_com.set_FD_active_state("AB", False) # set FD active state to False for use of CAN 2.0 slss_com.set_speed("A", "500") # set CAN speed of channel A to 500kbit/s slss_com.set_speed("B", "500") # set CAN speed of channel B to 500kbit/s slss_com.set_mode("AB", "normal") # set bus interaction mode for both bus channels to normal slss_com.mute_CAN("AB") # mute CAN channels A and B slss_com.connect_module(25) # Connect to Dongle which has is assigned to COM port 3 slss_com.set_runstate("play") if "not connected" in slss_com.get_connection(): # check if a dongle connection could be established print("ERROR: No Dongle connection could be established --> execution aborted!") exit(1) else: print("Well done! The settings have been set and the connection to the Dongle has been successfully established!") slss_com.rec_start() # Starts logging the data print("Logging data for 5 seconds!") time.sleep(5) # Delay for 5 seconds. slss_com.rec_stop("C:\\Users\\Public\\Documents\\Bus_record.csv", False, ";") # Stop log recording and save file in csv format with ; as delimiter slss_com.disconnect_module() # Disconnects the dongle when finished logging print("You may review your CSV formated data stored from the log export!")
Basic: Change hardware setup from CAN2.0 to CANFD with standard bitrate multiplicator
""" Example script for establishing a connection to the SLSS CANAnalyser software and demonstrates a change in hardware setup from CAN2.0 to CANFD with a multiplicator after 5 seconds. This example serves as an introduction to using the SLSS CANAnalyser TCP/IP Class. IMPORTANT: Please ensure that the TCP/IP interface is activated in the SLSS CANAnalyser. """ __author__ = "Ron Zlotnik" __company__ = "Sebastian Langer Software Solutions UG (haftungsbeschraenkt), SeRoSys Technology LLC" __credits__ = "Sebastian Langer and Ron Zlotnik" __dev_date__ = "2025.01.20" __version__ = "1.0.0.0" __email__ = "info@langer-sebastian.de, slanger@serosys-tech.com, rzlotnik@serosys-tech.com" __status__ = "production" __copyright__ = "Sebastian Langer @ Sebastian Langer Software Solutions UG (haftungsbeschraenkt)" """ USER SETTINGS AND SLSS CANANALYSER INITIALIZATION """ import time from cls.cl_SLSS_CANAnalyser_TCPIP_Interface import CANAnalyserTCPIP # import the class to the current script slss_com = CANAnalyserTCPIP("127.0.0.1", 49836, True, True) # Create an instance using the local host IP (this computer) and port 49836 # (must match the port set in the SLSS CANAnalyser) with immediate establishment # of the connection and activated log output. """ SLSS CANANALYSER COMMANDS """ slss_com.disconnect_module() # Begins by disconnecting a dongle (in case one is already connected) slss_com.set_FD_custom_state("AB", False) # Deactivate the FD custom settings slss_com.set_FD_active_state("AB", False) # set FD active state to False for use of CAN 2.0 slss_com.set_speed("A", "1000") # set CAN speed of channel A to 1000kbit/s slss_com.set_speed("B", "500") # set CAN speed of channel B to 500kbit/s slss_com.set_mode("AB", "normal") # set bus interaction mode for both bus channels to normal slss_com.mute_CAN("AB") # mute CAN channels A and B slss_com.connect_module(3) # Connect to Dongle which has is assigned to COM port 3 slss_com.set_runstate("play") if "not connected" in slss_com.get_connection(): # check if a dongle connection could be established print("ERROR: No Dongle connection could be established --> execution aborted!") exit(1) else: print("Well done! The settings have been set and the connection to the Dongle has been successfully established!") print("Beginning a 5 second delay until disconnecting and reconnecting with different settings") time.sleep(5) # Delay for 5 seconds. slss_com.disconnect_module() # Begins by disconnecting a dongle (in case one is already connected) slss_com.set_FD_custom_state("AB", False) # Do not activate the FD custom settings slss_com.set_FD_active_state("AB", True) # set FD active state to True to use CAN FD slss_com.set_FD_multiplicator("AB", 8) # set multiplicator value for channel A and B to 8 (8 times of the current arbitration speed) slss_com.set_speed("A", "500") # set CAN speed of channel A to 500kbit/s slss_com.set_speed("B", "500") # set CAN speed of channel B to 500kbit/s slss_com.mute_CAN("AB") # mute CAN channels A and B slss_com.connect_module(3) # Connect to Dongle which has is assigned to COM port 3 slss_com.set_runstate("play") if "not connected" in slss_com.get_connection(): # check if a dongle connection could be established print("ERROR: No Dongle connection could be established --> execution aborted!") exit(1) else: print("Well done! The settings have been set and the connection to the Dongle has been successfully established!")
Basic: Change bus interaction mode for channel A and channel B
""" Example script for establishing a connection to the SLSS CANAnalyser software and demonstrates changing the bus interaction mode from Channel A to Channel B. This example serves as an introduction to using the SLSS CANAnalyser TCP/IP Class. IMPORTANT: Please ensure that the TCP/IP interface is activated in the SLSS CANAnalyser. """ __author__ = "Ron Zlotnik" __company__ = "Sebastian Langer Software Solutions UG (haftungsbeschraenkt), SeRoSys Technology LLC" __credits__ = "Sebastian Langer and Ron Zlotnik" __dev_date__ = "2025.01.20" __version__ = "1.0.0.0" __email__ = "info@langer-sebastian.de, slanger@serosys-tech.com, rzlotnik@serosys-tech.com" __status__ = "production" __copyright__ = "Sebastian Langer @ Sebastian Langer Software Solutions UG (haftungsbeschraenkt)" """ USER SETTINGS AND SLSS CANANALYSER INITIALIZATION """ import time from cls.cl_SLSS_CANAnalyser_TCPIP_Interface import CANAnalyserTCPIP # import the class to the current script slss_com = CANAnalyserTCPIP("127.0.0.1", 49836, True, True) # Create an instance using the local host IP (this computer) and port 49836 # (must match the port set in the SLSS CANAnalyser) with immediate establishment # of the connection and activated log output. """ SLSS CANANALYSER COMMANDS """ slss_com.disconnect_module() # Begins by disconnecting a dongle (in case one is already connected) slss_com.set_FD_custom_state("AB", False) # Deactivate the FD custom settings slss_com.set_FD_active_state("AB", False) # set FD active state to False for use of CAN 2.0 slss_com.set_speed("A", "500") # set CAN speed of channel A to 500kbit/s slss_com.set_speed("B", "500") # set CAN speed of channel B to 500kbit/s slss_com.set_mode("A", "normal") # set bus interaction mode for channel A to normal slss_com.set_mode("B", "off") # set bus interaction mode for channel B to off slss_com.unmute_CAN("AB") # unmute CAN channels A and B and show received CAN traffic over TCP/IP slss_com.connect_module(3) # Connect to Dongle which has is assigned to COM port 3 slss_com.set_runstate("play") if "not connected" in slss_com.get_connection(): # check if a dongle connection could be established print("ERROR: No Dongle connection could be established --> execution aborted!") exit(1) else: print("Well done! The settings have been set and the connection to the Dongle has been successfully established!") print("Beginning a 5 second delay until disconnecting and reconnecting with different settings") time.sleep(5) # Delay for 5 seconds. slss_com.disconnect_module() # Begins by disconnecting a dongle (in case one is already connected) slss_com.set_FD_custom_state("AB", False) # Deactivate the FD custom settings slss_com.set_FD_active_state("AB", False) # set FD active state to False for use of CAN 2.0 slss_com.set_speed("A", "500") # set CAN speed of channel A to 500kbit/s slss_com.set_speed("B", "500") # set CAN speed of channel B to 500kbit/s slss_com.set_mode("B", "normal") # set bus interaction mode for channel B to normal slss_com.set_mode("A", "off") # set bus interaction mode for channel A to off slss_com.mute_CAN("AB") # mute CAN channels A and B slss_com.connect_module(3) # Connect to Dongle which has is assigned to COM port 3 slss_com.set_runstate("play") if "not connected" in slss_com.get_connection(): # check if a dongle connection could be established print("ERROR: No Dongle connection could be established --> execution aborted!") exit(1) else: print("Well done! The settings have been set and the connection to the Dongle has been successfully established!")
Basic: Switch between monitoring both channels, to channel B and then to channel A
""" Example script for establishing a connection to the SLSS CANAnalyser software and demonstrates switching between monitoring both channels, to channel B, then to channel A. This example serves as an introduction to using the SLSS CANAnalyser TCP/IP Class. IMPORTANT: Please ensure that the TCP/IP interface is activated in the SLSS CANAnalyser. """ __author__ = "Ron Zlotnik" __company__ = "Sebastian Langer Software Solutions UG (haftungsbeschraenkt), SeRoSys Technology LLC" __credits__ = "Sebastian Langer and Ron Zlotnik" __dev_date__ = "2025.01.20" __version__ = "1.0.0.0" __email__ = "info@langer-sebastian.de, slanger@serosys-tech.com, rzlotnik@serosys-tech.com" __status__ = "production" __copyright__ = "Sebastian Langer @ Sebastian Langer Software Solutions UG (haftungsbeschraenkt)" """ USER SETTINGS AND SLSS CANANALYSER INITIALIZATION """ import time from cls.cl_SLSS_CANAnalyser_TCPIP_Interface import CANAnalyserTCPIP # import the class to the current script slss_com = CANAnalyserTCPIP("127.0.0.1", 49836, True, True) # Create an instance using the local host IP (this computer) and port 49836 # (must match the port set in the SLSS CANAnalyser) with immediate establishment # of the connection and activated log output. """ SLSS CANANALYSER COMMANDS """ slss_com.disconnect_module() # Begins by disconnecting a dongle (in case one is already connected) slss_com.set_FD_custom_state("AB", False) # Deactivate the FD custom settings slss_com.set_FD_active_state("AB", False) # set FD active state to False for use of CAN 2.0 slss_com.set_speed("A", "500") # set CAN speed of channel A to 500kbit/s slss_com.set_speed("B", "500") # set CAN speed of channel B to 500kbit/s slss_com.set_mode("AB", "normal") # set bus interaction mode for both channels to normal slss_com.mute_CAN("AB") # mute CAN channels A and B to send CAN data over TCP/IP slss_com.connect_module(3) # Connect to Dongle which has is assigned to COM port 3 slss_com.set_runstate("play") if "not connected" in slss_com.get_connection(): # check if a dongle connection could be established print("ERROR: No Dongle connection could be established --> execution aborted!") exit(1) else: print("Well done! The settings have been set and the connection to the Dongle has been successfully established!") print("Currently monitoring both channels.") print("Beginning a 5 second delay until switching to monitor channel B only.") time.sleep(5) # Delay for 5 seconds. slss_com.set_channel("B") # monitor bus channel B only print("Currently monitoring channel B only.") print("Beginning a 5 second delay until switching to monitor channel B only.") time.sleep(5) # Delay for 5 seconds. slss_com.set_channel("A") # monitor bus channel A only print("Currently monitoring channel A only.")
Advanced: State machine for bus sleep violation detection with multiple loop of log file recording
""" Example script for using the SLSS CANAnalyser TCP/IP interface to monitor a CAN bus (channel A of the connected dongle) and to detect and record bus sleep violations. The script uses a simple state machine to first ensure that there is no more communication on the bus. After the bus sleep has been detected, the script starts the SLSS CANAnalyser log recorder and waits for new incoming messages. If new incoming messages are detected, the bus traffic is recorded for the time specified in “recording_time_s” and the log file is saved in the path specified in “log_file_path”. The cycle then starts again (waiting for the bus to go to sleep to start recording) until the number of cycles specified in “detections_before_exit” has been reached. """ __author__ = "Sebastian Langer" __company__ = "Sebastian Langer Software Solutions UG (haftungsbeschraenkt), SeRoSys Technology LLC" __credits__ = "Sebastian Langer" __dev_date__ = "2024.11.12" __version__ = "1.0.0.0" __email__ = "info@langer-sebastian.de, slanger@serosys-tech.com" __status__ = "productive" __copyright__ = "Sebastian Langer @ Sebastian Langer Software Solutions UG (haftungsbeschraenkt)" """ USER SETTINGS AND CANANALYSER INITIALIZATION """ import time from cls.cl_SLSS_CANAnalyser_TCPIP_Interface import CANAnalyserTCPIP ip_address = "127.0.0.1" # define ip address (use 127.0.0.1 for local use) port = 49836 # define port slss_com = CANAnalyserTCPIP(ip_address, port, True) # create instance with immediately establishing the connection but without log output dongle_com_port = 3 # the COM port of the connected Dongle recording_time_s = 60 # the recording time in seconds detections_before_exit = 2 # the amount of state machine loops before exit bus_check_interval_s = 1 # the interval how often the bus status should be checked log_file_path = r"C:\_Workspace\CANAnalyser files\Bus_monitoring_example\Bus_sleep_violation.rcdf" """ NO MORE USER CHANGES UNTIL HERE """ act_time = 0 # variable to store the current time (do not change) cur_loop = 0 # variable to store the current loop (do not change) level = 0 # variable to store the current level of the state machine (do not change) u_learn_counter = 0 # variable to store the counter to check steps multiple times before changing state (do not change) check_run_state = True # variable if run state check should be performed or not slss_com.unmute_CAN("A") # un-mute forwarded CAN messages for CAN channel A slss_com.mute_CAN("B") # mute forwarded of CAN messages for CAN channel B (not used in this example) if "COM" + str(dongle_com_port) not in slss_com.get_connection(): # check if Dongle on is connected to the specified COM port slss_com.connect_module(dongle_com_port) # if not connect, then connect if "COM" + str(dongle_com_port) not in slss_com.get_connection(): print(f"ERROR: No connection to the dongle on COM{dongle_com_port} could be established! Please check the selected COM port and start the test again...") exit(1) slss_com.set_channel("A") # select channel A only to monitor bus sleep on this channel (need to be done after establishing the connection) while True: # main infinite loop while check_run_state: # check if run state isn't play (play is necessary for the incoming message detection) status = slss_com.get_runstate() # get current run state if "play" not in status: print(f"CANAnalyser not in play mode: Try to set bus interaction mode to play and check again in {bus_check_interval_s}s") slss_com.set_runstate("play") # set play mode time.sleep(bus_check_interval_s) # wait interval time else: check_run_state = False # set run state check to false, because correct run state was verified break rcv = slss_com.receive_CAN_message(False, 100) # search for received CAN messages if level == 0: # level 0 of the state machine --> wait for bus sleep if isinstance(rcv, str) and len(rcv) > 0: # check if a bus message was received u_learn_counter = 0 # reset counter to initial value print(f"Bus communication is still active! Check bus communication again in {bus_check_interval_s}s") slss_com.flush_incoming_data() # flush message receive buffer time.sleep(bus_check_interval_s) # wait interval time elif isinstance(rcv, str): if u_learn_counter < 5: # check if u_learn_counter limit was reached u_learn_counter += 1 # increase u_learn_counter print(f"No more bus communication detected! Increase counter value to {u_learn_counter}/5 and check bus communication again in {bus_check_interval_s}s") time.sleep(bus_check_interval_s) else: print(f"Counter has reached maximum value and bus is still inactive, change to level 1") level = 1 elif level == 1: # level 1 of the state machine --> start recording slss_com.rec_start() # start recording CAN traffic print(f"Start recording CAN traffic and change to level 2") level = 2 elif level == 2: # of the state machine --> bus monitoring if len(rcv) == 0: # check if no bus message was received, then sleep is already active print(f"Bus still in sleep mode, check bus in {bus_check_interval_s}s again") time.sleep(bus_check_interval_s) else: print(f"ATTENTION: Bus sleep violation detected! Recording of CAN traffic for the next {recording_time_s}s") time.sleep(recording_time_s) print(f"Stop recording CAN traffic and save logfile to {log_file_path}") slss_com.rec_stop(log_file_path, False) # save the current logfile and do not overwrite existing files if cur_loop < detections_before_exit: # check if state machine should be restarted or program should exit print(f"Reset state machine to start next detection loop") cur_loop += 1 # increase current loop level = 0 # reset state machine slss_com.set_runstate("play") check_run_state = False else: print(f"Maximum state machine loops are reached, exit program") exit(0)
Advanced: User adjustable CAN Fuzzing example for Automotive Cyber Security testing
""" Example of a quick CAN fuzzing test via the SLSS CANAnalyser TCP/IP interface """ __author__ = "Sebastian Langer" __company__ = "Sebastian Langer Software Solutions UG (haftungsbeschraenkt), SeRoSys Technology LLC" __credits__ = "Sebastian Langer" __dev_date__ = "2025.01.10" __version__ = "1.0.0.0" __email__ = "info@langer-sebastian.de, slanger@serosys-tech.com" __status__ = "productive" __copyright__ = "Sebastian Langer @ Sebastian Langer Software Solutions UG (haftungsbeschraenkt)" """ USER SETTINGS AND SLSS CANANALYSER INITIALIZATION """ import random import time from cls.cl_SLSS_CANAnalyser_TCPIP_Interface import CANAnalyserTCPIP ip_address = "127.0.0.1" # define ip address (use 127.0.0.1 for local use) port = 49836 # define port slss_com = CANAnalyserTCPIP(ip_address, port, True) # create instance with immediately establishing the connection but without log output dongle_com_port = 3 # the COM port of the connected Dongle iMessageInterval_ms = 100 # delay time between two messages # Dictionary for user defined fuzzing messages # key: Provide a numeric value or string. Use a string with the flag "RAND" to create random id's within the range of CAN2.0 A/B. # To predefine the range of used ids, add underscore separated minimum and maximum values behind the RAND flag ("RAND_0_10", "RAND_25_1000", etc.) # value: Provide a list with 3 items. # item 1: numeric value of message generation loops with the given id and settings. Insert -1 to infinite loop this test setup # item 2: string value of message type identifiers. Add [EXT],[FD],[BRS] to specify the message type you would use for the execution # item 3: list of byte values. Each byte value provides different fuzzing methods. # The 5 main methods are: # - numeric value: to send a fix numeric value on this position # - string "RAND": to send a random byte value in the range between 0 and 255 # - string "INC": increase the byte value stepwise from 0 to 255 and restart if 255 is reached # - string "DEC": decrease the byte value stepwise from 255 to 0 and restart if 0 is reached # - dictionary: A dictionary with the manipulation method ("RAND", "INC", "DEC") as key and a list with two numerical values as minimum and # maximum range values. The method only uses values in this range dFuzzMessages = { 0x23: [256, "[FD]", ["INC", 0x12, "RAND", "DEC", {"INC": [10, 15]}, {"DEC": [110, 91]}, 0x22, {"RAND": [0, 10]}]], 0x36: [256, "[FD][BRS]", ["INC", 0x12, "RAND", "DEC", {"INC": [10, 15]}, {"DEC": [110, 91]}, 0x22, {"RAND": [0, 10]}]], "RAND": [10, "[EXT][FD][BRS]", ["DEC", {"DEC": [10, 5]}, {"INC": [0, 15]}, "DEC", "RAND", "RAND", "RAND", "RAND"]], "RAND_0_10": [100, "[FD][BRS]", [0x22, 0x0A, 0x45, 0xFF, 0x34, 0x27, 0x22, {"RAND": [0, 10]}]], "RAND_255_258": [100, "[EXT]", ["INC", 0x12, "RAND", "DEC", {"INC": [10, 15]}, {"DEC": [110, 91]}, 0x22, {"RAND": [0, 10]}]] } """ NO MORE USER CHANGES UNTIL HERE """ dOldDataStore = {} def get_rand_num(i_min=0, i_max=255): """ get a random number in range of i_min and i_max""" i_min = 0 if i_min < 0 else i_min i_max = 255 if i_max > 255 else i_max rand_num = random.randint(i_min, i_max) return rand_num def get_rand_id(is_extended, i_min=0, i_max=255): """ get a random ID in range of standard or extended id """ maxID = 2047 if not is_extended else 536870911 i_min = 0 if i_min < 0 else i_min i_max = i_max if i_max < maxID else maxID rand_id = random.randint(i_min, i_max) return rand_id if __name__ == '__main__': slss_com.mute_CAN("AB") # mute forwarded CAN messages for both CAN channels (not necessary in this example) if "COM" + str(dongle_com_port) not in slss_com.get_connection(): # check if Dongle on is connected to the specified COM port slss_com.connect_module(dongle_com_port) # if not connect, then connect if "COM" + str(dongle_com_port) not in slss_com.get_connection(): print(f"ERROR: No connection to the dongle on COM{dongle_com_port} could be established! Please check the selected COM port and start the test again...") exit(1) slss_com.set_channel("A") # select channel A only to monitor bus sleep on this channel (need to be done after establishing the connection) for hId, lMsg in dFuzzMessages.items(): # iterate over CAN id / message values if len(lMsg) != 3: # data length check for value list print(f"Value length error in message {str(hId)}!") else: bInfiniteFlag = True if lMsg[0] == -1 else False # detect infinite loop value and set to start value lMsg[0] = 1 if lMsg[0] == -1 else lMsg[0] # detect infinite loop value and set to start value cnt = 0 while cnt < lMsg[0]: # run until max value is reached lCANDataPayload = [] # variable to build send list out of message values for iBytePos in range(0, len(lMsg[2])): if isinstance(lMsg[2][iBytePos], int): # when a static numerical value is used lCANDataPayload.append(lMsg[2][iBytePos]) elif isinstance(lMsg[2][iBytePos], str): # when a string is used, check for commands if lMsg[2][iBytePos].upper() == "RAND": # when using random byte values lCANDataPayload.append(get_rand_num()) elif lMsg[2][iBytePos].upper() == "INC": # when incrementing a byte value if hId in dOldDataStore: # check if a k/v pair is already stored in data store num = dOldDataStore[hId][iBytePos] num = num + 1 if num < 255 else 0 # check if in range, else reset to 0 lCANDataPayload.append(num) else: lCANDataPayload.append(0) elif lMsg[2][iBytePos].upper() == "DEC": # when decrementing a byte value if hId in dOldDataStore: # check if a k/v pair is already stored in data store num = dOldDataStore[hId][iBytePos] num = num - 1 if num > 0 else 255 # check if in range, else reset to 255 lCANDataPayload.append(num) else: lCANDataPayload.append(255) elif isinstance(lMsg[2][iBytePos], dict): if "RAND" in str(lMsg[2][iBytePos].keys()).upper(): if isinstance(lMsg[2][iBytePos]["RAND"], list): startValue = lMsg[2][iBytePos]["RAND"][0] endValue = lMsg[2][iBytePos]["RAND"][1] if startValue > endValue: tempStartValue = startValue startValue = endValue endValue = tempStartValue num = get_rand_num(startValue, endValue) lCANDataPayload.append(num) elif "INC" in str(lMsg[2][iBytePos].keys()).upper(): if isinstance(lMsg[2][iBytePos]["INC"], list): startValue = lMsg[2][iBytePos]["INC"][0] endValue = lMsg[2][iBytePos]["INC"][1] if endValue < startValue: tempStartValue = startValue startValue = endValue endValue = tempStartValue if hId in dOldDataStore: # check if a k/v pair is already stored in data store num = dOldDataStore[hId][iBytePos] num = num + 1 if num < endValue else startValue # check if in range, else reset to 0 lCANDataPayload.append(num) else: lCANDataPayload.append(startValue) elif "DEC" in str(lMsg[2][iBytePos].keys()).upper(): if isinstance(lMsg[2][iBytePos]["DEC"], list): startValue = lMsg[2][iBytePos]["DEC"][0] endValue = lMsg[2][iBytePos]["DEC"][1] if endValue > startValue: tempStartValue = startValue startValue = endValue endValue = tempStartValue if hId in dOldDataStore: # check if a k/v pair is already stored in data store num = dOldDataStore[hId][iBytePos] num = num - 1 if num > endValue else startValue # check if in range, else reset to 0 lCANDataPayload.append(num) else: lCANDataPayload.append(startValue) dOldDataStore[hId] = lCANDataPayload cnt = 0 if bInfiniteFlag else cnt + 1 # detect infinite loop value and set to back to start value if necessary bExtIdent = True if "EXT" in lMsg[1] else False bFDMsg = True if "FD" in lMsg[1] else False bBRSused = True if "BRS" in lMsg[1] else False sCANDataPayload = "_".join(str(val) for val in lCANDataPayload) # check for ID manipulation iSendId = None if isinstance(hId, int): iSendId = hId elif isinstance(hId, str) and "RAND" in hId.upper(): if hId.count('_') > 0: iMinMaxValues = hId.split('_') iSendId = get_rand_id(bExtIdent, int(iMinMaxValues[1]), int(iMinMaxValues[2])) else: iSendId = get_rand_id(bExtIdent) else: continue slss_com.send_CAN_message("A", iSendId, lCANDataPayload, bExtIdent, bFDMsg, bBRSused) time.sleep(iMessageInterval_ms / 1000)