服务点设备声明和启用型号

使用服务点设备声明,启用 API 来声明设备,并启用它们来执行 I/O 操作。

声明独占使用

创建 PointOfService 设备对象后,必须使用设备类型的相应声明方法声明它,然后才能使用设备进行输入或输出。 声明授予应用程序对很多设备功能的独占访问权限,以确保一个应用程序不会干扰其他应用程序使用设备。 一次只能有一个应用程序声明 PointOfService 设备供独占使用。

注意

声明操作为设备建立排他锁,但不将其置于操作状态。 有关详细信息,请参阅启用设备进行 I/O 操作

用于声明/发布的 API

设备 声明 发布
BarcodeScanner BarcodeScanner.ClaimScannerAsync ClaimedBarcodeScanner.Close
CashDrawer CashDrawer.ClaimDrawerAsync ClaimedCashDrawer.Close
LineDisplay LineDisplay.ClaimAsync ClaimedineDisplay.Close
MagneticStripeReader MagneticStripeReader.ClaimReaderAsync ClaimedMagneticStripeReader.Close
PosPrinter PosPrinter.ClaimPrinterAsync ClaimedPosPrinter.Close

启用设备进行 I/O 操作

声明操作可建立对设备的独占权限,但不会将其置于操作状态。 若要接收事件或执行任何输出操作,必须使用 EnableAsync 来启用设备。 相反,可以调用 DisableAsync 来停止侦听来自设备的事件或停止执行输出。 还可使用 IsEnabled 来确定设备的状态。

使用了 enable/disable 的 API

设备 启用 禁用 IsEnabled?
ClaimedBarcodeScanner EnableAsync DisableAsync IsEnabled
ClaimedCashDrawer EnableAsync DisableAsync IsEnabled
ClaimedLineDisplay 不适用¹ 不适用¹ 不适用¹
ClaimedMagneticStripeReader EnableAsync DisableAsync IsEnabled
ClaimedPosPrinter EnableAsync DisableAsync IsEnabled

¹ 行显示不要求显式启用设备进行 I/O 操作。 启用操作由执行 I/O 的 PointOfService LineDisplay API 自动执行。

代码示例:声明和启用

此示例显示如何在成功创建了条形码扫描仪对象后声明条形码扫描仪设备。


    BarcodeScanner barcodeScanner = await BarcodeScanner.FromIdAsync(DeviceId);

    if(barcodeScanner != null)
    {
        // after successful creation, claim the scanner for exclusive use 
        claimedBarcodeScanner = await barcodeScanner.ClaimScannerAsync();

        if(claimedBarcodeScanner != null)
        {
            // after successful claim, enable scanner for data events to fire
            await claimedBarcodeScanner.EnableAsync();
        }
        else
        {
            Debug.WriteLine("Failure to claim barcodeScanner");
        }
    }
    else
    {
        Debug.WriteLine("Failure to create barcodeScanner object");
    }
    

警告

声明可能在以下情况下丢失:

  1. 另一个应用已请求了对同一设备的声明,并且你的应用没有为响应 ReleaseDeviceRequested 事件发出 RetainDevice。 (有关详细信息,请参阅下面的声明协商。)
  2. 你的应用已暂停,这导致设备对象关闭,声明因此不再有效。 (有关详细信息,请参阅设备对象生命周期。)

声明协商

由于 Windows 是一个多任务环境,因此同一计算机上的多个应用程序可能需要以协作方式访问外围设备。 PointOfService API 提供了允许多个应用程序共享连接到计算机的外设的协商模型。

当同一台计算机上的第二个应用程序请求声明已由其他应用程序声明的 PointOfService 外设时,将发布 ReleaseDeviceRequested 事件通知。 如果应用程序当前正在使用该设备来避免丢失声明,具有有效声明的应用程序必须通过调用 RetainDevice 响应事件通知。

如果具有有效声明的应用程序未立即使用 RetainDevice 响应,将假设该应用程序已暂停或不需要设备,声明将被撤销并给予新应用程序。

第一步是创建事件处理程序,该事件处理程序使用 RetainDevice 响应 ReleaseDeviceRequested 事件。

    /// <summary>
    /// Event handler for the ReleaseDeviceRequested event which occurs when 
    /// the claimed barcode scanner receives a Claim request from another application
    /// </summary>
    void claimedBarcodeScanner_ReleaseDeviceRequested(object sender, ClaimedBarcodeScanner myScanner)
    {
        // Retain exclusive access to the device
        myScanner.RetainDevice();
    }

接下来,注册与声明设备关联的事件处理程序

    BarcodeScanner barcodeScanner = await BarcodeScanner.FromIdAsync(DeviceId);

    if(barcodeScanner != null)
    {
        // after successful creation, claim the scanner for exclusive use 
        claimedBarcodeScanner = await barcodeScanner.ClaimScannerAsync();

        if(claimedBarcodeScanner != null)
        {
            // register a release request handler to prevent loss of scanner during active use
            claimedBarcodeScanner.ReleaseDeviceRequested += claimedBarcodeScanner_ReleaseDeviceRequested;

            // after successful claim, enable scanner for data events to fire
            await claimedBarcodeScanner.EnableAsync();          
        }
        else
        {
            Debug.WriteLine("Failure to claim barcodeScanner");
        }
    }
    else
    {
        Debug.WriteLine("Failure to create barcodeScanner object");
    }

用于声明协商的 API

声明的设备 发布通知 保留设备
ClaimedBarcodeScanner ReleaseDeviceRequested RetainDevice
ClaimedCashDrawer ReleaseDeviceRequested RetainDevice
ClaimedLineDisplay ReleaseDeviceRequested RetainDevice
ClaimedMagneticStripeReader ReleaseDeviceRequested RetainDevice
ClaimedPosPrinter ReleaseDeviceRequested RetainDevice