Click or drag to resize

JobUtilMagEncode Method

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
)

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: Int32
The assigned job ID number.
Exceptions
ExceptionCondition
ConnectionExceptionIf the device is busy or there is an error communicating with the printer.
SettingsExceptionIf the job settings are not valid.
ZebraCardExceptionIf a printer error occurs or there is an error creating the job.
ZebraIllegalArgumentExceptionIf any of the supplied arguments are invalid or an error occurs while building the job.
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()) {
                // Configure magnetic encoding settings
                zebraCardPrinter.SetJobSetting(ZebraCardJobSettingNames.MAG_ENCODING_TYPE, "ISO");  // ISO=default
                zebraCardPrinter.SetJobSetting(ZebraCardJobSettingNames.MAG_COERCIVITY, "High");    // High=default
                zebraCardPrinter.SetJobSetting(ZebraCardJobSettingNames.MAG_VERIFY, "yes");         // yes=default

                // Send job
                int jobId = zebraCardPrinter.MagEncode(1, "Zebra Technologies", "2222222222", "3333333333");

                // 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 encoding magnetic card: {e.Message}");
        } finally {
            CloseQuietly(connection, zebraCardPrinter);
        }
    }

-    #region JobStatus
     /// <exception cref="ArgumentException"></exception>
     /// <exception cref="ConnectionException"></exception>
     /// <exception cref="System.IO.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}, 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

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