CAPL Script

SetBusContext

This reference collection includes Vector documentation reproduced with permission. Snippets may require configuration-specific symbols and are not all standalone programs. Sources and validation limits.

Change the context for a specific operation

SetBusContext changes the bus context associated with the CAPL block. Context-dependent calls can then resolve to a different bus or node-layer instance. This is useful in multi-bus configurations, but a helper that leaves the context changed can also affect the caller's next operation.

Keep a temporary switch small and explicit: resolve the target, save the old context, perform the intended work, and restore the old context before returning.

Syntax and return value

dword SetBusContext(dword context);

The argument is the new context. The return value is the context that was active before the call. It is not a Boolean success result. Use GetBusNameContext to resolve a configured bus name and check its zero-result failure case before switching.

Changing the context is not itself a command to transmit a frame or bring a controller online. Check the separate operation's documentation and its execution context before adding it.

Example: switch, log and restore

This simulation-node helper illustrates context lifetime without changing controller state. Replace BodyBus with a configured name. The example has not been compiled or executed in your CANoe environment.

void inspectBodyBusContext()
{
  char busName[32] = "BodyBus";
  dword targetContext;
  dword previousContext;

  targetContext = GetBusNameContext(busName);
  if (targetContext == 0)
  {
    write("Bus lookup failed: %s", busName);
    return;
  }

  previousContext = SetBusContext(targetContext);
  write("Temporary context: %u", GetBusContext());

  SetBusContext(previousContext);
  if (GetBusContext() != previousContext)
  {
    write("Context restoration check failed");
  }
}

on key 'c'
{
  inspectBodyBusContext();
}

The early return occurs before the switch. If you add branches after the switch, each exit must restore previousContext. Saving the return value in a local variable also avoids overwriting a shared saved value when one helper calls another.

What to check in your project

  • Before the call: Record the caller's context with GetBusContext.
  • Inside the switched section: Inspect the context and verify the intended operation separately. A context number alone does not prove that the expected bus traffic was produced.
  • After the helper: Confirm that the caller's context matches the original value.
  • Missing target: Use an absent bus name in an isolated test and verify that the helper returns without switching.

Start with this synchronous inspection flow. Do not assume that a callback or later event inherits the context you happened to set in another handler; check the documentation for that callback and inspect its context at entry.

Source and validation boundary

The signature and previous-context return value follow the Vector SetBusContext reference, reproduced with permission. The save/restore explanation and inspection example are site guidance, not a claim of vendor certification or a recorded test run. See sources and permissions.