Implement a software trigger in a barcode scanner application
7/17/2014
This topic demonstrates how to implement a software trigger for a barcode scanner application in Windows Embedded 8.1 Handheld.
Implement a software trigger
Handheld 8.1 extends the existing PointOfService BarcodeScanner with a software trigger in addition to its support for a hardware trigger. The software trigger gives apps a way to signal the BarcodeScanner device to start scanning. It is the software equivalent of pressing the physical scanner trigger.
For example, consider the scenario of a retail self-checkout line. The self-checkout lane at your local grocery store has a BarcodeScanner integrated into the weight scale. This scanner contains no hardware trigger and is activated through software once a transaction is in progress. During the transaction, the customer can continuously scan items from their shopping cart. The software running this POS terminal needs to have the capability to trigger the BarcodeScanner to start scanning at the appropriate times. A software trigger makes this possible.
Sample code demonstrating the software scanner
The following sample code shows how to use a software trigger in a barcode scanner application for Handheld 8.1.
using Windows.Devices.PointOfService;
public struct ScanData
{
public UInt32 Symbology;
public string Data;
public string Label;
}
public async void ScannerDataReceived(ClaimedBarcodeScanner sender, BarcodeScannerDataReceivedEventArgs e)
{
ScanData scannerData = readScanDataFromBuffer(e.Report);
await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
TextBarcode.Text = String.Format("Data Received: Label:{0}, Data:{1}", scannerData.Label, scannerData.Data);
});
}
public void HookUpEventsClaimedScanner()
{
claimedScanner.DataReceived += ScannerDataReceived;
claimedScanner.ReleaseDeviceRequested += ScannerReleaseRequest;
}
public async Task<bool> GetDefaultScanner()
{
bool result = false;
try
{
scanner = await BarcodeScanner.GetDefaultAsync();
if (scanner == null)
TextBarcode.Text = "Barcode Scanner not found. Please connect a Barcode Scanner";
else
{
TextBarcode.Text = "Default Barcode Scanner created. Device Id is " + scanner.DeviceId;
result = true;
}
}
catch (Exception ex)
{
TextBarcode.Text = ex.Message;
}
return result;
}
public async Task<bool> ClaimScanner()
{
bool result = false;
try
{
claimedScanner = await scanner.ClaimScannerAsync();
if (claimedScanner != null)
{
TextBarcode.Text = "Scanner was claimed by this application";
result = true;
}
else
TextBarcode.Text = "Scanner wasn't claimed by this application";
}
catch (Exception ex)
{
TextBarcode.Text = ex.Message;
}
return result;
}
private async Task<bool> EnableClaimedScanner()
{
bool result = false;
try
{
await claimedScanner.EnableAsync();
if (claimedScanner.IsEnabled)
{
claimedScanner.IsDecodeDataEnabled = true;
TextBarcode.Text = "ClaimedScanner is now Enabled.";
result = true;
}
else
TextBarcode.Text = "ClaimedScanner wasn't Enabled.";
}
catch (Exception ex)
{
TextBarcode.Text = ex.Message;
}
return result;
}
private async void EnableScannerButton_Click(object sender, RoutedEventArgs e)
{
Task<bool> AsyncSuccess = EnableClaimedScanner();
bool x = await AsyncSuccess;
if (x)
{
HookUpEventsClaimedScanner();
}
}
private async void SoftwareTrigger_Click(object sender, RoutedEventArgs e)
{
if (scanner.Capabilities.IsSoftwareTriggerSupported)
{
await claimedScanner.StartSoftwareTriggerAsync();
}
}
Software trigger APIs
Handheld 8.1 extends the Windows BarcodeScanner by adding one additional property and two additional methods. The use of these is demonstrated is the code sample above.
Properties
- IsSoftwareTriggerSupported
This property is used to determine whether the device supports the software trigger functionality. The property returns true if the barcode scanner supports the software trigger feature; otherwise, it returns false.
Methods
- StartSoftwareTriggerAsync
This method is used to trigger the image scanner to acquire decode data. A session is active until the StopSoftwareTriggerAsync method is invoked, or until the image scanner ends the session on its own. A session may terminate early when an image or decode data is acquired, or when a session timeout has expired. The criteria for ending a session is implementation dependent.
- StopSoftwareTriggerAsync
This method is used to stop a session that was started with a StartSoftwareTriggerAsync method. If this method is invoked and the session is no longer active, then no error is raised.