Click or drag to resize

JobUtilSmartCardEncode Method

Positions a card at the smart card station, suspends the job to allow the card to be read or encoded, and waits for Resume to be called before completing the job.

Namespace:  Zebra.Sdk.Card.Job
Assembly:  SdkApi_Card_Core (in SdkApi_Card_Core.dll) Version: 2.14.1989
Syntax
int SmartCardEncode(
	int copies
)

Parameters

copies
Type: SystemInt32
Number of cards to be encoded.

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 copies is invalid.
Remarks
  • Smart card job settings must be configured prior to calling this method in order to specify the card type.
Examples
Demonstrates how to position a smart card for encoding.
using System;
using System.Collections.Generic;
using System.Threading;
using Zebra.Sdk.Card.Containers;
using Zebra.Sdk.Card.Job;
using Zebra.Sdk.Card.Printer;
using Zebra.Sdk.Comm;

public class PositionSmartCardExample {

    private const int CARD_FEED_TIMEOUT = 30000;

    public static void Main(string[] args) {
        Connection connection = null;
        ZebraPrinterZmotif zmotifCardPrinter = null;

        try {
            connection = new UsbConnection("\\\\?\\usb#vid_0a5f&pid_00a7#411738706#...");
            connection.Open();

            zmotifCardPrinter = ZebraCardPrinterFactory.GetZmotifPrinter(connection);

            if (zmotifCardPrinter.HasSmartCardEncoder()) {
                ConfigureSmartCardJobSettings(zmotifCardPrinter);

                // Send job and poll for status
                int jobId = zmotifCardPrinter.SmartCardEncode(1);
                JobStatusInfo jobStatus = PollJobStatus(jobId, zmotifCardPrinter);
                Console.WriteLine($"Job {jobId} completed with status '{jobStatus.PrintStatus}'.");
            } else {
                Console.WriteLine("No smart card encoder installed.");
            }
        } catch (Exception e) {
            Console.WriteLine($"Error positioning card: {e.Message}");
        } finally {
            CloseQuietly(connection, zmotifCardPrinter);
        }
    }

-    #region SmartCard
     /// <exception cref="ArgumentException"></exception>
     /// <exception cref="ConnectionException"></exception>
     /// <exception cref="System.IO.IOException"></exception>
     /// <exception cref="Zebra.Sdk.Settings.SettingsException"></exception>
     /// <exception cref="Zebra.Sdk.Card.Exceptions.ZebraCardException"></exception>
     private static void ConfigureSmartCardJobSettings(ZebraCardPrinter zebraCardPrinter) {
         Console.Write("Available smart card encoding types: ");
         Dictionary<string, string> smartCardEncoders = zebraCardPrinter.GetSmartCardConfigurations();
         Console.WriteLine(string.Join(", ", smartCardEncoders.Keys));
 
         // Configure smart card encoding type
         string encoderType = "";
         if (smartCardEncoders.ContainsKey("mifare")) {
             encoderType = "mifare";
         } else if (smartCardEncoders.ContainsKey("hf")) {
             encoderType = "hf";
         } else if (smartCardEncoders.ContainsKey("other")) {
             encoderType = "other";
         } else if (smartCardEncoders.ContainsKey("contact") || smartCardEncoders.ContainsKey("contact_station")) {
             encoderType = smartCardEncoders.ContainsKey("contact") ? "contact" : "contact_station";
         }
 
         if (!string.IsNullOrEmpty(encoderType)) {
             Console.WriteLine($"Setting encoder type to: {encoderType}");
             if (encoderType.Contains("contact")) {
                 zebraCardPrinter.SetJobSetting(ZebraCardJobSettingNames.SMART_CARD_CONTACT, "yes");
             } else {
                 zebraCardPrinter.SetJobSetting(ZebraCardJobSettingNames.SMART_CARD_CONTACTLESS, encoderType);
             }
         }
     }
     #endregion SmartCard

-    #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}, contact:{jobStatusInfo.ContactSmartCardStatus}, " +
                 $"contactless:{jobStatusInfo.ContactlessSmartCardStatus}, alarm:{jobStatusInfo.AlarmInfo.Value}{alarmDesc}, error:{jobStatusInfo.ErrorInfo.Value}{errorDesc}");
 
             if (jobStatusInfo.PrintStatus.Contains("done_ok")) {
                 break;
             } else if (jobStatusInfo.ContactSmartCardStatus.Contains("at_station") || jobStatusInfo.ContactlessSmartCardStatus.Contains("at_station")) {
                 // Perform smart card encoding operations, resume job when complete
                 WaitForUserInput(zebraCardPrinter);
             } 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;
     }
 
     private static void WaitForUserInput(ZebraCardPrinter printer) {
         while (true) {
             try {
                 Console.Write("Card staged for encoding. Press 'r' to resume or 'c' to cancel: ");
                 char key = Console.ReadKey().KeyChar;
                 if (key.Equals('r') || key.Equals('R')) {
                     Console.WriteLine("\nResuming smartcard job");
                     printer.Resume();
                     break;
                 } else if (key.Equals('c') || key.Equals('C')) {
                     Console.WriteLine("\nCancelling smart card job");
                     if (printer is ZebraPrinterZmotif) {
                         ((ZebraPrinterZmotif)printer).Abort(true);
                     } else {
                         printer.Cancel(0);
                     }
                     break;
                 }
             } catch { }
         }
     }
     #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
}
Examples
Demonstrates how to position and print to a smart card.
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Threading;
using Zebra.Sdk.Card.Containers;
using Zebra.Sdk.Card.Enumerations;
using Zebra.Sdk.Card.Graphics;
using Zebra.Sdk.Card.Graphics.Enumerations;
using Zebra.Sdk.Card.Job;
using Zebra.Sdk.Card.Printer;
using Zebra.Sdk.Comm;

