Writing a Generic Driver
Note
The MATLAB® instrument driver functions makemid
,
midedit
, and midtest
will be removed
in a future release. Use the ividev
interface from the Instrument Control Toolbox™ Support Package for
IVI® and VXIplug&play Drivers instead. For more information, see IVI and VXIplug&play Drivers.
Creating the Driver and Defining Its Initialization Behavior
In this example, the generic “instrument” that you control is Microsoft® Internet Explorer® (IE), which is represented by a COM object. (This example works only on Windows® systems.) Working through the example, you write a simple MATLAB instrument generic driver that allows the Instrument Control Toolbox software to communicate with a COM object. Using both a graphical interface and command-line code, with your driver you create an IE browser window, control its size, and specify what Web page it displays. The principles demonstrated in this example can be applied when writing a generic driver for any kind of instrument.
In this section, you create a new driver and specify what happens when an object is created for this driver.
Open the MATLAB Instrument Driver Editor from the MATLAB Command Window.
midedit
To make it known that this driver is a generic driver, in the MATLAB Instrument Driver Editor, select File > New > Generic driver, as shown.
Select File > Save as.
Navigate to the directory where you want to save your driver, and give it any name you want. This example uses the name
ie_drv
. Remember where you have saved your driver.Select the
Summary
node in the driver editor window. Set the fields of this pane with any values you want. This example uses the following settings:Manufacturer
Microsoft
Supported models
IE
Instrument type
Browser
Driver version
1.0
Select the node
Initialization and Cleanup
.Click the Create tab.
This is where you define the code to execute when this driver is used to create a device object. This example identifies the COM object for Internet Explorer, and assigns the handle to that object as the
Interface
property of the device object being created.Add the following lines of code to the Create tab:
ie = actxserver('internetexplorer.application'); obj.Interface = ie);
Click the Connect tab.
This is where you define the code to execute when you connect your device object to your instrument or software.
Add the following lines of code to the Connect tab:
ie = get(obj, 'Interface'); ie.Visible = 1); ie.FullScreen = 0);
The first line gets ie
as a handle to the
COM object, based on the assignment in the Create code.
The two lines after that set the window visibility and size.
Defining Properties
Writing properties for generic drivers in the MATLAB Instrument Driver Editor is a matter of writing straight code.
In this example, you define two properties. The first property uses the same name as the corresponding property of the COM object; the second property uses a different name from its corresponding COM object property.
Using the Same Name for a Property
The position of the IE browser window is determined by the Top
and Left
properties
of its COM object. In the following steps, you make the Top
property
available to your device object through your generic driver. For this
property, the name of the property is the same in both the COM object
and in your device object.
Select the
Properties
node in the driver editor tree.In the Add property field, enter the text
Top
, and click Add.Expand the
Properties
node in the tree, and select the new nodeTop
.Click the Property Values tab. Your property can have a numeric value corresponding to screen pixels. For this example, you can limit the value of the property from
0
to200
.Make sure the Data Type field indicates
Double
. In the Constraint field, click the pull-down menu and selectBounded
.Keep the Minimum value of
0.0
, and enter a Maximum value of200
.Your driver editor window should look like the following figure.
Now that you have defined the data type and acceptable values of the property, you can write the code to be executed whenever the device object property is accessed by
get
orset
.Click the Code tab.
The concept of reading the property is rather straightforward. When you get the
Top
property of the device object, the driver merely gets the value of the COM object's correspondingTop
property. So all you need in the Get code function is to identify the COM object to get the information from.Add the following code at the bottom of the function in the Get code pane:
ie = obj.Interface; propertyValue = get(ie, propertyName);
The first line gets
ie
as a handle to the COM object. Remember that theInterface
property of the device object is set to this value back in the driver's Create code. The second line retrieves the value of the COM object'sTop
property, and assigns it topropertyValue
, which is returned to theget
function for the device object.Add the following code at the bottom of the function in the Set code pane:
ie = get(obj, 'Interface'); ie.propertyName = propertyValue;
Using a Different Name for a Property
In the preceding steps, you created in your driver a device
object property that has the same name as the property of the COM
object representing your instrument. You can also create properties
with names that do not match those of the COM object properties. In
the following steps, you create a property called Vsize
that
corresponds to the IE COM object property Height
.
Select the
Properties
node in the driver editor tree.In the Add property field, enter the text
Vsize
, and click Add.Expand the
Properties
node in the tree, and select the new nodeVsize
.Click the Property Values tab. This property can have a numeric value corresponding to screen pixels, whose range you define as
200
to800
.Make sure the Data Type field indicates
Double
. In the Constraint field, click the pull-down menu and selectBounded
.Enter a Minimum value of
200
, and enter a Maximum value of800
.Click the Code tab.
Add the following code at the bottom of the function in the Get code pane:
ie = obj.Interface; propertyValue = ie.Height;
Add the following code at the bottom of the function in the Set code pane:
ie = get(obj, 'Interface'); set(ie, 'Height', propertyValue);
Save your driver.
Defining Functions
A common function for Internet Explorer is to download a
Web page. In the following steps, you create a function called goTo
that
allows you to navigate the Web with the browser.
Select the
Functions
node in the driver editor tree.In the Add function field, enter the text
goTo
, and click Add.Expand the
Functions
node in the tree, and select the new nodegoTo
.Writing functions for generic drivers in the MATLAB Instrument Driver Editor is a matter of writing straight code.
Your
goTo
function requires only one input argument: the URL of the Web page to navigate to. You can call that argumentsite
.Change the first line of the MATLAB code pane to read
function goTo(obj, site)
The variable
obj
is the device object using this driver. The value ofsite
is a character vector passed into this function when you are using this driver. Your function then must pass the value ofsite
on to the IE COM object. So your function must get a handle to the COM object, then call the IE COM methodNavigate2
, passing to it the value ofsite
.Add the following code at the bottom of the function in the MATLAB code pane:
ie = obj.Interface; invoke(ie, 'Navigate2', site);
Save your driver, and close the MATLAB Instrument Driver Editor.