Rest bus simulation CAN scripting API

Prerequisites

Create a new project and configure the Hardware device(s) as a first step.

Creating nodes by Drag and Drop

After adding a CAN database, drag and drop an ECU to the Nodes element.

../_images/CAN_drag_drop_ecu.png

Node properties

Select the created ECU node and open the Properties View to show and change the adjustments to simulate the ECU:

  • Sender and Receiver.

    ../_images/CAN_propertiesnode.png

Enable and disable frames

  • By default all frames of the ECU node are deactivated, i.e. after the creation of a node the frames needed for the simulation must be activated.

  • Each frame has an enable flag in properties, which specifies whether it is activated or not.

../_images/frame.png
  • In order to activate all frames, right click on the transmitted_Frames and/ or received_Frames element and select Activate.

../_images/frame_activate.png

Properties of a frame

Each frame has further information which is read out of the CAN database file and is used during the simulation.

../_images/frame_properties.png

Starting and stopping ECU nodes

There are three ways on how to start and stop ECU nodes:

  1. Start all ECU nodes via context menu of Nodes element.

  2. Start only one ECU node via context menu of an ECU node.

../_images/CAN_ecu_start.png

3. Start Nodes in scripts.

# start individual node using its name
DSU.start()

# stop individual node using its name
DSU.stop()

# iterate over all nodes and start/stop them
for ecu in tc_get_all_ecus():
    ecu.start()

Example using get_value(), set_value() and reset_value() methods

from time import sleep

# enable a frame using frame name
DSU.enable("ACC_HUD")
# enable a frame using frame identifier
DSU.enable("742")
# enable all frames of the ECU
DSU.enable("*")

DSU.start()

##################################################################
#                          Get Value                             #
##################################################################
### 1- get_value(frame : str)###
# get value of all signals of a frame using frame name
DSU.get_value("LEAD_INFO")
# get value of all signals of a frame using frame identifier
DSU.get_value("742")

### 2- get_value(frame : str, signal : str)###
# get value of one signal of a frame using frame name
DSU.get_value("LEAD_INFO", "LEAD_REL_SPEED")
# get value of one signal of a frame using frame identifier
DSU.get_value("742", "LEAD_REL_SPEED")

##################################################################
#                          Set Value                             #
##################################################################
### 1- set_value(frame : str, value : IDictionary)###
# set multiple signals using frame name 
DSU.set_value("LEAD_INFO", {"LEAD_REL_SPEED": 2, "LEAD_LONG_DIST": 3})
# set multiple signals using frame identifier 
DSU.set_value("742", {"LEAD_REL_SPEED": 2, "LEAD_LONG_DIST": 3})
# set multiple signals without knowing the name of the frame
DSU.set_value("*", {"LEAD_REL_SPEED": 2, "LEAD_LONG_DIST": 3})
# set a signal with None will reset its value
DSU.set_value("ACC_HUD", {"FCW": None, "SET_ME_X20": 6})

### 2- set_value(frame : str, signal : str, value : int or None)###
# set a signal using frame name 
DSU.set_value("LEAD_INFO", "LEAD_REL_SPEED", 2)
# set a signal using frame identifier 
DSU.set_value("742", "LEAD_REL_SPEED", 2)
# set a signal without knowing the name of the frame
DSU.set_value("*", "LEAD_REL_SPEED", 2)
# set a signal with None will reset its value
DSU.set_value("ACC_HUD", "FCW", None)

##################################################################
#                         Reset Value                            #
##################################################################
# reset one signal using frame name
DSU.reset_value("LEAD_INFO", "LEAD_REL_SPEED")
# reset one signal using frame identifier
DSU.reset_value("742", "LEAD_REL_SPEED")
# reset a signal without knowing the name of the frame
DSU.reset_value("*", "FCW")
# Reset all signal values of a frame
DSU.set_value("ACC_HUD", None)
DSU.reset_value("ACC_HUD", "*")
DSU.reset_value("ACC_HUD")
# reset all signals of all frames
DSU.reset_value()
DSU.reset_value("*", "*")


# disable a frame using frame name
DSU.disable("ACC_HUD")
# disable a frame using frame identifier
DSU.disable("742")
# disable all frames of the ECU
DSU.disable("*")

DSU.stop()

Example using trigger() method

from time import sleep

# enable frame LEAD_INFO
DSU.enable("LEAD_INFO")
print("Starting RBS node using the start() method")
DSU.start()
sleep(2)

# set value of signal LEAD_REL_SPEED.
DSU.set_value("LEAD_INFO", "LEAD_REL_SPEED", 1)
print("triggering frame")
DSU.trigger("LEAD_INFO")
sleep(2)

# disable frame LEAD_INFO
DSU.disable("LEAD_INFO")

# stop DSU
DSU.stop()

Example using on_value_changed() method

from time import sleep

# ---  variables
nb = 0
# --Functions


def on_value_changed(ecu, evt):
    global nb, evt_name, old_val, new_val
    # print evt
    if evt.match("LEAD_INFO"):
        print(evt.message_name)
        evt_name = evt.message_name
        print("old value")
        print(evt.old_value)
        old_val = evt.old_value
        print("new value")
        print(evt.new_value)
        new_val = evt.new_value
        nb = nb + 1


# Enable the frame LEAD_INFO
DSU.enable("LEAD_INFO")
HCU.enable("LEAD_INFO")

print("Starting RBS node using the start() method")
DSU.start()
HCU.start()
sleep(2)
value = {"LEAD_REL_SPEED": 2, "LEAD_LONG_DIST": 3}

HCU.on_value_changed += on_value_changed

DSU.set_value("LEAD_INFO", value)
DSU.trigger("LEAD_INFO")
sleep(2)

# disable the frame LEAD_INFO
DSU.disable("LEAD_INFO")
HCU.disable("LEAD_INFO")

# stop the ecus
DSU.stop()
HCU.stop()