public class PositionSmartCardAndPrintExample {

    private const int CARD_FEED_TIMEOUT = 30000;

    public static void Main(string[] args) {
        Connection connection = null;
        ZebraPrinterZmotif zmotifCardPrinter = null;

        try {
            connection = new UsbConnection("\\\\?\\usb#vid_0a5f&pid_00a7#411738706#...");
            connection.Open();

            zmotifCardPrinter = ZebraCardPrinterFactory.GetZmotifPrinter(connection);

            if (zmotifCardPrinter.HasSmartCardEncoder()) {
                ConfigureSmartCardJobSettings(zmotifCardPrinter);
                List<GraphicsInfo> graphicsData = DrawGraphics(zmotifCardPrinter);

                // Send job and poll for status
                int jobId = zmotifCardPrinter.Print(1, graphicsData);
                JobStatusInfo jobStatus = PollJobStatus(jobId, zmotifCardPrinter);
                Console.WriteLine($"Job {jobId} completed with status '{jobStatus.PrintStatus}'.");
            } else {
                Console.WriteLine("No smart card encoder installed.");
            }
        } catch (Exception e) {
            Console.WriteLine($"Error printing and positioning card: {e.Message}");
        } finally {
            CloseQuietly(connection, zmotifCardPrinter);
        }
    }

-    #region Graphics
     /// <exception cref="ConnectionException"></exception>
     /// <exception cref="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 List<GraphicsInfo> DrawGraphics(ZebraCardPrinter zebraCardPrinter) {
         // Generate image data
         ZebraCardImageI zebraCardImage = null;
         List<GraphicsInfo> graphicsData = new List<GraphicsInfo>();
 
