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"); } }
/// \endcond private void StartWatcher() { //deviceWatcher = DeviceInformation.CreateWatcher(UsbDevice.GetDeviceSelector(0x04d8, 0xf426)); deviceWatcher = DeviceInformation.CreateWatcher( UsbDevice.GetDeviceSelector(TreehopperUsb.Settings.Vid, TreehopperUsb.Settings.Pid)); // Hook up handlers for the watcher events before starting the watcher handlerAdded = async(watcher, deviceInfo) => { Debug.WriteLine("Device added: " + deviceInfo.Name); var newConnection = new UsbConnection(deviceInfo); var newBoard = new TreehopperUsb(newConnection); await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { Boards.Add(newBoard); }); }; deviceWatcher.Added += handlerAdded; handlerUpdated = (watcher, deviceInfoUpdate) => { Debug.WriteLine("Device updated"); }; deviceWatcher.Updated += handlerUpdated; handlerRemoved = async(watcher, deviceInfoUpdate) => { Debug.WriteLine("Device removed"); await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { Boards.Where(board => ((UsbConnection)board.Connection).DevicePath == deviceInfoUpdate.Id) .ToList() .All(i => { i.Disconnect(); Boards.Remove(i); return(true); } ); }); }; deviceWatcher.Removed += handlerRemoved; handlerEnumCompleted = (watcher, obj) => { Debug.WriteLine("Enum completed"); }; deviceWatcher.EnumerationCompleted += handlerEnumCompleted; handlerStopped = (watcher, obj) => { Debug.WriteLine("Device or something stopped"); }; deviceWatcher.Stopped += handlerStopped; Debug.WriteLine("Starting the wutchah"); deviceWatcher.Start(); }
public FormAddAtt(Attenuator att) { InitializeComponent(); buttonAdd.Text = "Apply"; panelHttp.Visible = false; panelUsb.Visible = false; panelSerial.Visible = false; textBoxDescription.Text = att.description; comboBoxAttType.Text = att.GetType().Name.Equals("Rudat") ? "RUDAT" : "RCDAT"; comboBoxConType.Text = att.conType.ToString().ToUpper(); switch (att.conType) { case ConnectionTypes.Http: case ConnectionTypes.Telnet: panelHttp.Visible = true; textBoxHttpIp.Text = att.ipAddress; textBoxHttpPort.Text = att.port; break; case ConnectionTypes.Serial: foreach (string id in SerialPort.GetPortNames()) { comboBoxComPort.Items.Add(id); } comboBoxComPort.Text = att.comPortName; textBoxBaudRate.Text = att.baudRate.ToString(); comboBoxParity.Text = att.parity.ToString(); comboBoxDataBits.Text = att.dataBits.ToString(); comboBoxStopBits.Text = att.stopBits.ToString(); panelSerial.Visible = true; break; case ConnectionTypes.Usb: panelUsb.Visible = true; foreach (string id in UsbConnection.GetAvailableSNList()) { comboBoxDeviceId.Items.Add(id); } comboBoxDeviceId.Text = att.serialNumber; break; default: break; } }
private void comboBoxConType_SelectedIndexChanged(object sender, EventArgs e) { panelHttp.Visible = false; panelUsb.Visible = false; panelSerial.Visible = false; switch (comboBoxConType.SelectedItem.ToString()) { case "HTTP": panelHttp.Visible = true; labelPort.Text = "Port (optional)"; textBoxHttpPort.Text = "80"; break; case "TELNET": panelHttp.Visible = true; labelPort.Text = "Port"; textBoxHttpPort.Text = "23"; break; case "USB": foreach (string id in UsbConnection.GetAvailableSNList()) { comboBoxDeviceId.Items.Add(id); } panelUsb.Visible = true; break; case "SERIAL": foreach (string id in SerialPort.GetPortNames()) { comboBoxComPort.Items.Add(id); } textBoxBaudRate.Text = "9600"; comboBoxParity.Text = "None"; comboBoxDataBits.Text = "8"; comboBoxStopBits.Text = "1"; panelSerial.Visible = true; break; default: break; } }
/// <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); } }