Encodes a magnetic card for the current job.
Namespace:
Zebra.Sdk.Card.Job
Assembly:
SdkApi_Card_Core (in SdkApi_Card_Core.dll) Version: 2.14.1989
Syntax int MagEncode(
int copies,
string track1Data,
string track2Data,
string track3Data
)
Function MagEncode (
copies As Integer,
track1Data As String,
track2Data As String,
track3Data As String
) As Integer
int MagEncode(
int copies,
String^ track1Data,
String^ track2Data,
String^ track3Data
)
Parameters
- copies
- Type: SystemInt32
Number of copies to be encoded. - track1Data
- Type: SystemString
Data to be encoded on track 1. - track2Data
- Type: SystemString
Data to be encoded on track 2. - track3Data
- Type: SystemString
Data to be encoded on track 3.
Return Value
Type:
Int32The assigned job ID number.
Exceptions Examples Demonstrates how to encode a magnetic card.
using System;
using System.Threading;
using Zebra.Sdk.Card.Containers;
using Zebra.Sdk.Card.Job;
using Zebra.Sdk.Card.Printer;
using Zebra.Sdk.Comm;
public class MagneticEncodeExample {
private const int CARD_FEED_TIMEOUT = 30000;
public static void Main(string[] args) {
Connection connection = null;
ZebraCardPrinter zebraCardPrinter = null;
try {
connection = new TcpConnection("1.2.3.4", 9100);
connection.Open();
zebraCardPrinter = ZebraCardPrinterFactory.GetInstance(connection);
if (zebraCardPrinter.HasMagneticEncoder()) {
zebraCardPrinter.SetJobSetting(ZebraCardJobSettingNames.MAG_ENCODING_TYPE, "ISO");
zebraCardPrinter.SetJobSetting(ZebraCardJobSettingNames.MAG_COERCIVITY, "High");
zebraCardPrinter.SetJobSetting(ZebraCardJobSettingNames.MAG_VERIFY, "yes");
int jobId = zebraCardPrinter.MagEncode(1, "Zebra Technologies", "2222222222", "3333333333");
JobStatusInfo jobStatus = PollJobStatus(jobId, zebraCardPrinter);
Console.WriteLine($"Job {jobId} completed with status '{jobStatus.PrintStatus}'.");
} else {
Console.WriteLine("No magnetic encoder installed.");
}
} catch (Exception e) {
Console.WriteLine($"Error encoding magnetic card: {e.Message}");
} finally {
CloseQuietly(connection, zebraCardPrinter);
}
}
+JobStatus- #region JobStatus
private static JobStatusInfo PollJobStatus(int jobId, ZebraCardPrinter zebraCardPrinter) {
JobStatusInfo jobStatusInfo = new JobStatusInfo();
bool isFeeding = false;
long start = Math.Abs(Environment.TickCount);
while (true) {
jobStatusInfo = zebraCardPrinter.GetJobStatus(jobId);
if (!isFeeding) {
start = Math.Abs(Environment.TickCount);
}
isFeeding = jobStatusInfo.CardPosition.Contains("feeding");
string alarmDesc = jobStatusInfo.AlarmInfo.Value > 0 ? $" ({jobStatusInfo.AlarmInfo.Description})" : "";
string errorDesc = jobStatusInfo.ErrorInfo.Value > 0 ? $" ({jobStatusInfo.ErrorInfo.Description})" : "";
Console.WriteLine($"Job {jobId}: status:{jobStatusInfo.PrintStatus}, position:{jobStatusInfo.CardPosition}, mag:{jobStatusInfo.MagneticEncodingStatus}, " +
$"alarm:{jobStatusInfo.AlarmInfo.Value}{alarmDesc}, error:{jobStatusInfo.ErrorInfo.Value}{errorDesc}");
if (jobStatusInfo.PrintStatus.Contains("done_ok")) {
break;
} else if (jobStatusInfo.PrintStatus.Contains("error") || jobStatusInfo.PrintStatus.Contains("cancelled")) {
Console.WriteLine($"The job encountered an error [{jobStatusInfo.ErrorInfo.Description}] and was cancelled.");
break;
} else if (jobStatusInfo.ErrorInfo.Value > 0) {
Console.WriteLine($"The job encountered an error [{jobStatusInfo.ErrorInfo.Description}] and was cancelled.");
zebraCardPrinter.Cancel(jobId);
} else if (jobStatusInfo.PrintStatus.Contains("in_progress") && isFeeding) {
if (Math.Abs(Environment.TickCount) > start + CARD_FEED_TIMEOUT) {
Console.WriteLine("The job timed out waiting for a card and was cancelled.");
zebraCardPrinter.Cancel(jobId);
}
}
Thread.Sleep(1000);
}
return jobStatusInfo;
}
#endregion JobStatus
+CleanUp- #region CleanUp
private static void CloseQuietly(Connection connection, ZebraCardPrinter zebraCardPrinter) {
try {
if (zebraCardPrinter != null) {
zebraCardPrinter.Destroy();
}
} catch { }
try {
if (connection != null) {
connection.Close();
}
} catch { }
}
#endregion CleanUp
}
See Also