         using (ZebraGraphics graphics = new ZebraCardGraphics(zebraCardPrinter)) {
             // Front side color
             string colorImagePath = @"path\to\myColorImage.bmp";
             byte[] imageData = File.ReadAllBytes(colorImagePath);
 
             zebraCardImage = DrawImage(graphics, PrintType.Color, imageData, 0, 0, 0, 0);
             graphicsData.Add(AddImage(CardSide.Front, PrintType.Color, 0, 0, -1, zebraCardImage));
         }
         return graphicsData;
     }
 
     /// <exception cref="Zebra.Sdk.Card.Exceptions.ZebraCardException"></exception>
     /// <exception cref="Zebra.Sdk.Device.ZebraIllegalArgumentException"></exception>
     private static ZebraCardImageI DrawImage(ZebraGraphics graphics, PrintType printType, byte[] imageData, int xOffset, int yOffset, int width, int height) {
         graphics.Initialize(0, 0, OrientationType.Landscape, printType, Color.White);
         graphics.DrawImage(imageData, xOffset, yOffset, width, height, RotationType.RotateNoneFlipNone);
         return graphics.CreateImage();
     }
 
     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 SmartCard
     /// <exception cref="ArgumentException"></exception>
     /// <exception cref="ConnectionException"></exception>
     /// <exception cref="IOException"></exception>
     /// <exception cref="Zebra.Sdk.Settings.SettingsException"></exception>
     /// <exception cref="Zebra.Sdk.Card.Exceptions.ZebraCardException"></exception>
     private static void ConfigureSmartCardJobSettings(ZebraCardPrinter zebraCardPrinter) {
         Console.Write("Available smart card encoding types: ");
         Dictionary<string, string> smartCardEncoders = zebraCardPrinter.GetSmartCardConfigurations();
         Console.WriteLine(string.Join(", ", smartCardEncoders.Keys));
 
         // Configure smart card encoding type
         string encoderType = "";
         if (smartCardEncoders.ContainsKey("mifare")) {
             encoderType = "mifare";
         } else if (smartCardEncoders.ContainsKey("hf")) {
             encoderType = "hf";
         } else if (smartCardEncoders.ContainsKey("other")) {
             encoderType = "other";
         } else if (smartCardEncoders.ContainsKey("contact") || smartCardEncoders.ContainsKey("contact_station")) {
             encoderType = smartCardEncoders.ContainsKey("contact") ? "contact" : "contact_station";
         }
 
         if (!string.IsNullOrEmpty(encoderType)) {
             Console.WriteLine($"Setting encoder type to: {encoderType}");
             if (encoderType.Contains("contact")) {
                 zebraCardPrinter.SetJobSetting(ZebraCardJobSettingNames.SMART_CARD_CONTACT, "yes");
             } else {
                 zebraCardPrinter.SetJobSetting(ZebraCardJobSettingNames.SMART_CARD_CONTACTLESS, encoderType);
             }
         }
     }
     #endregion SmartCard

-    #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}, contact:{jobStatusInfo.ContactSmartCardStatus}, " +
                 $"contactless:{jobStatusInfo.ContactlessSmartCardStatus}, alarm:{jobStatusInfo.AlarmInfo.Value}{alarmDesc}, error:{jobStatusInfo.ErrorInfo.Value}{errorDesc}");
 
             if (jobStatusInfo.PrintStatus.Contains("done_ok")) {
                 break;
             } else if (jobStatusInfo.ContactSmartCardStatus.Contains("at_station") || jobStatusInfo.ContactlessSmartCardStatus.Contains("at_station")) {
                 // Perform smart card encoding operations, resume job when complete
                 WaitForUserInput(zebraCardPrinter);
             } 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;
     }
 
     private static void WaitForUserInput(ZebraCardPrinter printer) {
         while (true) {
             try {
                 Console.Write("Card staged for encoding. Press 'r' to resume or 'c' to cancel: ");
                 char key = Console.ReadKey().KeyChar;
                 if (key.Equals('r') || key.Equals('R')) {
                     Console.WriteLine("\nResuming smartcard job");
                     printer.Resume();
                     break;
                 } else if (key.Equals('c') || key.Equals('C')) {
                     Console.WriteLine("\nCancelling smartcard job");
                     if (printer is ZebraPrinterZmotif) {
                         ((ZebraPrinterZmotif)printer).Abort(true);
                     } else {
                         printer.Cancel(0);
                     }
                     break;
                 }
             } catch { }
         }
     }
     #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