LinePrinter Tutorial: LinePrinter Tutorial Source Code

using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Honeywell.Mobility.Print;
using System.Windows.Forms;
namespace SmartDeviceProject1
{
   class Program
   {
      static LinePrinter lp = null;
      static void Main(string[] args)
      {
         try
         {
            lp = new LinePrinter("ITC_CONFIG.XML", "PR2Bt_40COL");
            lp.WarningMessageEventEx += new LinePrinter.WarningMessageEventHandler(WarningHandler);
            lp.Open();
            for (int i = 1; i <= 100; i++)
            {
               lp.WriteLn("This is line " + i.ToString());
            }
            lp.NewLine();
            lp.Close();
         }
         catch (Exception x)
         {
            try
            {
               lp.Close();
            }
            catch
            {
            } // Ignore any errors coming from Close this time.
            ShowError(x);
         }
         lp.WarningMessageEventEx -= new LinePrinter.WarningMessageEventHandler(WarningHandler);
      }
      static void WarningHandler(object source, LinePrinter.WarningMessageArgs Error)
      {
         string temp;
         DialogResult result;
         temp = string.Format("{0} from {1} Printer \n\r\n\r Fix problem and continue printing?",
             GetErrorStr(Error.ErrorCode), Error.DeviceType);
         result = MessageBox.Show(temp, "PRINTER WARNING", MessageBoxButtons.YesNo,
             MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
         if (result == DialogResult.No)
            lp.Cancel();
      }
      static public void ShowError(Exception e)
      {
         string Message = "UNKNOWN ERROR WHILE PRINTING";
         if (e is LinePrinterException)
         {
            LinePrinterException lpe = (LinePrinterException) e;
            if (lpe.ErrorType == LinePrinterException.ErrorTypes.CONFIG_ERROR)
            {
               Message =
               "PRINTER CONFIGURATION ERROR\r\n\r\nPossibly invalid configuration file!";
            }
            else if (lpe.ErrorType == LinePrinterException.ErrorTypes.PRINTER_ERROR)
            {
               Message = string.Format(
                   "PRINTER ERROR\n\r\n\rError # {0} - {1}",
                   lpe.ErrorCode, GetErrorStr(lpe.ErrorCode));
            }
         }
         MessageBox.Show(Message,
             "REPORT ERROR!",
             MessageBoxButtons.OK, MessageBoxIcon.Exclamation,
             MessageBoxDefaultButton.Button1);
      }
      static string GetErrorStr(int Err)
      {
         switch (Err)
         {
            case 102:
               return "Printer Not Ready";
            case 104:
               return "Not Receiving";
            case 106:
               return "Not Transmitting";
            case 113:
               return "MAC Length Parity Error";
            case 120:
               return "Invalid Frame Received";
            case 121:
               return "NS != VR";
            case 122:
               return "NR != VS";
            case 123:
               return "Receive Length Error";
            case 124:
               return "CRC Error";
            case 200:
               return "Frame Reject"
            case 201:
               return "Invalid Frame Rejected";
            case 202:
               return "NR Mismatch";
            case 203:
               return "NS Mismatch";
            case 204:
               return "Disconnect";
            case 210:
               return "Bind Error";
            case 221:
               return "Invalid PLDU";
            case 222:
               return "Head Jam";
            case 223:
               return "Paper Out";
            case 224:
               return "Low Voltage";
            case 225:
               return "Over Voltage";
            case 226:
               return "Low Battery";
            case 227:
               return "Lid Open";
            case 228:
               return "Print Head Error";
            case 229:
               return "Paper Feed Error";
            case 1001:
               return "Command Error";
            case 1002:
               return "Data Error";
            case 1003:
               return "Font Error";
            case 1004:
               return "Global Parameter Error";
            default:
               return "Unknown";
         }
      }
   }
}