| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → xPC Target |
| Contents | Index |
| Learn more about xPC Target |
| On this page… |
|---|
This section uses an example of a multimeter attached to a GPIB bus with an address of 16. This multimeter needs the initialization command
:conf:volt:dc
to set the device to read DC voltages, and needs the command
:read?
during each sample interval to read one voltage value.
The GPIB driver blocks initialize and communicate directly with the GPIB controller. The GPIB controller then communicates with the GPIB devices on the instrument bus.
After you create a Simulink model, you can add GPIB driver blocks and define the initialization, send/receive, and termination message structures.
In the MATLAB Command Window, type
xpclib
The xPC Target driver block library opens.
Double-click the GPIB group block.
A manufacturers window opens. Currently xPC Target only supports GPIB communication with a National Instruments controller.
Double-click the National Instruments group block.
A window with blocks for GPIB drivers opens.

Alternatively, you could access the xPC Target block library from the Simulink Library Browser. In the Simulink window, and from the View menu, click Show Library Browser. In the left pane, double-click xPC Target, double-click GPIB, and then click National Instruments.
Drag and drop a GPIB Setup block and a GPIB Send/Receive block to your Simulink model.
Your model should look similar to the figure below. Note that the input and output ports are not defined or visible on the blocks. The inputs and outputs are defined in a MATLAB message structure, and are visible only after you load that structure into the MATLAB workspace and update your Simulink model.

Double-click the GPIB Setup block. Enter values that correspond to the DIP switch settings you set on the GPIB-232CT-A controller. In the Initialization Struct box, enter the name for the MATLAB structure this block uses to send initialization messages to the GPIB device.
For example, if the target PC is connected to COM1, and you set the switches on the controller to 38400 baud, 8 data bits, and 1 stop bit, your Block Parameter dialog box should look similar to the figure shown below.

For more information on entering the block parameters, see National Instruments PC-DIO-24. For the procedure to create the initialization structure, see Creating GPIB Message Structures.
Double-click the GPIB Send/Receive block. The Block Parameters dialog box opens.
From the Port list, select either COM1 or COM2. This is the port on the target PC connected to the GPIB controller. In the Message Struct Name box, enter the name for the MATLAB structure this block uses to send and receive messages to the GPIB device. In the Sample Time box, enter the same sample time or multiple of the sample time you entered for the fixed step size in theSimulation > Configuration Parameters dialog box Solver pane.
Your Block Parameter dialog box should look similar to the figure shown below.

For more information on entering the block parameters, see National Instruments PC-DIO-24.
Your next task is to create the MATLAB message structures that the GPIB driver blocks use to sequence commands to the GPIB controller. See Creating GPIB Message Structures.
GPIB drivers use MATLAB structures to send and receive messages and to map the input and output ports on the GPIB driver blocks to the data written and read from the GPIB devices.
After you add GPIB driver blocks to your Simulink model, you can create the message structures to communicate with the GPIB controller. You need to create and load these structures into the MATLAB workspace before you build your target application. The easiest way to create these structures is to create an M-file and load that M-file into the MATLAB workspace.
In the MATLAB Command Window, and from the File menu, point to New, and then click M-file.
A MATLAB text editor window opens.
Enter the initialization and send/receive messages. Each message is an element in a MATLAB structure array with a series of fields. For information and examples of these fields, see GPIB Initialization and Termination Message Structures and GPIB Send/Receive Message Structure.
As an example, if you have a multimeter attached to a GPIB bus that has an address of 16, needs the initialization command :conf:volt:dc to set the device to read DC voltages, and uses the command :read? to read one voltage value, you could type the following:
GPIB_Initialize(1).Command = 'wrt 16';
GPIB_Initialize(1).SendData = ':conf:volt:dc';
GPIB_Send_Receive(1).Address= 16;
GPIB_Send_Receive(1).Command = 'wrt 16';
GPIB_Send_Receive(1).SendData = ':read?';
GPIB_Send_Receive(1).Timeout = 0.05;
GPIB_Send_Receive(2).Command = 'rd 16';
GPIB_Send_Receive(2).RecData = '%f';
GPIB_Send_Receive(2).RdLength = 20;
GPIB_Send_Receive(2).OutputPorts = [1];
GPIB_Send_Receive(2).OutputDataTypes = {'double'};
GPIB_Send_Receive(2).Timeout = 0.15;This example did not need a termination structure. But if it did, the format of the structure is the same as the initialization structure. For example, a termination structure could have a message with the .Command and .SendData fields.
GPIB_Termination(1).Command GPIB_Termination(1).SendData
From the File menu, click Save As. In the Save As File dialog box, enter the name of the M-file. For example, enter
GPIB_Messages.m
In the MATLAB Command Window, type the name of the M-file you created with the GPIB structures. For example, type
GPIB_Messages
The MATLAB interface loads and runs the M-file to create the message structures in the MATLAB workspace needed by the GPIB driver blocks.
Open your Simulink model, or press Ctrl+D.
The GPIB driver blocks are updated with the information from the structures. For example, inputs and outputs defined in the structures are now visible on the driver blocks.
Your model should look similar to the figure shown below.

Set the PreLoadFcn for your Simulink model to load the message structures when you open the model. For example, if you saved the message structures in the M-file GPIB_messages, type
set_param(gcs, 'PreLoadFcn','GPIB_messages.m')
Your next task is to build the target application and download it to the target PC.
![]() | GPIB Drivers | GPIB MATLAB Structure Reference | ![]() |

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |