public override ConnectionA GetConnection() { DiscoveredPrinterDriver driverPrinter = null; List <DiscoveredPrinterDriver> printers = UsbDiscoverer.GetZebraDriverPrinters(); if (printers == null || printers.Count <= 0) { //MessageBox.Show("没有检测到打印机,请检查打印机是否开启!"); myEventLog.LogInfo("没有检测到打印机,请检查打印机是否开启!"); return(null); } driverPrinter = printers[0]; var connection = new DriverPrinterConnection(driverPrinter.Address); connection.Open(); try { ZebraPrinterFactory.GetInstance(connection); } catch (Exception ex) { } try { ZebraPrinterFactory.GetLinkOsPrinter(connection); } catch (Exception ex) { } return(connection); }
public static bool ZebraPrinter(String Result, String IDCode) { Printer printer = new Printer("ZDesigner GT800 (ZPL)"); //构件打印字符串static StringBuilder hexBuilder = new StringBuilder(4 * 1024); hexBuilder.Append("~CT~~CD,~CC^~CT~\n" + "^XA~TA000~JSN^LT0^MNW^MTT^PON^PMN^LH0,0^JMA^PR5,5~SD15^JUS^LRN^CI0^XZ\n"); hexBuilder.Append(printer.TextToHex("中文", "000.GRF", 20)); hexBuilder.Append("^XA\n" + "^MMT\n" + "^PW609\n" + "^LL0406\n" + "^LS0\n" + "^FT128,192^XG000.GRF,1,1^FS\n" + "^PQ1,0,1,Y^XZ\n" + "^XA^ID000.GRF^FS^XZ"); //打印 try { var printerConnection = new DriverPrinterConnection("ZDesigner GT800 (ZPL)"); printerConnection.Open(); printerConnection.Write(Encoding.UTF8.GetBytes(hexBuilder.ToString())); printerConnection.Close(); return(true); } catch (Exception ex) { return(false); } }
public async Task <bool> CetakZebraAsync(PrintTicket printData, string printerName) { var parser = new FileIniDataParser(); IniData iniData = parser.ReadFile("Configuration.ini"); string serverUrl = iniData["server"]["url"]; string serverApi = iniData["server"]["api"]; Connection conn = null; try { conn = new DriverPrinterConnection(printerName); conn.Open(); ZebraPrinter zebraPrinter = ZebraPrinterFactory.GetInstance(conn); PrinterStatus printStatus = zebraPrinter.GetCurrentStatus(); if (printStatus.isReadyToPrint) { foreach (TicketData data in printData.tickets) { byte[] buffer1 = ASCIIEncoding.ASCII.GetBytes(data.ticket); conn.SendAndWaitForResponse(buffer1, 1000, 1000, null); await UpdateStatus(data.id, serverApi, serverUrl, "printed"); } conn.Close(); return(true); } else { MessageBox.Show("Printer is not ready!"); foreach (TicketData data in printData.tickets) { await UpdateStatus(data.id, serverApi, serverUrl, "draft"); } return(false); } } catch (ConnectionException e) { MessageBox.Show(e.Message); foreach (TicketData data in printData.tickets) { await UpdateStatus(data.id, serverApi, serverUrl, "draft"); } return(false); } finally { try { if (conn != null) { conn.Close(); } } catch (ConnectionException e) { MessageBox.Show(e.Message); } } }