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 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 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); } } }
private async void TestButton_Click(object sender, RoutedEventArgs e) { SetTestButtonState(false); Connection printerConnection = null; if (connectionSelector.ConnectionType == ConnectionType.Network) { try { printerConnection = connectionSelector.GetConnection(); } catch (ConnectionException) { UpdateStatusBarOnGui("Invalid Address and/or Port", ConnectionState.Error); await Task.Delay(1000); UpdateStatusBarOnGui("Not Connected", ConnectionState.Error); return; } } else if (connectionSelector.ConnectionType == ConnectionType.Bluetooth) { try { printerConnection = connectionSelector.GetConnection(); } catch (ConnectionException) { UpdateStatusBarOnGui("Invalid Mac Address", ConnectionState.Error); await Task.Delay(1000); UpdateStatusBarOnGui("Not Connected", ConnectionState.Error); } } else if (connectionSelector.SelectedUsbPrinter is DiscoveredPrinterDriver printer) { try { printerConnection = new DriverPrinterConnection(printer.PrinterName); } catch (ConnectionException) { return; } } else if (connectionSelector.ConnectionType == ConnectionType.UsbDirect) { try { printerConnection = connectionSelector.GetConnection(); } catch (ConnectionException) { UpdateStatusBarOnGui("Invalid Address", ConnectionState.Error); await Task.Delay(1000); UpdateStatusBarOnGui("Not Connected", ConnectionState.Error); return; } } else { return; } await Task.Run(async() => { try { UpdateStatusBarOnGui("Connecting...", ConnectionState.Progress); await Task.Delay(1500); printerConnection.Open(); UpdateStatusBarOnGui("Connected", ConnectionState.Success); await Task.Delay(1500); UpdateStatusBarOnGui("Determining Printer Language...", ConnectionState.Progress); await Task.Delay(1500); PrinterLanguage printerLanguage = ZebraPrinterFactory.GetInstance(printerConnection).PrinterControlLanguage; UpdateStatusBarOnGui("Printer Language " + printerLanguage.ToString(), ConnectionState.Info); await Task.Delay(1500); UpdateStatusBarOnGui("Sending Data...", ConnectionState.Progress); printerConnection.Write(GetConfigLabel(printerLanguage)); } catch (ConnectionException) { UpdateStatusBarOnGui("Communications Error", ConnectionState.Error); } catch (ZebraPrinterLanguageUnknownException) { UpdateStatusBarOnGui("Invalid Printer Language", ConnectionState.Error); } finally { try { await Task.Delay(1000); UpdateStatusBarOnGui("Disconnecting...", ConnectionState.Progress); if (printerConnection != null) { printerConnection.Close(); } await Task.Delay(1000); UpdateStatusBarOnGui("Not Connected", ConnectionState.Error); } catch (ConnectionException) { } finally { SetTestButtonState(true); } } }); }