quarc_basic_server_mixed_types_demo.mdl
quarc_basic_client_mixed_types_demo.mdl
Open these models

QUARC Basic Communications Mixed Types Demo

This example consists of two Simulink models; a server and a client. It demonstrates how to use the Basic Communications blockset from the QUARC Targets Library to establish a connection between two Simulink models and to pass mixed data types between the client and server. This example makes use of Simulink Bus objects. For a detailed description of these models and how they operate, please refer to the Basic Communications section of the QUARC documentation.

Basic Server Mixed Types Demo Simulink Diagram

Basic Client Mixed Types Demo Simulink Diagram

Configuring the Demonstration

To set up the demonstration, double-click on the Stream Server block in the quarc_basic_server_mixed_types_demo.mdl model and on the Stream Client block in the quarc_basic_client_mixed_types_demo.mdl model and make sure the URI upon which to listen parameter of Stream Server block is the same as the URI of host to which to connect parameter of the Stream Client block.

Demonstration

Building, deploying and starting the models

Refer to One Step Process in QUARC Basic Procedures to build, deploy and start each model.

Note that the order in which you start the models is not important. However, in practical situations, the server side should always be running before the client starts.

Warning In the case of using UDP as the communication protocol, it's necessary to start the server prior to the client. The reason is UDP does not establish a connection between client and server, and server relies on the IP address of its received packages to communicate. When the client is started, it sends a data package to the server, and because of its blocking design, it waits for a reply from the server which has not been started yet. In the other hand, when the server is started, it waits for a package from the client to know the IP address of the client. Therefore, both parties wait for each other, and no data is communicated.

This problem can be solved when the client is running on Windows. The solution is to add unreachable=yes in front of the client URI. In this case, when server is unreachable an error signal is created internally, and the client tries to send the starting data package again. Therefore, when the server is started, it eventually receives a data package from the client making it leave its listening state, and start the communication.

Running the model

Double-click on the "Received Data - Server" Scope blocks in the quarc_basic_server_mixed_types_demo.mdl model to open the Scope. Do the same for the "Received Data - Client" Scope blocks in the quarc_basic_client_mixed_types_demo.mdl model to open those Scopes as well.

Sine waves of amplitude 64, 96 and 127 will appear on the "Received Data - Server1" Scope. Their data type is int8. Meanwhile, the "Received Data - Server2" Scope traces a repeating sequence of amplitude 65530. Its data type is uint16.

Scope with Sine Waves of Amplitude 64, 96 and 127

Scope with Repeating Sequence of Amplitude 65530

On the client end, a sawtooth wave of amplitude 4 will appear on the "Received Data - Client1" Scope, a pulse train of amplitude 1 on the "Received Data - Client2" Scope, and three sine waves of amplitude 1, 2 and 3 respectively on the "Received Data - Client3" Scope. The data types are double, boolean and single respectively.

Scope with Sawtooth of Amplitude 4

Scope with Pulse Train of Amplitude 1

Scope with Sine Waves of Amplitude 1, 2 and 3

Stopping the models

Refer to One Step Process in QUARC Basic Procedures to stop each model.

Miscellaneous

To see the BusObject used to define the bus carrying the mixed data types, open the Simulink Bus Editor by selecting Bus Editor button inside the Design gallery on the MODELING tab. The dialog shown below will appear.

BusObject in the Simulink Bus Editor

The BusObject in the Bus Editor is a variable in the MATLAB workspace. It is an instance of the Simulink.Bus class and was created using the Bus Editor. Refer to the Simulink help for details on using the Bus Editor.

To make sure that this BusObject variable is always available when the Simulink diagram needs it, even when the clear command has been executed, the BusObject was saved in a MAT file using the save command. It is stored in the quarc_basic_mixed_types_demo.mat file in the same folder as the Simulink models. This MAT-file is then loaded every time the diagram is updated or code is built by putting the load command in the model's InitFcn callback.

View the model's InitFcn callback by selecting Model Properties button inside the Setup gallery on the MODELING tab. Then select the Callbacks tab and click on the InitFcn callback. The Model Properties dialog is depicted below.

Model Properties

The expression which(bdroot) returns the full path to the Simulink model itself. The fileparts command is then used to get the folder in which the Simulink model resides from that path. Thus, the expression:

    fullfile(fileparts(which(bdroot)), 'quarc_basic_mixed_types_demo.mat')

is the full path to the MAT-file containing the BusObject variable. The load command is issued for this path to load this variable into the MATLAB workspace. Since the InitFcn callback is executed each time the Simulink diagram is updated, the BusObject variable will be loaded into the MATLAB workspace every time the model is built or started (even in normal simulation). The same command is placed in the PreLoadFcn callback so that the bus object is defined as soon as the model is loaded.

Of course, that means if you change the BusObject you need to save it to this MAT file before using the Simulink diagram again. Otherwise the contents of the MAT file will be loaded and override your changes!

Running the example on a different target

To run the example on a different target, refer to the instructions on the Running QUARC Examples on Remote Targets page.

If the quarc_basic_server_mixed_types_demo.mdl and the quarc_basic_client_mixed_types_demo.mdl model are being run on different targets, then the URI of the Stream Client block in the client model should use the host name or IP address of the machine running the server for the hostname of its URI. For example, if the server is running on a machine with IP address 192.168.0.16 and the URI of the Stream Server block in the server model is tcpip://localhost:18000, then the URI of the Stream Client block in the client model should be tcpip://192.168.0.16:18000.