private void commandPrint() { UsbConnection connection = null; try { DiscoveredUsbPrinter usbPrinter = null; List <DiscoveredUsbPrinter> printers = UsbDiscoverer.GetZebraUsbPrinters(new ZebraPrinterFilter()); if (printers == null || printers.Count <= 0) { MessageBox.Show("没有检测到打印机,请检查打印机是否开启!"); myEventLog.LogInfo("没有检测到打印机,请检查打印机是否开启!"); return; } usbPrinter = printers[0]; connection = new UsbConnection(usbPrinter.Address); connection.Open(); var printer = ZebraPrinterFactory.GetInstance(connection); //printer.SendCommand("~JA"); var startTime = DateTime.Now; var command = GetCommandFromDb(); Console.WriteLine($"生成打印命令花费时间:{(DateTime.Now - startTime).TotalMilliseconds}ms"); System.Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff")); startTime = DateTime.Now; for (int i = 0; i < 20; i++) { printer.SendCommand(command); Console.WriteLine($"打印内容发送成功!花费时间:{(DateTime.Now - startTime).TotalMilliseconds}ms"); Thread.Sleep(200); startTime = DateTime.Now; } //System.Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff")); //printer.SendCommand(command); //Console.WriteLine($"打印内容发送成功!花费时间:{(DateTime.Now - startTime).TotalMilliseconds}ms"); //Thread.Sleep(5000); //printer.SendCommand("~JA"); } catch (Exception e) { connection.Close(); } }
private void button1_Click(object sender, EventArgs e) { if (textBoxId.Text != "") { Value1 = textBoxId.Text; ZebraCardPrinter zebraCardPrinter = null; Connection connection = null; String usbAdress = null; try { foreach (DiscoveredPrinter usbPrinter in UsbDiscoverer.GetZebraUsbPrinters(new ZebraCardPrinterFilter())) { usbAdress = usbPrinter.Address; } connection = new UsbConnection(usbAdress); connection.Open(); zebraCardPrinter = ZebraCardPrinterFactory.GetInstance(connection); ZebraTemplate zebraCardTemplate = new ZebraCardTemplate(zebraCardPrinter); //string templateData = GetTemplateData(); List <string> templateFields = zebraCardTemplate.GetTemplateDataFields(Template); Dictionary <string, string> fieldData = PopulateTemplateFieldData(templateFields); // Generate template job TemplateJob templateJob = zebraCardTemplate.GenerateTemplateDataJob(Template, fieldData); // Send job int jobId = zebraCardPrinter.PrintTemplate(1, templateJob); // Poll job status JobStatusInfo jobStatus = PollJobStatus(jobId, zebraCardPrinter); //labelStatus.Text = "Impression OK"; //Console.WriteLine($"Job {jobId} completed with status '{jobStatus.PrintStatus}'."); } catch (Exception ev) { labelStatus.Text = "Erreur d'impression : " + ev.Message; //Console.WriteLine($"Error printing template: {ev.Message}"); } finally { CloseQuietly(connection, zebraCardPrinter); } } else { MessageBox.Show("Pas de valeur"); } }
/// <summary> /// 프린터 커넥션 활성화 /// </summary> public static bool PrinterConnectionOpen() { try { List <string> list = new List <string>(); foreach (DiscoveredUsbPrinter usbPrinter in UsbDiscoverer.GetZebraUsbPrinters(new ZebraPrinterFilter())) { list.Add(usbPrinter.ToString()); } if (list.Count == 0) { MessageBox.Show("연결된 라벨 프린터가 없습니다.", "알림", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } // 커넥션이 없으면 커넥션 만들어서 Open하고 return true if (PrinterConnection == null) { PrinterConnection = new UsbConnection(list[0]); PrinterConnection.Open(); return(true); } else { return(true); } } catch (ConnectionException ex) { MessageBox.Show("프린터 연결실패" + Environment.NewLine + ex.ToString(), "실패", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } catch (Exception ex) { MessageBox.Show("프린터 연결실패" + Environment.NewLine + ex.ToString(), "실패", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } }
public void Print(string Printer) { Connection connection = null; ZebraCardPrinter zebraCardPrinter = null; ZebraCardPrint.DLL.DatosCarnet datosCarnet = new ZebraCardPrint.DLL.DatosCarnet(); try { //connection = new TcpConnection("1.2.3.4", 9100); connection = new UsbConnection(Printer); connection.Open(); zebraCardPrinter = ZebraCardPrinterFactory.GetInstance(connection); List <GraphicsInfo> graphicsData = DrawGraphics(zebraCardPrinter, dataTable); // Set the card source //Descomentar zebraCardPrinter.SetJobSetting(ZebraCardJobSettingNames.CARD_SOURCE, "Feeder"); // Feeder=default // Set the card destination - If the destination value is not specifically set, it will be auto set to the most appropriate value if (checkBox1.CheckState == CheckState.Unchecked) { // Set the card source zebraCardPrinter.SetJobSetting(ZebraCardJobSettingNames.CARD_SOURCE, "Feeder"); // Feeder=default // Set the card destination - If the destination value is not specifically set, it will be auto set to the most appropriate value if (zebraCardPrinter.HasLaminator()) { zebraCardPrinter.SetJobSetting(ZebraCardJobSettingNames.CARD_DESTINATION, "LaminatorAny"); } else { zebraCardPrinter.SetJobSetting(ZebraCardJobSettingNames.CARD_DESTINATION, "Eject"); } // Send job int jobId = zebraCardPrinter.Print(1, graphicsData); // Poll job status JobStatusInfo jobStatus = PollJobStatus(jobId, zebraCardPrinter); MessageBox.Show($"Impresion Id: {jobId} completada con estado: '{jobStatus.PrintStatus}'."); if (jobStatus.PrintStatus.ToString().ToUpper() == "DONE_OK") { datosCarnet.SDInsertaImpresionCarnet(txtNumeroDeEmpleado.Text); rdoAmbasCaras.Checked = true; } } } catch (Exception e) { MessageBox.Show($"Error printing image: {e.Message}"); } finally { CloseQuietly(connection, zebraCardPrinter); } }