Main Content

Group Signals or Messages into Virtual Buses

To reduce line complexity and clutter in a block diagram and make incrementally changing an interface easier, use virtual buses. A virtual bus is analogous to a bundle of wires held together by tie wraps. For comparison, a nonvirtual bus is analogous to a structure in C code. You can access the bus as a whole or select specific elements from the bus.

When you create a virtual bus, group lines based on their functionality. By organizing signals or messages into logical groupings, you reduce the likelihood of significant refactoring.

Not all blocks can accept buses, and some blocks implicitly convert buses to vectors. To learn which blocks support which types of buses, see Bus-Capable Blocks. To identify bus conversions, see Identify Automatic Bus Conversions.

How you create virtual buses differs based on the location of the signals or messages that you want to group.

  • Within a component — Use Bus Creator blocks.

  • At the output interface of a component — Use Out Bus Element blocks.

  • At the input interface of a component — Use In Bus Element blocks.

To focus on fundamental steps, these examples are simple. However, buses are most useful when you have many signals or messages to group.

Tip

When you open a model or create a bus, the Simulink® Editor does not display bus line styles. To update the line styles, compile the model. In the Simulink Toolstrip, on the Modeling tab, click Update Model or Run.

To create buses at the input or output interface of a component using functions, see Programmatically Create Bus Element Ports.

Group Signal Lines Within Component

You can group signals into a virtual bus within a component by using Bus Creator blocks.

Open the model named VirtualBusCreation, which contains three blocks.

Model with three source blocks

To create a bus that contains the outputs from multiple blocks, click and drag to select the blocks. For this example, select the Chirp Signal and Sine Wave blocks. In the action bar that appears, click Create Bus.

Pointer paused on Create Bus button in action bar

The software adds a Bus Creator block and connects the inputs to that block. The output of the Bus Creator block is a virtual bus.

First two source blocks connected to a Bus Creator block

To make identifying the elements of the bus easier, label the inputs to the Bus Creator block. Double-click the line between the Chirp Signal block and the Bus Creator block. Then, enter Chirp. Double-click the line between the Sine Wave block and the Bus Creator block. Then, enter Sine.

To create a second bus that contains the first bus and the output of the Step block, click and drag to select the Bus Creator and Step blocks. In the action bar that appears, click Create Bus. Since the Sine and Chirp signals are elements of the input bus, the software creates the same bus regardless of whether your selection includes the Sine Wave and Chirp Signal blocks.

The software adds another Bus Creator block and connects the inputs to that block. The output of the Bus Creator block is a virtual bus that contains a nested bus.

First Bus Creator block and third source block connected to another Bus Creator block

You can nest buses to any depth. If one of the inputs to a Bus Creator block is a bus, then its output is a bus hierarchy that contains at least one nested bus.

Label the inputs to the new Bus Creator block. Double-click the line between the Step block and the Bus Creator block. Then, enter Step. Double-click the line between the Bus Creator blocks. Then, enter NestedBus. Optionally, arrange the blocks to improve the readability of the model.

Connect the output of the second Bus Creator block to a Scope block and label the output TopBus. To visually identify the buses, compile the model by clicking Update Model or Run on the Modeling tab of the Simulink Toolstrip. Compiling the model updates the line styles.

Compiled model with bus line style at output of Bus Creator blocks

Connect Multiple Output Signals to a Port

This example shows how to group the output signals of a subsystem or model into a virtual bus using Out Bus Element blocks.

Open the model named VirtualBusOutputCreation, which contains three source blocks.

Model with three source blocks

Add an Out Bus Element block to the model. Then, connect the Chirp Signal block to the Out Bus Element block.

First source block connected to an Out Bus Element block labeled OutBus.signal1

To make two copies of the block, press Ctrl and drag the Out Bus Element block twice. When copying the block this way, specify whether to create a new port or use the existing port. To create one output bus that contains all the signals, select New Element each time you copy the block.

Menu with pointer on New Element

Connect the Sine Wave and Step blocks to the Out Bus Element blocks.

Each source block connected to a default Out Bus Element block

The label next to each Out Bus Element block has two parts. The first part of the label describes the port and uses the default name OutBus. The second part of the label describes a bus element and uses the default name signal1.

To make identifying elements of a port easier, rename the elements. Optionally, create hierarchy by nesting a bus element in other buses. Add a dot after each bus name. For example:

  • Double-click signal1. Then, enter NestedBus.Chirp.

  • Double-click signal2. Then, enter NestedBus.Sine.

  • Double-click signal3. Then, enter Step.

Out Bus Element blocks labeled OutBus.NestedBus.Chirp, OutBus.NestedBus.Sine, and OutBus.Step with cursor after Step

