建立服務物件範例 (POS for .NET v1.14 SDK 文件)
先前的主題說明如何使用「隨插即用」支援來建立基本的「服務物件範例」。 本節會新增如何使用下列新功能建立有限的範例:
- 已實作必要的抽象方法,以便成功編譯範例。
- 「服務物件」會由使用 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 的值通常會從當地語系化字串資源檔讀取。