Main Content

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.

  1. Open the MATLAB Instrument Driver Editor from the MATLAB Command Window.

    midedit
  2. To make it known that this driver is a generic driver, in the MATLAB Instrument Driver Editor, select File > New > Generic driver, as shown.

  3. 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.

  4. 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

  5. Select the node Initialization and Cleanup.

  6. 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.

  7. Add the following lines of code to the Create tab:

    ie = actxserver('internetexplorer.application');
    obj.Interface = ie);
  8. Click the Connect tab.

    This is where you define the code to execute when you connect your device object to your instrument or software.

  9. 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.

  1. Select the Properties node in the driver editor tree.

  2. In the Add property field, enter the text Top, and click Add.

  3. Expand the Properties node in the tree, and select the new node Top.

  4. 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 to 200.

  5. Make sure the Data Type field indicates Double. In the Constraint field, click the pull-down menu and select Bounded.

  6. Keep the Minimum value of 0.0, and enter a Maximum value of 200.

    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 or set.

  7. 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 corresponding Top property. So all you need in the Get code function is to identify the COM object to get the information from.

  8. 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 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.

  9. 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.

  1. Select the Properties node in the driver editor tree.

  2. In the Add property field, enter the text Vsize, and click Add.

  3. Expand the Properties node in the tree, and select the new node Vsize.

  4. Click the Property Values tab. This property can have a numeric value corresponding to screen pixels, whose range you define as 200 to 800.

  5. Make sure the Data Type field indicates Double. In the Constraint field, click the pull-down menu and select Bounded.

  6. Enter a Minimum value of 200, and enter a Maximum value of 800.

  7. Click the Code tab.

  8. Add the following code at the bottom of the function in the Get code pane:

    ie = obj.Interface;
    propertyValue = ie.Height;
  9. Add the following code at the bottom of the function in the Set code pane:

    ie = get(obj, 'Interface');
    set(ie, 'Height', propertyValue);
  10. 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.

  1. Select the Functions node in the driver editor tree.

  2. In the Add function field, enter the text goTo, and click Add.

  3. Expand the Functions node in the tree, and select the new node goTo.

    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.

  4. 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 of site is a character vector 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.

  5. Add the following code at the bottom of the function in the MATLAB code pane:

    ie = obj.Interface;
    invoke(ie, 'Navigate2', site);
  6. Save your driver, and close the MATLAB Instrument Driver Editor.