主要内容

连接模块

通过连接模型中的各模块以在这些模块之间建立关系。

您可以使用信号线连接大多数模块。信号线连接到模块上的端口。在仿真期间,信号从输出端口传输到连接的输入端口。

  • 您可以通过创建分支信号线将信号从一个模块的输出端口发送到多个模块的输入端口。

  • 对于某些模块,您可以更改模块上的输入端口数量。例如,要在同一图上绘制多个信号,您可以增加 Scope 模块上的输入端口数量。

有关更改信号线布局的信息,请参阅配置模型布局。有关如何对信号线加标签的信息,请参阅配置模型元素名称和标签

您可以通过捆绑信号来简化模块图。有关捆绑信号的信息,请参阅将信号或消息组合到虚拟总线中

相关模块无需信号线即可连接。有关如何连接相关模块的信息,请参阅在不使用信号线的情况下连接模块

用信号线连接模块

要使用一条信号线连接两个模块,请点击一个模块的输出端口上的端口符号 Port Symbol 并将其拖动到另一个模块的输入端口。

A Sine Wave block is to the left of a Gain block. They are not connected. The output port of the Sine Wave block and the input port of the Gain block face each other and are labeled. The output port is on the right side of the block, and the input port is on the left side of the block.

您可以使用以下快捷方式连接模块:

  • 要在不拖动信号线的情况下连接模块,请执行以下操作:

    1. 垂直对齐要连接的输出端口和输入端口,中间没有其他组件。如果端口已对齐,请移动其中一个模块以打破对齐,然后重新对齐它们。当您释放为对齐端口而移动的模块时,会出现连接预览。

      A Sine Wave block is to the left of a Gain block. They are not connected. The Sine Wave block output port is horizontally aligned with the Gain block input port. A faint blue horizontal arrow points from the output port to the input port.

    2. 要建立连接,请点击预览。

      The Sine Wave block output port connects to the Gain block input port with a solid black signal line.

  • 要在不拖动信号线或对齐端口的情况下连接模块,请执行以下操作:

    1. 点击要连接的输出端口的端口符号。端口提示符号 Port hint symbol 会出现在可连接到的其他组件的输入端口上。

      A Gain block is below and to the right of a Sine Wave block. The port symbol of the Sine Wave block output port is highlighted in blue, and there is a port hint symbol in front of the Gain block input port.

    2. 要查看连接预览,请将鼠标指针悬停在要连接的输入端口的端口提示符号上。

      A light blue line connects the Sine Wave block output port to the Gain block input port.

    3. 要建立连接,请点击预览。

      A solid black line connects the Sine Wave output port to the Gain block input port.

  • 要在不拖动信号线或对齐端口的情况下将多个模块连接到单个模块,请执行以下操作:

    1. 选择要连接其输出端口的模块,例如通过左键点击并拖动以在这些模块周围形成一个选择框。

    2. Ctrl 键并点击要连接其输入端口的模块。

    The video shows three In Bus Element blocks labeled myPort.A, myPort.C, and myPort.B simultaneously connecting to the same Subsystem block, which has input ports named A, B, and C. The block labeled myPort.A connects to port A, the block labeled myPort.C connects to port C, and the block labeled myPort.B connects to port B.

  • 要将一个模块的输入端口和输出端口都连接到一条现有信号线,请点击该模块并将其拖到该信号线上。定位模块,使端口符号 Port Symbol 位于信号线上。当输出端口符号消失且输入端口符号变为实心箭尖时,释放鼠标指针。

    The top image shows a pointer holding a Gain block over a signal line, with the port symbols a little above the signal line. The bottom image shows the Gain block over the signal line such that the ports are right on top of the signal line. The right port symbol is not visible, and the left port symbol is changed to a solid arrowhead.

要使一个信号线段变为对角线,请按 Shift 键并拖动连接到另一个信号线段的顶点。

A Sine Wave block connects to a Scope block. The Sine Wave block is higher on the canvas than the Scope block. The signal line between the blocks consists of three line segments joined at two vertices. The pointer is dragging the lower vertex to the right. The line segment connecting to the Sine Wave block and the line segment connecting to the Scope block are horizontal. The line segment joining the two horizontal segments is diagonal.

将单个输出端口连接到多个输入端口

要将一个输出端口连接到多个输入端口,请先将一个输入端口连接到该输出端口,然后建立分支信号线以连接到其他输入端口。

要建立分支信号线,请按 Ctrl 键,然后点击现有信号线并将其拖动到要连接的模块的输入端口。

A Sine Wave Block connected to a Gain block by a signal line, and a dotted line branching out of the signal line and reaching part of the way towards an unconnected second Gain block

提示

要建立分支信号线,您也可以点击未连接的输入端口的端口符号并将其拖到现有信号线上。

The top image shows a Sine Wave block connected to a Gain block by a signal line, and an unconnected second Gain block with a dotted line part of the way towards the signal line. Above the dotted line is the text "Drag to Existing Signal Line". The bottom image shows the Sine Wave block connected to both Gain blocks.

