即插即用支持(POS for .NET v1.14 SDK 文档)

POS for .NET 完全支持 Windows Embedded 即插即用技术。 若要利用此功能,服务对象开发人员只需向其代码添加一个或多个 HardwareId 属性,或者将硬件引用包含在即插即用 XML 配置文件中。

将此属性添加到服务对象可帮助应用程序开发人员,他们现在知道,当他们使用 PosExplorer 获取服务对象列表时,该列表中的任何服务对象都将与正常运行的 POS 设备相关联。 应用程序通过更高的可靠性和易用性直接从这种关联中受益。 我们建议服务对象尽可能支持即插即用功能。

即插即用行为

将服务对象与 POS 设备的硬件 ID 相关联后,POS for .NET 将使用 Windows 即插即用管理器来确定哪些 POS 设备连接到计算机。 应用程序或服务对象不需要其他代码。

当应用程序调用 PosExplorer.GetDevices 方法时,PosExplorer 会查找与每个即插即用服务对象关联的设备,然后查询 Windows 即插即用管理器以确定设备的状态。 如果设备不可用,则不会将其添加到从 PosExplorer.GetDevices 返回到应用程序的设备列表。

PosExplorer 服务对象筛选

当应用程序调用 PosExplorer.GetDevices 时,PosExplorer 能够有效地筛选即插即用服务对象的列表。 筛选过程如下所示:

  1. 搜索指定的 POS for .NET 目录中的所有程序集。
  2. 如果未使用 PosAssembly 全局属性标记程序集,则放弃该程序集。
  3. 搜索使用 ServiceObject 属性标记的类。 对于每个此类:
    1. 作为 HardwareId 属性或在即插即用 XML 配置文件中查找与此类关联的硬件 ID。 如果没有硬件 ID,请在 PosExplorer 列表中保留服务对象。
    2. 如果有硬件 ID,则查询 Windows 以检索设备的状态。 如果设备已连接到计算机,请将其保留在 PosExplorer 列表中。
    3. 如果设备未连接到计算机,请将其从 PosExplorer 列表中删除。

示例

下面的代码示例演示了一种处理即插即用事件的简单方法。 PosExplorer 生成的信息用于实例化正确的设备,在本例中为磁条读取器 (MSR)。

// Connect the Plug and Play events to detect the removal or
// connection of a new device.
   explorer.DeviceAddedEvent += new
         DeviceChangedEventHandler(explorer_DeviceAddedEvent);
   explorer.DeviceRemovedEvent += new
         DeviceChangedEventHandler(explorer_DeviceRemovedEvent);

// This event handler extends Plug and Play functionality to the MSR
// device type. A message is printed to the console if the connection
// is successful.
void explorer_DeviceAddedEvent(object sender, DeviceChangedEventArgs e)
{

   // Checks if the newly added device is an MSR.
   if (e.Device.Type == DeviceType.Msr)
   {

      // Checks if an MSR instance has already been created and,
      // if not,creates one. If a new MSR instance is created, its
      // name is recorded in a string and written to the console.
      // Once the printing is finished, the MSR is closed.
      if (msr == null)
      {
         CreateMsr(e.Device);
         strMsrConfig = e.Device.ServiceObjectName;
         Console.WriteLine(strMsrConfig);
         // It is important that applications close all open
         // Service Objects before terminating.
         msr.Close();
      }
   }
}

另请参阅

任务

参考

概念

其他资源