To inspect the bus hierarchy at the port, double-click any of the Out Bus Element blocks. The dialog box displays a top-level bus named OutBus that contains elements named NestedBus and Step. NestedBus is a nested bus that contains elements named Chirp and Sine.

Bus hierarchy with NestedBus and Step under OutBus and Chirp and Sine under NestedBus

While this example connects a signal to each Out Bus Element block, you can connect a bus to an Out Bus Element block.

The three Out Bus Element blocks create a virtual bus at the output port of the model. To see the virtual bus, reference the model in another model or move the Out Bus Element blocks into a subsystem. The line styles update when you compile the model. To compile the model, in the Simulink Toolstrip, on the Modeling tab, click Update Model or Run.

Bus output from a Model block that references the example model

Specify Multiple Elements of a Port

When a model or subsystem file requires a bus at a port, define the port with an In Bus Element or Out Bus Element block.

To specify the elements of a bus element port at an interface, you can:

  • Add elements to the interface with or without adding blocks to the block diagram.

  • Define the bus hierarchy with a Simulink.Bus object.

Note

To specify a Simulink.Bus object as the data type of a bus at a bus element port, an In Bus Element or Out Bus Element block must represent the bus or an element of the bus.

This example shows how to define an input bus element port at an interface with one block and no Simulink.Bus object.

Suppose the port expects a bus that contains a nested bus named NestedBus and a signal named Step. The nested bus contains signals named Chirp and Sine. Your model or subsystem uses only the signal named Step currently.

In a blank model or subsystem, add an In Bus Element block. This block creates an input port.

Default In Bus Element block with block label that says InBus.signal1

To rename the element that the block represents, in the block label, double-click signal1. Then, replace signal1 with Step.

In Bus Element block with block label that says InBus.Step. The part of the label that says Step is in edit mode.

Open the Property Inspector. Then, select the In Bus Element block. Alternatively, double-click the block to open a dialog box.

In the Property Inspector or dialog box, select the top-level bus named InBus. Then, click the Add element button arrow, and select Add element without block.

Pointer paused on Add element without block button

The new element is nested under the previously selected element. The block diagram is unchanged.

InBus with new element named signal1

The new element name is gray and italicized to indicate that an In Bus Element block does not represent the element directly.

Double-click the new element name. Then, enter NestedBus.

Bus hierarchy with NestedBus in edit mode

To add an element to the nested bus, select NestedBus. Then, click the Add element button arrow, and select Add element without block.

NestedBus selected and pointer paused on Add element without block button

To add another bus element, select NestedBus. Then, click the Add element button arrow, and select Add element without block.

NestedBus selected and pointer paused on Add element without block button

To rename the bus elements from signal and signal2 to Chirp and Sine, respectively, double-click each new element name and enter the new name.

Bus hierarchy with Chirp and Sine under NestedBus

By adding elements without adding blocks, you can define the entire bus hierarchy with only one In Bus Element block and without a Simulink.Bus object.

Tip

When you want to add many elements without blocks, using the Simulink.Bus.addElementToPort function can be quicker than using the dialog box. For an example, see Programmatically Create Bus Element Ports.

To specify the properties of an element, pause on the element name. Then, click the pencil button Specify attributes that appears.

Pencil button next to element name

The nondefault attributes appear next to the element name. To further edit the attributes, click an attribute summary next to an element name.

Bus hierarchy that has elements with nondefault properties

Defining the properties of elements at an interface is useful when you create a file for reuse. The interface definition determines the input that a referenced subsystem or model supports. The hierarchy and properties of the input bus must match the definition at the corresponding port.

Suppose you add elements without blocks then decide that you want In Bus Element blocks that select them. To add blocks for input elements, click an element name or hold Ctrl as you click multiple element names. Then, click Add blocks for selected elements. For example, add blocks for Chirp and Sine.

Pointer paused on Add blocks for selected signals button

Corresponding In Bus Element blocks appear in the block diagram.

In Bus Element blocks labeled InBus.NestedBus.Chirp and InBus.NestedBus.Sine

In the block label, each dot indicates a new level of hierarchy.

For output bus element ports, the way to add blocks for elements differs slightly. To add blocks for output bus element ports, select an element that an existing Out Bus Element block does not represent. Then, add an Out Bus Element block by clicking Add block for selected element. To prevent a conflict where multiple Out Bus Element blocks represent the same element, you can add only one Out Bus Element block at a time.

For more examples and information, see In Bus Element and Out Bus Element.

Access Elements of Buses

You can access the bus as a whole or select specific signals, messages, or nested buses from the bus.

How you extract elements from a bus depends on the location of the bus.

See Also

| | |

Related Topics