This example shows how to build a simple model using the Instrument Control Toolbox™ blocks in conjunction with other blocks in the Simulink® library. The example illustrates how to send data to a simple loopback device connected to the computer's serial port and how to read that data back into your model.
After you connect your loopback device to your computer, determine which serial port it is on. For instructions, see Find Serial Port Information for Your Platform.
You use the To Instrument block to write a value to the serial port on your computer, and then use the Query Instrument block to read that same value back into your model.
Block names are not shown by default in the model. To display the hidden block names while working in the model, select Display and clear the Hide Automatic Names check box.
To start Simulink and create a new model, enter the following in the MATLAB® Command Window.
On the Simulink start page, click Blank Model and then Create Model. An empty Editor window opens.
In the toolstrip, click Save to assign a name to your new model.
In the toolstrip, click the Library Browser button in the Simulation tab.
The Simulink Library Browser opens. The left pane contains a tree of available block libraries in alphabetical order. Click Instrument Control Toolbox.
To use a block, add it to an existing model or create a new model.
To use a block in a model, drag the block from the library into the Simulink Editor. For this example, you need one instance of the To Instrument and the Query Instrument blocks in your model.
The To Instrument block can be used with these interfaces: VISA, GPIB, Serial, TCP/IP, and UDP. It is not supported on these interfaces: SPI, I2C, and Bluetooth.
This example requires two more blocks. One block provides the data that is sent to the instrument; the other block displays the data received from the instrument.
Because the data sent to the instrument is constant, you can use the Constant block for this purpose. Access the block by expanding the Simulink node in the browser tree and clicking the Sources library entry. From the blocks in the right pane, drag the Constant block into the Simulink Editor and place it to the left of the To Instrument block.
To display the data received from the instrument, you can use the Display block. To access the Display block, click the Sinks library entry in the expanded Simulink node in the browser tree. From the blocks displayed in the right pane, drag the Display block into the Simulink Editor and place it to the right of the Query Instrument block.
Make a connection between the Constant block and the To Instrument block. A quick way to make the connection is to select the Constant block, press and hold the Ctrl key, and then click the To Instrument block.
In the same way, make the connection between the output port of the Query Instrument block and the input port of the Display block.
The two blocks do not directly connect within the model. The only communication between them is through the instrument, which is the loopback device connected to the serial port. Because the two blocks have no direct connection, you must consider their timing when running the model. The Query Instrument block does not get its input from the To Instrument block, so it has no way to know when the data from the instrument is available. Therefore, you must set the block parameters to write the data to the loopback before the model attempts to receive data from the loopback.
Set parameters for the blocks in your model by double-clicking the block.
Double-click the To Instrument block to open its parameters dialog box. Set Port to the port that your loopback device is connected to. For instructions, see Find Serial Port Information for Your Platform.
Click OK to close the dialog box.
Double-click the Query Instrument block to open its parameters dialog box. Make sure that the values on the Hardware Configuration tab match the Hardware values on the To Instrument block.
The model uses the default values on the Instrument Initialization and Query tabs of this block, so you do not need to modify any of their values.
Click OK to apply any changes and close the dialog box.
Double-click the Constant block to open its parameters dialog box. Change the Constant value to the value you want to send to the instrument. For this example:
Constant value to
Sample time to
For the Display block, you can use its default parameters.
The block with the lowest number gets the highest priority. In the Simulink Editor, right-click a block and select
Properties. Enter the priority number in the
Priority field in the Block Properties dialog box. To
ensure that the To Instrument block first completes writing data to
the loopback before the Query Instrument block reads it, set the
priority of the To Instrument block to
1 and the
Query Instrument block to
You must set the correct priority for the blocks in your model. Otherwise you might see unexpected results.
To run the simulation, click the green Run button in the Simulink Editor toolstrip. You can use toolstrip options to specify how long to run the simulation and to stop a running simulation.
When you run the simulation, the constant value you specify (25) is written to the instrument (the serial loopback), received from the instrument, and shown in the Display block.
While the simulation is running, the status bar at the bottom of the Simulink Editor updates the progress of the simulation.