Click or drag to resize

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
)

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: MagTrackData
A MagTrackData instance containing the magnetic data read from the card.
Exceptions
ExceptionCondition
ConnectionExceptionIf the device is busy or there is an error communicating with the printer.
SettingsExceptionIf the job settings are not valid.
TimeoutExceptionIf the process times out while trying to read the data.
ZebraCardExceptionIf a printer error occurs.
ZebraIllegalArgumentExceptionIf tracksToRead is invalid or an error occurs while building the job.
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);
        }
    }

-    #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()) {
                // Set the card destination to 'Hold'
                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)
                };

                // Set the card source to 'Internal'
                zebraCardPrinter.SetJobSetting(ZebraCardJobSettingNames.CARD_SOURCE, "Internal");

                // Send job
                int jobId = zebraCardPrinter.Print(1, graphicsData);

                // Poll job status
                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);
        }
    }

-    #region Graphics
     /// <exception cref="ConnectionException"></exception>
     /// <exception cref="System.IO.IOException"></exception>
     /// <exception cref="NotSupportedException"></exception>
     /// <exception cref="System.Security.SecurityException"></exception>
     /// <exception cref="UnauthorizedAccessException"></exception>
     /// <exception cref="Zebra.Sdk.Card.Exceptions.ZebraCardException"></exception>
     /// <exception cref="Zebra.Sdk.Device.ZebraIllegalArgumentException"></exception>
     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

-    #region JobStatus
     /// <exception cref="ArgumentException"></exception>
     /// <exception cref="ConnectionException"></exception>
     /// <exception cref="IOException"></exception>
     /// <exception cref="OverflowException"></exception>
     /// <exception cref="Zebra.Sdk.Settings.SettingsException"></exception>
     /// <exception cref="Zebra.Sdk.Card.Exceptions.ZebraCardException"></exception>
     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

-    #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