将多个模块的输出端口连接到单个模块

某些模块可以在其输入端口侧连接多条信号线。例如,要在一个图上绘制多个信号,您可以将多个模块的输出端口连接到单个 Scope 模块。

要将多个模块的输出端口连接到单个模块,请从要连接的每个输出端口将信号线拖动到单个模块的输入端口侧(左边缘)。当模块图标变为蓝色且端口号出现时,释放指针。系统会自动创建一个输入端口以连接该信号线。

Two Constant blocks are to the left of a Scope block. The Scope block only has one port, and one of the Constant blocks connects to it. A blue signal preview line runs from the other Constant block to the left side of the Scope block. The Scope block icon is blue, and the port number 2 is displayed next to the point where the preview line connects to the Scope block.

如果要连接其输出端口的模块尚未在模型中,要增加单个模块上的输入端口数量,请执行以下操作:

  1. 将指针悬停在该模块的输入端口侧(左边缘),直到指针旁边出现加号。

    Pointer at left edge of Scope block, with boxed plus symbol near pointer

  2. 然后,点击并将指针拖离模块。将出现一条虚线信号线,您可以将其连接到另一个模块的输出端口。

    Scope block with two input ports, with the top input port connected to signal line, and bottom input port unconnected

要删除一个输入端口,请点击其端口符号并按 Delete 键。

要指定一个模块的输入端口数量,请使用“模块参数”对话框或属性检查器的参数选项卡。

Scope block Property Inspector dialog box with the Parameters tab showing and the "Number of input ports" parameter text box highlighted

提示

对于某些模块,您可以在将模块添加到模型时输入输入端口数量。

The left image shows a Scope block with a prompt below it asking for the "Number of input ports" and the answer "2". The right image shows the resulting Scope block with two input ports.

连接子系统

要将信号发送到子系统中,该子系统必须包含与要发送到该子系统的信号数量相同的输入端口。

要从子系统接收信号,该子系统必须包含与要从子系统接收的信号数量相同的输出端口。

当您将一个空白子系统添加到模型时,该子系统默认包含一个输入端口和一个输出端口。

要连接子系统,请执行以下操作:

  1. 要进入子系统,双击 Subsystem 模块。

    Two unconnected Constant blocks on the left, an unconnected Subsystem block in the center, and two unconnected Scope blocks on the right

  2. 在子系统内部,添加所需数量的输入端口和输出端口。

    Inside the Subsystem block, there are two unconnected input ports on the left, two unconnected gain blocks in the center, and two unconnected output ports on the right. The two input ports display the numbers 1 and 2 on the block icons, and so do the two output ports.

  3. 在子系统内部,将输入端口和输出端口连接到子系统。

    Inside of the Subsystem block, the input port with the number 1 on the block icon connects to a Gain block, which connects to the output port with the number 1 on the block icon. The input port with the number 2 on the block icon connects to the other Gain block, which connects to the output port with the number 2 on the block icon.

  4. 要在模型层次结构中向上导航一个层级,请在 Simulink® 画布顶部的资源管理器栏中,点击 Subsystem 模块名称左侧的层级名称。

    Explorer Bar showing the hierarchy model->Subsystem, with the callout "Click" pointing to the model level

  5. 将要发送到子系统中的信号的信号线连接到 Subsystem 模块的输入端口。

    Subsystem 模块上的每个输入端口对应子系统内部的一个 Inport 模块。您可以通过端口号判断 Subsystem 模块上的哪个输入端口对应子系统内部的哪个 Inport 模块。

  6. 将要从子系统接收的信号的信号线连接到 Subsystem 模块的输出端口。

    Subsystem 模块上的每个输出端口对应子系统内部的一个 Outport 模块。

    The two input ports of the Subsystem block connect to two Constant blocks, and the two output ports connect to two Scope blocks. The input ports and output ports of the Subsystem block have the same numbers on the block icon as the input ports and output ports inside the subsystem.

提示

要避免手动连接子系统,请在要添加 Subsystem 模块的模型层次结构层级中构建子系统的内容。然后,将内容转换为子系统:

  1. 通过在画布上点击并拖动指针绘制包含这些组件的矩形来选择子系统组件。

  2. 将鼠标暂停在出现的省略号上。

  3. 在展开的操作栏中,点击创建子系统

转换为子系统会自动添加并连接子系统的输入端口和输出端口。

在不使用信号线的情况下连接模块

相关模块无需信号线即可相互连接。

例如,GotoFrom 模块是相关模块,您无需信号线即可使用它们发送信号。进入 Goto 模块的信号无需通过信号线即可退出 From 模块。

A Constant block connects to a Goto block with a signal line, and a From block connects to a Display block with a signal line. The Constant block has a value of 5.25, and the Display block shows a value of 5.25. The Goto block is selected, and the From block is highlighted in purple.

要连接相关模块,请使用“模块参数”对话框或属性检查器的参数选项卡。

提示

要检查两个或更多相关模块是否已连接,请选择其中一个模块。所有与选定模块连接的相关模块会以紫色突出显示。

