JobUtilReadMagData Method (DataSource, Boolean)
|
Reads the data from a magnetically encoded card.
Namespace:
Zebra.Sdk.Card.Job
Assembly:
SdkApi_Card_Core (in SdkApi_Card_Core.dll) Version: 2.14.1989
Syntax MagTrackData ReadMagData(
DataSource tracksToRead,
bool isVerbose
)
Function ReadMagData (
tracksToRead As DataSource,
isVerbose As Boolean
) As MagTrackData
MagTrackData^ ReadMagData(
DataSource tracksToRead,
bool isVerbose
)
Parameters
- tracksToRead
- Type: Zebra.Sdk.Card.EnumerationsDataSource
Specifies which tracks to read data from. - isVerbose
- Type: SystemBoolean
True to increase the amount of detail presented to the user (Console.Out).
Return Value
Type:
MagTrackDataA
MagTrackData instance containing the magnetic data read from the card.
Exceptions Examples Demonstrates how to read data from a magnetic card.
using System;
using Zebra.Sdk.Card.Containers;
using Zebra.Sdk.Card.Enumerations;
using Zebra.Sdk.Card.Printer;
using Zebra.Sdk.Comm;
public class ReadMagneticCardExample {
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()) {
DataSource tracksToRead = DataSource.Track1 | DataSource.Track2 | DataSource.Track3;
MagTrackData trackData = zebraCardPrinter.ReadMagData(tracksToRead, true);
if ((tracksToRead & DataSource.Track1) == DataSource.Track1) {
Console.WriteLine($"Track 1 Data: {trackData.Track1}");
}
if ((tracksToRead & DataSource.Track2) == DataSource.Track2) {
Console.WriteLine($"Track 2 Data: {trackData.Track2}");
}
if ((tracksToRead & DataSource.Track3) == DataSource.Track3) {
Console.WriteLine($"Track 3 Data: {trackData.Track3}");
}
} else {
Console.WriteLine("No magnetic encoder installed.");
}
} catch (Exception e) {
Console.WriteLine($"Error reading magnetic data: {e.Message}");
} finally {
CloseQuietly(connection, zebraCardPrinter);
}
}
+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
}
Examples Demonstrates how to read from, and print to, a magnetic card.
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Threading;
using Zebra.Sdk.Card.Containers;
using Zebra.Sdk.Card.Enumerations;
using Zebra.Sdk.Card.Graphics;
using Zebra.Sdk.Card.Job;
using Zebra.Sdk.Card.Printer;
using Zebra.Sdk.Comm;
public class ReadMagneticCardAndPrintExample {
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.CARD_DESTINATION, "Hold");
DataSource tracksToRead = DataSource.Track1 | DataSource.Track2 | DataSource.Track3;
MagTrackData trackData = zebraCardPrinter.ReadMagData(tracksToRead, true);
List<GraphicsInfo> graphicsData = new List<GraphicsInfo>() {
DrawGraphics(zebraCardPrinter, trackData)
};
zebraCardPrinter.SetJobSetting(ZebraCardJobSettingNames.CARD_SOURCE, "Internal");
int jobId = zebraCardPrinter.Print(1, graphicsData);
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 reading and printing magnetic card: {e.Message}");
} finally {
CloseQuietly(connection, zebraCardPrinter);
}
}
+Graphics- #region Graphics
private static GraphicsInfo DrawGraphics(ZebraCardPrinter zebraCardPrinter, MagTrackData trackData) {
using (ZebraCardGraphics graphics = new ZebraCardGraphics(zebraCardPrinter)) {
graphics.Initialize(0, 0, OrientationType.Landscape, PrintType.MonoK, Color.White);
using (Font font = new Font("Arial", 12)) {
graphics.DrawText($"Track 1 Data: {trackData.Track1}", font, Color.Black, 50, 50);
graphics.DrawText($"Track 2 Data: {trackData.Track2}", font, Color.Black, 50, 150);
graphics.DrawText($"Track 3 Data: {trackData.Track3}", font, Color.Black, 50, 250);
ZebraCardImageI zebraCardImage = graphics.CreateImage();
return AddImage(CardSide.Front, PrintType.MonoK, 0, 0, -1, zebraCardImage);
}
}
}
private static GraphicsInfo AddImage(CardSide side, PrintType printType, int xOffset, int yOffset, int fillColor, ZebraCardImageI zebraCardImage) {
return new GraphicsInfo {
Side = side,
PrintType = printType,
GraphicType = zebraCardImage != null ? GraphicType.BMP : GraphicType.NA,
XOffset = xOffset,
YOffset = yOffset,
FillColor = fillColor,
Opacity = 0,
Overprint = false,
GraphicData = zebraCardImage ?? null
};
}
#endregion Graphics
+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}, 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