|On this page…|
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.
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.
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.
ie = actxserver('internetexplorer.application'); set(obj, 'Interface', ie);
This is where you define the code to execute when you connect your device object to your instrument or software.
ie = get(obj, 'Interface'); set(ie, 'Visible', 1); set(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.
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.
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.
Your driver editor window should look like the following figure.
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 corresponding Top property. So all you need in the Get code function is to identify the COM object to get the information from.
ie = get(obj, 'Interface'); propertyValue = get(ie, propertyName);
The first line gets ie as a handle to the COM object. Remember that the Interface 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's Top property, and assigns it to propertyValue, which is returned to the get function for the device object.
ie = get(obj, 'Interface'); set(ie, propertyName, propertyValue);
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.
ie = get(obj,'Interface'); propertyValue = get(ie,'Height');
ie = get(obj, 'Interface'); set(ie, 'Height', propertyValue);
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.
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 argument site.
function goTo(obj, site)
The variable obj is the device object using this driver. The value of site is a string passed into this function when you are using this driver. Your function then must pass the value of site on to the IE COM object. So your function must get a handle to the COM object, then call the IE COM method Navigate2, passing to it the value of site.
ie = get(obj, 'Interface'); invoke(ie, 'Navigate2', site);
Now that your generic driver is ready, you can use it with the Test & Measurement Tool (tmtool) or at the MATLAB command line.