If you are writing an application that needs to make use of point of service devices such as a bar code scanner or magnetic strip reader (MSR), the Microsoft Point of Service (POS) for .NETlibrary provides a common methods and properties for interfacing with 36 classes of POS supported peripherals. This library works with devices that meet either the Unified Point of Service (UPOS) or OLE for Point of Service (OPOS) standards. More information on the UPOS and OPOS standards can be found here.
Unfortunately, I have had difficulties finding any good documentation that shows how to get started using the Microsoft POS for .NET library. I ended up having to resort to the old trial-and-error method to get my proof of concept app working. I am going to share what I have learned about basics of getting started using the library. My example works with a Magnetic Swipe Reader device.
To start off, we will need to download and install the Microsoft POS for .NET v1.12 library if you do not already have it. Be sure to install both the SDK and runtime components. In our project, we will have to add a reference the Microsoft.PointOfService.dll which by default can be found in “C:\Program Files\Microsoft Point Of Service\SDK”.
In the class where we want to use the library, we will add this using statement:
?
|1
|
|using Micorsoft.PointOfService;
|
Preparing the Device for Use
First we have to prepare the device(s) so that they can be used in our application. Typically this would be done when a certain screen is loaded in your application like the screen for processing credit card payments. The library provides a PosExplorer object which will allow us to work with the devices connected to the computer. That is the starting point for setting up the device for use.
[?
|1
|
2
|PosExplorer myPosExplorer = new PosExplorer();
DeviceCollection myDevices = myPosExplorer.GetDevices(DeviceType.Msr);
|
T
his will create an instance of the PosExplorer and then get us a DeviceCollection of the all the Msr type devices that are connected to the computer. The DeviceCollection is a collection of DeviceInfo objects. To actually work with a device, we will have to create an instance of the device.
[?
|01
|
02
03
04
05
06
07
08
09
10
|foreach (DeviceInfo devInfo in myDevices)
if (devInfo.ServiceObjectName == "IDTECH_MMII_USBKB")
{
msrDevice = myPosExplorer.CreateInstance(devInfo) as Msr;
msrDevice.Open();
msrDevice.Claim(1000);
msrDevice.DataEvent += new DataEventHandler(msrDevice_DataEvent);
msrDevice.DataEventEnabled = true;
msrDevice.DeviceEnabled = true;
}
|
In my example, I know the name of the service object for my device so I am looking for that name so I setup the correct device. This is a very simplified example and in the real world you would not want to hard code the service object name in your code. The object msrDevice is a class level object because we will need to access it in other places in the class.
Once we create an instance of our device, we have to prepare it so it can actually be used to our application. This preparation has to be done with all types of devices. After the instance of msrDevice is created, we have to Open the device and Claim it for use this is done by the two lines that follow the creation of the instance.
After the device is open and claimed, we will register a handler for the DataEvent. The DataEvent gets raised whenever data is captured by the device. For an Msr this will occur when a card is swiped through the device.
Finally, we will set the DataEventEnabled and DeviceEnabled properties to true. Both of these properties default to false and if they are both not set to true the DataEvent will not be raised.
Handling the DataEvent
So now that we have the Msr device setup and ready to be used, we need to implement the code that will use the data once the DataEvent is fired. The handler for the data event could look something like this.
[?
|01
|
02
03
04
05
06
07
08
09
10
11
12
|private void msrDevice_DataEvent(object sender, DataEventArgs e)
{
txtAccountNo.Text = msrDevice.AccountNumber;
txtExpiration.Text = msrDevice.ExpirationDate;
txtFirstName.Text = msrDevice.FirstName.Trim();
txtMiddleName.Text = msrDevice.MiddleInitial.Trim();
txtLastName.Text = msrDevice.LastName.Trim();
txtTrack1Data.Text = ASCIIEncoding.ASCII.GetString(msrDevice.Track1Data);
txtTrack2Data.Text = ASCIIEncoding.ASCII.GetString(msrDevice.Track2Data);
msrDevice.DataEventEnabled = true;
msrDevice.DeviceEnabled = true;
}
|
Here we are just populating some text boxes on the UI with the data that is captured on the device. So there is not anything too special here, but the interesting part is the last two lines where we have to set the DataEventEnabled and DeviceEnabled properties to true. Once data is captured on a device, these properties both get set to false. We have to set both properties back to true so we can capture the data from subsequent card swipes.
Final Cleanup
When we are finished using our device, we need to do some cleanup to release the device and allow it to be reused later or by other application. Most of the devices are exclusive use devices and therefore can only be claimed by one application or process at a time. If a device is open and another application tries to claim it, the device will throw an exception in the application trying to claim it.
?
|1
|
2
3
4
5
|if (msrDevice != null)
{
msrDevice.Release();
msrDevice.Close();
}
|
This will release the exclusive lock on the device and close it. This could be placed in the Dispose method of a class so the class will clean up its use of the Msr device once it is disposed.
Final Thoughts
The Microsoft Point of Service for .NET library contains common objects for accessing and using up to 36 different types of devices. This makes the library very power and useful when you need to access one of these devices from your application. Hopefully this post will provide you some guidance to help you when getting started using the Microsoft Point of Service for .NET library.