Commit 1a83758c 刘韬

1

1 个父辈 21403dfb
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.Diagnostics;
using System.Drawing; using System.Drawing;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
...@@ -92,10 +93,15 @@ namespace ZebraPrinterHelper ...@@ -92,10 +93,15 @@ namespace ZebraPrinterHelper
msg = ""; msg = "";
try try
{ {
Debug.WriteLine("Connection1");
printerConnection = GetConnection(); printerConnection = GetConnection();
Debug.WriteLine("Connection2");
printerConnection.Open(); printerConnection.Open();
Debug.WriteLine("Connection3");
PrinterDPI = GetPrinterDPI(); PrinterDPI = GetPrinterDPI();
Debug.WriteLine("Connection4");
printer = ZebraPrinterFactory.GetInstance(printerConnection); printer = ZebraPrinterFactory.GetInstance(printerConnection);
Debug.WriteLine("Connection5");
return true; return true;
} }
catch (ConnectionException e) catch (ConnectionException e)
...@@ -108,15 +114,19 @@ namespace ZebraPrinterHelper ...@@ -108,15 +114,19 @@ namespace ZebraPrinterHelper
} }
catch (IOException e) catch (IOException e)
{ {
msg = "Image Error" + e.Message; msg = "Image Error:" + e.Message;
} }
catch (ZebraIllegalArgumentException e) catch (ZebraIllegalArgumentException e)
{ {
msg = "Illegal Arguments" + e.Message; msg = "Illegal Arguments:" + e.Message;
} }
catch (ArgumentException e) catch (ArgumentException e)
{ {
msg = "Invalid File Path" + e.Message; msg = "Invalid File Path:" + e.Message;
}
catch (Exception e)
{
msg = "Exception:" + e.Message;
} }
finally finally
{ {
...@@ -165,26 +175,36 @@ namespace ZebraPrinterHelper ...@@ -165,26 +175,36 @@ namespace ZebraPrinterHelper
/// <param name="msg">错误消息</param> /// <param name="msg">错误消息</param>
/// <param name="checkPeeler">是否检查剥离器</param> /// <param name="checkPeeler">是否检查剥离器</param>
/// <returns>是否成功</returns> /// <returns>是否成功</returns>
public bool PrintImage(Bitmap bmp, out string msg,bool checkPeeler) public bool PrintImage(Bitmap bmp, out string msg, bool checkPeeler, bool checkstatus = true) {
return PrintImage(bmp, bmp.Width, bmp.Height, out msg, checkPeeler, checkstatus);
}
public bool PrintImage(Bitmap bmp,int width, int height, out string msg,bool checkPeeler,bool checkstatus=true)
{ {
if (!CheckAndGetStatus(out PrinterStatus status, out msg)) msg = "";
return false; Debug.WriteLine("StartPrintImage");
if (status.labelsRemainingInBatch > 0) if (checkstatus)
CancelAll();
if (!status.isReadyToPrint)
{ {
string[] printerStatusString = new PrinterStatusMessages(status).GetStatusMessage(); if (!CheckAndGetStatus(out PrinterStatus status, out msg))
return false;
Debug.WriteLine("CheckAndGetStatus");
if (status.labelsRemainingInBatch > 0)
CancelAll();
Debug.WriteLine("labelsRemainingInBatch");
if (!status.isReadyToPrint)
{
string[] printerStatusString = new PrinterStatusMessages(status).GetStatusMessage();
msg = string.Join("\r\n", printerStatusString); msg = string.Join("\r\n", printerStatusString);
return false; return false;
} }
if (checkPeeler && IsLabelOnPeeler) Debug.WriteLine("isReadyToPrint");
{ if (checkPeeler && IsLabelOnPeeler)
msg = "上一个标签尚未移走"; {
return false; msg = "上一个标签尚未移走";
return false;
}
Debug.WriteLine("IsLabelOnPeeler");
} }
try try
{ {
//var width = bmp.Width * ((double)PrinterDPI / 300); //var width = bmp.Width * ((double)PrinterDPI / 300);
...@@ -192,6 +212,7 @@ namespace ZebraPrinterHelper ...@@ -192,6 +212,7 @@ namespace ZebraPrinterHelper
//var db = GetReducedImage(bmp, (int)width, (int)height); //var db = GetReducedImage(bmp, (int)width, (int)height);
//db.Save("temp.bmp"); //db.Save("temp.bmp");
ZebraImageI image = ZebraImageFactory.GetImage(bmp); ZebraImageI image = ZebraImageFactory.GetImage(bmp);
Debug.WriteLine("ZebraImageFactory.GetImage");
printer.PrintImage(image, 0, 0, (int)bmp.Width, (int)bmp.Height, false); printer.PrintImage(image, 0, 0, (int)bmp.Width, (int)bmp.Height, false);
return true; return true;
} }
...@@ -209,11 +230,15 @@ namespace ZebraPrinterHelper ...@@ -209,11 +230,15 @@ namespace ZebraPrinterHelper
} }
catch (ZebraIllegalArgumentException e) catch (ZebraIllegalArgumentException e)
{ {
msg = "Illegal Arguments" + e.ToString(); msg = "Illegal Arguments:" + e.ToString();
} }
catch (ArgumentException e) catch (ArgumentException e)
{ {
msg = "Invalid File Path" + e.ToString(); msg = "Invalid File Path:" + e.ToString();
}
catch (Exception e)
{
msg = "Exception:" + e.Message;
} }
finally finally
{ {
...@@ -235,18 +260,25 @@ namespace ZebraPrinterHelper ...@@ -235,18 +260,25 @@ namespace ZebraPrinterHelper
return false; return false;
} }
} }
Debug.WriteLine("StartGetStatus");
status = GetStatus(); status = GetStatus();
if (status == null) if (status == null)
{ {
Close(); Debug.WriteLine("GetStatusAgain");
ReTryCount++; status = GetStatus();
if (ReTryCount > 3) if (status == null)
{ {
msg = "连接打印机失败"; Close();
return false; ReTryCount++;
if (ReTryCount > 3)
{
msg = "连接打印机失败";
return false;
}
goto ReTry;
} }
goto ReTry;
} }
Debug.WriteLine("isPaused");
ReTryCount = 0; ReTryCount = 0;
if (status.isPaused) if (status.isPaused)
{ {
...@@ -380,8 +412,10 @@ namespace ZebraPrinterHelper ...@@ -380,8 +412,10 @@ namespace ZebraPrinterHelper
/// <returns></returns> /// <returns></returns>
public PrinterStatus GetStatus() public PrinterStatus GetStatus()
{ {
if (printerConnection == null || !printerConnection.Connected) if (printerConnection == null || !printerConnection.Connected)
{ {
Debug.WriteLine("ReConnection");
if (!Connection(out string m)) if (!Connection(out string m))
{ {
//msg = m; //msg = m;
...@@ -390,12 +424,15 @@ namespace ZebraPrinterHelper ...@@ -390,12 +424,15 @@ namespace ZebraPrinterHelper
} }
try try
{ {
Debug.WriteLine("GetCurrentStatus");
return printer.GetCurrentStatus(); return printer.GetCurrentStatus();
//ZebraPrinterLinkOs linkOsPrinter = ZebraPrinterFactory.CreateLinkOsPrinter(printer); //ZebraPrinterLinkOs linkOsPrinter = ZebraPrinterFactory.CreateLinkOsPrinter(printer);
//Debug.WriteLine("linkOsPrinter:"+ linkOsPrinter==null);
//return (linkOsPrinter != null) ? linkOsPrinter.GetCurrentStatus() : printer.GetCurrentStatus(); //return (linkOsPrinter != null) ? linkOsPrinter.GetCurrentStatus() : printer.GetCurrentStatus();
} }
catch catch(Exception ex)
{ {
Debug.WriteLine("GetCurrentStatusError:"+ex);
return null; return null;
} }
} }
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!