Plug and Play Support (POS for .NET v1.12 SDK Documentation)

2/27/2008

POS for .NET fully supports Windows Embedded Plug and Play technology. To utilize this feature, Service Object developers can simply add one or more HardwareId attributes to their code, or include the hardware reference in a Plug and Play XML Configuration file.

Adding this attribute to a Service Object helps application developers, who will now know that when they use PosExplorer to get a list of Service Objects, any Service Object in that list will be associated with a functioning POS device. The application benefits directly from this association by greater reliability and ease of use. We recommend that Service Objects support the Plug and Play feature whenever possible.

Plug and Play Behavior

Once the Service Object has been associated with the POS device's hardware ID, POS for .NET uses the Windows Plug and Play Manager to determine what POS devices are connected to the computer. No additional code is required by the application or Service Object.

When an application invokes the method PosExplorer.GetDevices, PosExplorer finds the device that is associated with each Plug and Play Service Object and then queries the Windows Plug and Play Manager to determine the device's status. If the device is not available, it will not be added to the device list that is returned to the application from PosExplorer.GetDevices.

PosExplorer Service Object Filtering

PosExplorer is able to effectively filter the list of Plug and Play Service Objects when the application calls PosExplorer.GetDevices. The filtering process works as follows:

  1. Searches for all assemblies in the specified POS for .NET directories.
  2. If the assembly is not marked with the PosAssembly global attribute, discards it.
  3. Searches for classes marked with the ServiceObject attribute. For each such class:
    1. Looks for a hardware ID associated with this class, as either a HardwareId attribute or within the Plug and Play XML Configuration file. If there is no hardware ID, leaves the Service Object in the PosExplorer list.
    2. If there is a hardware ID, then queries Windows to retrieve the device's status. If the device is attached to the computer, leaves it in the PosExplorer list.
    3. If the device is not attached to the computer, removes it from the PosExplorer list.

Example

The following code example demonstrates a simple method of handling Plug and Play events. Information generated by PosExplorer is used to instantiate the correct device, in this case a Magnetic Stripe Reader (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();
      }
   }
}

See Also

Tasks

Adding Plug and Play Support

Reference

PosExplorer
HardwareIdAttribute
PosAssemblyAttribute
ServiceObjectAttribute

Concepts

Attributes for Identifying Service Objects and Assigning Hardware
POS for .NET Registry Settings
Plug and Play XML Configuration

Other Resources

POS for .NET Service Object Architecture