前面的主题介绍了如何创建能够支持即插即用的基本服务对象模板。 本部分介绍如何创建具有以下新功能的有限示例:
- 实现必要的抽象方法,以成功地编译示例。
- 服务对象将由使用 PosExplorer 的应用程序识别,例如,SDK 随附的 POS for .NET 测试应用程序。
- 应用程序现在可以在服务对象或访问属性上调用方法,但不会返回有用的结果。
要求
若要编译此示例,项目必须具有正确的引用和全局属性。
示例
using System;
using Microsoft.PointOfService;
using Microsoft.PointOfService.BaseServiceObjects;
namespace Samples.ServiceObjects.MSR
{
[HardwareId(@"HID\Vid_05e0&Pid_038a", @"HID\Vid_05e0&Pid_038a")]
[ServiceObject(DeviceType.Msr,
"SampleMsr",
"Sample Msr Service Object",
1,
9)]
public class SampleMsr : MsrBase
{
// String returned from CheckHealth
private string MyHealthText;
public SampleMsr()
{
// Initialize device capability properties.
Properties.CapIso = true;
Properties.CapTransmitSentinels = true;
Properties.DeviceDescription = "Sample MSR";
// Initialize other class variables.
MyHealthText = "";
}
~SampleMsr()
{
Dispose(false);
}
// Release any resources managed by this object.
protected override void Dispose(bool disposing)
{
try
{
// Your code here.
}
finally
{
// Must call base class Dispose.
base.Dispose(disposing);
}
}
#region PosCommon overrides
// Returns the result of the last call to CheckHealth().
public override string CheckHealthText
{
get
{
// MsrBasic.VerifyState(mustBeClaimed,
// mustBeEnabled). This may throw an exception.
VerifyState(false, false);
return MyHealthText;
}
}
public override string CheckHealth(
HealthCheckLevel level)
{
// Verify that device is open, claimed, and enabled.
VerifyState(true, true);
// Your code here:
// check the health of the device and return a
// descriptive string.
// Cache result in the CheckHealthText property.
MyHealthText = "Ok";
return MyHealthText;
}
public override DirectIOData DirectIO(
int command,
int data,
object obj)
{
// Verify that device is open.
VerifyState(false, false);
return new DirectIOData(data, obj);
}
#endregion // PosCommon overrides
#region MsrBasic Overrides
protected override MsrFieldData ParseMsrFieldData(
byte[] track1Data,
byte[] track2Data,
byte[] track3Data,
byte[] track4Data,
CardType cardType)
{
// Your code here:
// Implement this method to parse track data
// into fields which will be returned as
// properties to the application
// (for example, FirstName,
// AccountNumber, etc.)
return new MsrFieldData();
}
protected override MsrTrackData ParseMsrTrackData(
byte[] track1Data,
byte[] track2Data,
byte[] track3Data,
byte[] track4Data,
CardType cardType)
{
// Your code here:
// Implement this method to convert raw track data.
return new MsrTrackData();
}
#endregion
}
}
为了简化此示例,代码不实现任何全球化功能。 例如,通常从本地化的字符串资源文件中读取 Properties.DeviceDescription 的值。