某些相关模块使用标记进行连接。例如,所有具有相同标记的 Goto From 模块都会连接。

Two rows of blocks are one above another. The top row contains a Constant block connected to a Goto block with a signal line, and a From block connected to a Display block with a signal line. The bottom row contains the same blocks. The value of the Constant block in the top row is 5.25. The tag on the Goto block icon in the top row is A, and the tag on the From block icon in the bottom row is A. The display block in the bottom row shows a value of 5.25. The value of the Constant block in the bottom row is-1. The tag on the Goto block icon in the bottom row is B, and the tag on the From block icon in the top row is B. The display block in the top row shows a value of -1.

您可以在“模块参数”对话框或属性检查器的参数选项卡中为这些模块设置标记。

In the Goto block Block Parameters dialog box, the "Goto tag" parameter text box is highlighted and displays the value A.

Dashboard 和 Customizable Blocks 库中的模块也是相关模块。您可以将这些库中的显示模块连接到信号。您可以将控制模块连接到模块参数或变量。有关如何连接 Dashboard 模块的信息,请参阅Connect Dashboard Blocks to Simulink Model

将信号线转换为 GotoFrom 模块,反之亦然

您可以通过用 GotoFrom 模块替换冗长的信号线来简化模型图。但是,当您要跟踪信号路径时,查看信号线会很有用。

您可以将信号线和虚拟总线转换为 GotoFrom 模块集,并且可以将 GotoFrom 模块集转换为信号线。GotoFrom 模块组是一组连接的 GotoFrom 模块,这意味着这些模块具有相同的标记。

要转换的模型元素必须满足以下条件:

  • 信号线、总线或模块已完全连接。

  • 信号线、总线或模块都位于模型层次结构中的同一位置,即:要么都在层次结构的顶层,要么都在同一组件中。

  • 信号线或总线未连接到任何 GotoFrom 模块。

在转换过程中,软件会忽略不符合这些条件的模型元素。无论模块标记是否可见,您都可以执行转换。

要将 Goto 模块及其连接的所有 From 模块转换为信号线,请选择 Goto 模块。要将 From 模块及其连接的 Goto 模块转换为信号线,请选择 From 模块。将指针悬停在选定模块上方出现的省略号上。在展开的操作菜单中,选择转换为信号。或者,选择 GotoFrom 模块,然后在 Simulink 工具条的 GotoFrom 选项卡上,点击转换为信号

A Sine Wave block is connected to a Goto block, and a From block is connected to a Scope block. The pointer selects the Goto block, and uses the action menu to convert the Goto and From blocks to a signal line that connects the Sine Wave block to the Scope block.

要将信号线或总线转换为 GotoFrom 模块组,请选择该信号线或总线。将鼠标暂停在出现的省略号上。在展开的操作菜单中,选择转换为 Goto 和 From 模块。或者,选择信号线或总线,然后在工具条的信号选项卡上,点击转换为 Goto/From

要同时将多个 Goto 模块及其所有连接的 From 模块转换为信号线,请按住 Shift 键并点击各模块,或通过拖动在这些模块周围形成一个选择框来选择所有 Goto 模块。在 Simulink 工具条的 Goto 选项卡上,点击转换为信号

如果一个 Goto 模块具有多个具有相同标记的 From 模块,要将其中部分 From 模块转换为信号线,请选择您要转换的 From 模块。在工具条的 From 选项卡上,点击转换为信号。选定 From 模块将转换为连接到对应 Goto 模块输出信号线的信号线。Goto 模块不会转换为信号线。

要同时将多条信号线或总线转换为 GotoFrom 模块组,请通过按住 Shift 键并点击或通过拖动以形成一个选择框来选择这些信号线或总线。在工具条的信号选项卡上,点击转换为 Goto/From

The pointer selects a signal line and uses the action menu to convert the signal line into a Goto and From block pair. The pointer then selects two signal lines and uses the toolstrip to convert the signal lines to a Goto and From block set.

注释掉和注释直通模块

您可以通过注释掉或注释直通模块来将模块排除在仿真之外,而无需从模型中物理删除这些模块。

  • 注释掉:将选定模块排除在仿真之外。信号终止并接地。

  • 注释直通:将选定模块排除在仿真之外。信号通过。要注释直通一个模块,该模块必须具有相同数量的输入端口和输出端口,并且没有控制端口或连接端口。

注意

以下 Simulink 模块不支持注释掉注释直通命令:

  • Inport

  • Outport

  • Connection Port

  • Argument Inport

  • Argument Outport

  • Data Store Memory

  • Goto Tag Visibility

Signal Generator 模块不支持注释直通命令。

要注释掉某模块,请选择该模块。在出现的操作栏中,点击注释掉

Selected Constant block with the action bar above it, and the Comment Out button selected with the tooltip "Comment Out" displayed above it

要注释直通某个模块,请右键点击该模块并选择注释直通

提示

或者,选择该模块并按:

  • Ctrl+Shift+X 将其注释掉

  • Ctrl+Shift+Y 将其注释直通

另请参阅

主题