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.
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.
Group Signal Lines Within Component
You can group signals into a virtual bus within a component by using Bus Creator blocks.
Open the example model, which contains three 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.
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.
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
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
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.
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.
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 example model, which contains three source blocks.
Add an Out Bus Element block to the model. Then, connect the Chirp Signal block to the Out Bus Element block.
To make two copies of the block, hold 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 Use Existing Port each time you copy the block.
Connect the Sine Wave and Step blocks to the Out Bus Element blocks.
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
To make identifying elements of a port easier, rename them. Optionally, create hierarchy by nesting a bus element in other buses. Add a dot after each bus name. For example:
signal1. Then, enter
signal2. Then, enter
signal3. Then, enter
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 is a nested bus that contains elements named
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.
Specify Multiple Elements at Input Port
When a model or subsystem file requires a bus at an input port, define the port with an In Bus Element block.
To specify the elements of an input 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
For you to specify a
Simulink.Bus object as the data type of an
input bus at a bus element port, an In Bus Element block must select the
bus or an element of the bus.
This example shows how to define an input bus element port at an interface without a
Suppose the model component expects an input bus that contains a nested bus named
NestedBus and a signal named
Step. The nested bus
contains signals named
In a blank model or subsystem, add an In Bus Element block. This block creates an input port for the model or subsystem.
To rename the element, in the block label, double-click
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 .
In the label of the new block, double-click
signal1. Then, enter
The dot specifies another level of hierarchy, putting an element named
Chirp in a nested bus named
In the Property Inspector or dialog box, select the element named
NestedBus. Then, click the button arrow and select Add element without
Double-click the new element name. Then, enter
By adding elements without adding blocks, you can define the entire bus hierarchy with
only one In Bus Element block and without a
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 reorder the elements after creation, drag them to a new position. For example, drag
Optionally, to specify the properties of an element, pause on the element name. Then, click the pencil button that appears.
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.
To edit the properties or specify additional properties, click the properties next to the element name.
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 .
Corresponding In Bus Element blocks appear in the block diagram.
For more examples and information, see In 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.