主要内容

Writing a Generic Driver

创建驱动程序并定义其初始化行为

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®系统。)通过示例工作,您编写了一个简单的matlab®仪器通用驱动程序允许仪器控制工具箱™软件与COM对象通信。使用图形界面和命令行代码,使用您的驱动程序创建IE浏览器窗口,控制其大小,并指定所显示的网页。在为任何类型的仪器编写通用驱动程序时,可以应用在该示例中所示的原理。

在本节中,您可以创建一个新驱动程序,并指定为此驱动程序创建对象时会发生什么。

  1. 从MATLAB命令窗口打开MATLAB仪器驱动程序编辑器。

    midedit
  2. To make it known that this driver is a generic driver, in the MATLAB Instrument Driver Editor, select文件>新的>通用驾驶员, as shown.

  3. Select文件>另存为.

    导航到要保存驱动程序的目录,并给出您想要的任何名称。此示例使用名称IE_drv. Remember where you have saved your driver.

  4. 选择概括node in the driver editor window. Set the fields of this pane with any values you want. This example uses the following settings:

    制造商

    微软

    金宝app支持的模型

    IE

    Instrument type

    浏览器

    Driver version

    1.0

  5. 选择节点Initialization and Cleanup.

  6. 点击创造tab.

    这是在使用此驱动程序创建设备对象时定义要执行的代码的位置。此示例标识Internet Explorer的COM对象,并将句柄分配给该对象Interface正在创建的设备对象的属性。

  7. Add the following lines of code to the创造标签:

    IE = ACTXSERVER('InternetExplorer.Application');obj.interface = IE);
  8. 点击连接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连接标签:

    IE = get(obj,'interface');IE.visible = 1);IE.FULLSCREEN = 0);

The first line getsIE作为COM对象的句柄,基于分配创造代码。The two lines after that set the window visibility and size.

Defining Properties

MATLAB仪器驱动程序编辑器中的通用驱动程序的写作属性是写入直接代码的问题。

在此示例中,您可以定义两个属性。第一个属性使用与COM对象的相应属性相同的名称;第二属性使用相应的COM对象属性使用不同的名称。

Using the Same Name for a Property

The position of the IE browser window is determined by the最佳properties of its COM object. In the following steps, you make the最佳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. 选择Properties驱动程序编辑器树中的节点。

  2. 在里面添加属性field, enter the text最佳, and clickAdd.

  3. Expand thePropertiesnode in the tree, and select the new node最佳.

  4. 点击财产价值tab. Your property can have a numeric value corresponding to screen pixels. For this example, you can limit the value of the property from0to200..

  5. 确保这一点数据类型字段表示双倍的. In theConstraint字段,单击下拉菜单并选择Bounded.

  6. Keep the最低限度value of0.0,并输入一个最大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得到或者set.

  7. 点击Codetab.

    阅读财产的概念相当简单。当你得到的时候最佳设备对象的属性,驱动程序仅获取对应的COM对象的值最佳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 theGet code窗格:

    IE = obj.interface;propertyvalue = get(即propertyName);

    The first line getsIEas a handle to the COM object. Remember that theInterfaceproperty of the device object is set to this value back in the driver's创造代码。第二行检索COM对象的值最佳property, and assigns it topropertyValue, which is returned to the得到function for the device object.

  9. Add the following code at the bottom of the function in the设置代码窗格:

    IE = get(obj,'interface');IE.propertyName = propertyValue;

为一个属性使用不同的名称

在里面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 calledvsize.that corresponds to the IE COM object property高度.

  1. 选择Properties驱动程序编辑器树中的节点。

  2. 在里面添加属性field, enter the textvsize., and clickAdd.

  3. Expand thePropertiesnode in the tree, and select the new nodevsize..

  4. 点击财产价值tab. This property can have a numeric value corresponding to screen pixels, whose range you define as200.to800.

  5. 确保这一点数据类型字段表示双倍的. In theConstraint字段,单击下拉菜单并选择Bounded.

  6. Enter a最低限度value of200.,并输入一个最大value of800.

  7. 点击Codetab.

  8. Add the following code at the bottom of the function in theGet code窗格:

    IE = obj.interface;propertyValue = ie.Height;
  9. Add the following code at the bottom of the function in the设置代码窗格:

    IE = get(obj,'interface');set(ie, 'Height', propertyValue);
  10. 保存你的司机。

定义功能

Internet Explorer的常用功能是下载网页。在以下步骤中,您可以创建一个调用的函数that allows you to navigate the Web with the browser.

  1. 选择Functions驱动程序编辑器树中的节点。

  2. 在里面Add functionfield, enter the text, and clickAdd.

  3. Expand theFunctionsnode in the tree, and select the new node.

    MATLAB仪器驱动程序编辑器中的通用驱动程序的函数是编写直接代码的问题。

    Yourfunction requires only one input argument: the URL of the Web page to navigate to. You can call that argument地点.

  4. 更改MATLAB代码窗格的第一行以阅读

    函数goto(obj,网站)

    变量objis the device object using this driver. The value of地点是使用此驱动程序时传递到此函数的字符矢量。你的功能然后必须通过价值地点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 of地点.

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

    IE = obj.interface;调用(即'Navigate2',网站);
  6. Save your driver, and close the MATLAB Instrument Driver Editor.

    Now that your generic driver is ready, you can use it with theTest and Measurement Tool或在Matlab命令行。