/** * When a ZebraPrinterConnection has been established, this method * gets the printer and gathers information from the printer such as * the printer language and prints to the printer * **/ private void threadedConnect(String addressName) { try { /** * Open the connection * **/ connection.Open(); Thread.Sleep(1000); } catch (ZebraPrinterConnectionException) { updateGuiFromWorkerThread("Unable to connect with printer", Color.Red); disconnect(); } catch (Exception) { updateGuiFromWorkerThread("Error communicating with printer", Color.Red); disconnect(); } printer = null; if (connection != null && connection.IsConnected()) { /** * Get the printer instance and the printer language, then print image * **/ try { updateGuiFromWorkerThread("Getting printer...", Color.Goldenrod); Thread.Sleep(1000); printer = ZebraPrinterFactory.GetInstance(connection); updateGuiFromWorkerThread("Got Printer, getting Language...", Color.LemonChiffon); Thread.Sleep(1000); PrinterLanguage pl = printer.GetPrinterControlLanguage(); updateGuiFromWorkerThread("Printer Language " + pl.ToString(), Color.LemonChiffon); Thread.Sleep(1000); updateGuiFromWorkerThread("Formatting Image", Color.AliceBlue); printer.GetGraphicsUtil().PrintImage(image, 0, 0, 550, 412, false); updateGuiFromWorkerThread("Printing To " + addressName, Color.Green); Thread.Sleep(2000); disconnect(); } catch (ZebraPrinterConnectionException) { updateGuiFromWorkerThread("Unknown Printer Language", Color.Red); printer = null; Thread.Sleep(1000); disconnect(); } catch (ZebraPrinterLanguageUnknownException) { updateGuiFromWorkerThread("Unknown Printer Language", Color.Red); printer = null; Thread.Sleep(1000); disconnect(); } } updateButtonFromWorkerThread(true); }
private void createFile(string filePath) { ZebraPrinter printer = getPrinter(); PrinterLanguage printerLang = printer.GetPrinterControlLanguage(); if (File.Exists(filePath)) { File.Delete(filePath); } using (FileStream fileStream = File.Open(filePath, FileMode.OpenOrCreate)) { if (printerLang.Equals(PrinterLanguage.ZPL)) { Byte[] zplLabel = Encoding.Default.GetBytes("^XA^FO17,16^GB379,371,8^FS^FT65,255^A0N,135,134^FDTEST^FS^XZ"); fileStream.Write(zplLabel, 0, zplLabel.Length); } else if (printerLang.Equals(PrinterLanguage.CPCL)) { Byte[] cpclLabel = Encoding.Default.GetBytes("! 0 200 200 406 1\r\n" + "ON-FEED IGNORE\r\n" + "BOX 20 20 380 380 8\r\n" + "T 0 6 137 177 TEST\r\n" + "PRINT\r\n"); fileStream.Write(cpclLabel, 0, cpclLabel.Length); } fileStream.Close(); } }
private void getListOfAllFormats() { Invoke(new ButtonEnablingEventHandler(toggleButtonEnabled), false); updateGuiFromWorkerThread("Retrieving Formats...", Color.Gold); ZebraPrinter printer = getPrinter(); try { String[] formats; PrinterLanguage pl = printer.GetPrinterControlLanguage(); if (pl == PrinterLanguage.ZPL) { formats = new String[] { "ZPL" }; } else { formats = new String[] { "FMT", "LBL" }; } String[] formatNames = printer.GetFileUtil().RetrieveFileNames(formats); StringBuilder sb = new StringBuilder(); sb.Append("Printer Format Files:\r\n"); foreach (String formatName in formatNames) { sb.Append("\r\n" + formatName); } updateGuiFromWorkerThread("Done Retrieving Formats...", Color.Lime); Thread.Sleep(500); Invoke(new ListBoxEventHandler(displayNames), sb.ToString()); } catch (ZebraException) { updateGuiFromWorkerThread("Error Retrieving Formats", Color.Red); } Invoke(new ButtonEnablingEventHandler(toggleButtonEnabled), true); }
/** * When a ZebraPrinterConnection has been established, this method * gets the printer and gathers information from the printer such as * the printer language. ToolsUtil will then be used to default the printer. * **/ private void threadedConnect(String addressName) { try { /** * Open the connection * **/ connection.Open(); Thread.Sleep(1000); } catch (ZebraPrinterConnectionException) { updateGuiFromWorkerThread("Unable to connect with printer", Color.Red); disconnect(); } catch (Exception) { updateGuiFromWorkerThread("Error communicating with printer", Color.Red); disconnect(); } printer = null; if (connection != null && connection.IsConnected()) { /** * Get the printer instance and the printer language, then do a factory default on the printer. * **/ try { updateGuiFromWorkerThread("Getting printer...", Color.Goldenrod); Thread.Sleep(1000); printer = ZebraPrinterFactory.GetInstance(connection); updateGuiFromWorkerThread("Got Printer, getting Language...", Color.LemonChiffon); Thread.Sleep(1000); PrinterLanguage pl = printer.GetPrinterControlLanguage(); updateGuiFromWorkerThread("Printer Language " + pl.ToString(), Color.LemonChiffon); ToolsUtil printer_default = printer.GetToolsUtil(); printer_default.RestoreDefaults(); updateGuiFromWorkerThread("Restoring Defaults", Color.Aqua); Thread.Sleep(1000); disconnect(); } catch (ZebraPrinterConnectionException) { updateGuiFromWorkerThread("Unknown Printer Language", Color.Red); printer = null; Thread.Sleep(1000); disconnect(); } catch (ZebraPrinterLanguageUnknownException) { updateGuiFromWorkerThread("Unknown Printer Language", Color.Red); printer = null; Thread.Sleep(1000); disconnect(); } } }
/** * Common function used to open the connection depending on connection * type such as IP/DNS, Serial and Bluetooth(R). Sends a sample label * to print for checking connectivity and then close the connection. **/ private void threadedConnect(String addressName) { // Open the connection try { connection.Open(); Thread.Sleep(1000); } catch (ZebraPrinterConnectionException) { updateGuiFromWorkerThread("Unable to connect with printer", Color.Red); disconnect(); } catch (Exception) { updateGuiFromWorkerThread("Error communicating with printer", Color.Red); disconnect(); } printer = null; // If connection opened successfully than get the printer language and display it. // Also sends a sample lable to print and than close the connection. if (connection != null && connection.IsConnected()) { try { updateGuiFromWorkerThread("Getting printer...", Color.Goldenrod); Thread.Sleep(1000); printer = ZebraPrinterFactory.GetInstance(connection); updateGuiFromWorkerThread("Got Printer, getting Language...", Color.LemonChiffon); Thread.Sleep(1000); PrinterLanguage pl = printer.GetPrinterControlLanguage(); updateGuiFromWorkerThread("Printer Language " + pl.ToString(), Color.LemonChiffon); Thread.Sleep(1000); updateGuiFromWorkerThread("Connected to " + addressName, Color.Green); Thread.Sleep(1000); sendLabel(); Thread.Sleep(1000); disconnect(); } catch (ZebraPrinterConnectionException) { updateGuiFromWorkerThread("Unknown Printer Language", Color.Red); printer = null; Thread.Sleep(1000); disconnect(); } catch (ZebraPrinterLanguageUnknownException) { updateGuiFromWorkerThread("Unknown Printer Language", Color.Red); printer = null; Thread.Sleep(1000); disconnect(); } } }
private string CreateDemoFile(PrinterLanguage printerLanguage) { string tempFilePath = Path.Combine(Path.GetTempPath(), "TEST_ZEBRA.LBL"); using (FileStream tempFileStream = new FileStream(tempFilePath, FileMode.Create)) { byte[] testLabelBytes = GetTestLabelBytes(printerLanguage); tempFileStream.Write(testLabelBytes, 0, testLabelBytes.Length); tempFileStream.Flush(); } return(new FileInfo(tempFilePath).FullName); }
private void threadedConnect(String addressName) { try { connection.Open(); Thread.Sleep(1000); } catch (ZebraPrinterConnectionException) { updateGuiFromWorkerThread("Unable to connect with printer", Color.Red); disconnect(); } catch (Exception) { updateGuiFromWorkerThread("Error communicating with printer", Color.Red); disconnect(); } printer = null; if (connection != null && connection.IsConnected()) { try { updateGuiFromWorkerThread("Getting printer...", Color.Goldenrod); Thread.Sleep(1000); printer = ZebraPrinterFactory.GetInstance(connection); updateGuiFromWorkerThread("Got Printer, getting Language...", Color.LemonChiffon); Thread.Sleep(1000); PrinterLanguage pl = printer.GetPrinterControlLanguage(); updateGuiFromWorkerThread("Printer Language " + pl.ToString(), Color.LemonChiffon); Thread.Sleep(1000); updateGuiFromWorkerThread("Connected to " + addressName, Color.Green); Thread.Sleep(1000); //disconnect(); } catch (ZebraPrinterConnectionException) { updateGuiFromWorkerThread("Unknown Printer Language", Color.Red); printer = null; Thread.Sleep(1000); disconnect(); } catch (ZebraPrinterLanguageUnknownException) { updateGuiFromWorkerThread("Unknown Printer Language", Color.Red); printer = null; Thread.Sleep(1000); disconnect(); } } updateButtonFromWorkerThread(true); }
public async void SendZplPalletAsync(string header, string ipAddr) { Connection connection = null; try { if (!string.IsNullOrEmpty(ipAddr)) { connection = new TcpConnection(ipAddr, 9100); } else { myPrinter = ConstantManager.PrinterSetting; connection = myPrinter.GetConnection(); } await Task.Run(async() => { try { connection.Open(); PrinterLanguage printerLanguage = ZebraPrinterFactory.GetInstance(connection).PrinterControlLanguage; connection.Write(GetTestLabelBytes(printerLanguage, header)); await Task.Delay(1000); } catch (Exception e) { // Connection Exceptions and issues are caught here Device.BeginInvokeOnMainThread(async() => { await _dialogService.DisplayAlertAsync("Error", $"Error: {e.Message}", "Ok"); }); } finally { try { connection?.Close(); } catch (ConnectionException) { } } }); } catch (Exception ex) { // Connection Exceptions and issues are caught here Device.BeginInvokeOnMainThread(async() => { await _dialogService.DisplayAlertAsync("Error", "Could not connect to printer", "Ok"); }); Crashes.TrackError(ex); } }
/* * Returns the command for a test label depending on the printer control language * The test label is a box with the word "TEST" inside of it * * _________________________ * | | * | | * | TEST | * | | * | | * |_______________________| * */ private byte[] GetConfigLabel(PrinterLanguage printerLanguage) { byte[] configLabel = null; if (printerLanguage == PrinterLanguage.ZPL) { configLabel = Encoding.UTF8.GetBytes("^XA^FO17,16^GB379,371,8^FS^FT65,255^A0N,135,134^FDTEST^FS^XZ"); } else if (printerLanguage == PrinterLanguage.CPCL) { string cpclConfigLabel = "! 0 200 200 406 1\r\n" + "ON-FEED IGNORE\r\n" + "BOX 20 20 380 380 8\r\n" + "T 0 6 137 177 TEST\r\n" + "PRINT\r\n"; configLabel = Encoding.UTF8.GetBytes(cpclConfigLabel); } return(configLabel); }
private async void TestButton_Clicked(object sender, EventArgs eventArgs) { SetInputEnabled(false); Connection connection = null; try { connection = CreateConnection(); } catch (Exception e) { UpdateConnectionStatus($"Error: {e.Message}", Color.Red); } if (connection == null) { SetInputEnabled(true); return; } await Task.Run(async() => { try { await DisplayConnectionStatusAsync("Connecting...", Color.Goldenrod, 1500); connection.Open(); await DisplayConnectionStatusAsync("Connected", Color.Green, 1500); await DisplayConnectionStatusAsync("Determining printer language...", Color.Goldenrod, 1500); PrinterLanguage printerLanguage = ZebraPrinterFactory.GetInstance(connection).PrinterControlLanguage; await DisplayConnectionStatusAsync("Printer language: " + printerLanguage.ToString(), Color.Blue, 1500); UpdateConnectionStatus("Sending data...", Color.Goldenrod); connection.Write(GetTestLabelBytes(printerLanguage)); await Task.Delay(1000); } catch (Exception e) { await DisplayConnectionStatusAsync($"Error: {e.Message}", Color.Red, 3000); } finally { try { connection?.Close(); await DisplayConnectionStatusAsync("Disconnecting...", Color.Goldenrod, 1000); UpdateConnectionStatus("Not connected", Color.Red); } catch (ConnectionException) { } } }); SetInputEnabled(true); }
/* * Returns the command for a test label depending on the printer control language. * The test label is a box with the word "TEST" inside of it. * * _________________________ * | | * | | * | TEST | * | | * | | * |_______________________| * */ private byte[] GetTestLabelBytes(PrinterLanguage printerLanguage) { if (printerLanguage == PrinterLanguage.ZPL) { return(Encoding.UTF8.GetBytes(TestLabelZpl)); } else if (printerLanguage == PrinterLanguage.CPCL || printerLanguage == PrinterLanguage.LINE_PRINT) { return(Encoding.UTF8.GetBytes(TestLabelCpcl)); } else { throw new ZebraPrinterLanguageUnknownException(); } }
private byte[] getConfigLabel(PrinterLanguage printerLanguage) { String configLabel = ""; if (printerLanguage == PrinterLanguage.ZPL) { configLabel = "^XA^FO17,16^GB379,371,8^FS^FT65,255^A0N,135,134^FDTEST^FS^XZ"; } else if (printerLanguage == PrinterLanguage.CPCL) { configLabel = "! 0 200 200 406 1\r\n" + "ON-FEED IGNORE\r\n" + "BOX 20 20 380 380 8\r\n" + "T 0 6 137 177 TEST\r\n" + "PRINT\r\n"; } return(Encoding.Default.GetBytes(configLabel)); }
private string CreateDemoFile(PrinterLanguage pl) { string tempFilePath = $"{Path.GetTempPath()}TEST_ZEBRA.LBL"; using (FileStream tmpFile = new FileStream(tempFilePath, FileMode.Create)) { byte[] configLabel = null; if (pl == PrinterLanguage.ZPL) { configLabel = Encoding.UTF8.GetBytes("^XA^FO17,16^GB379,371,8^FS^FT65,255^A0N,135,134^FDTEST^FS^XZ"); } else if (pl == PrinterLanguage.CPCL) { string cpclConfigLabel = "! 0 200 200 406 1\r\n" + "ON-FEED IGNORE\r\n" + "BOX 20 20 380 380 8\r\n" + "T 0 6 137 177 TEST\r\n" + "PRINT\r\n"; configLabel = Encoding.UTF8.GetBytes(cpclConfigLabel); } tmpFile.Write(configLabel, 0, configLabel.Length); tmpFile.Flush(); } return(new FileInfo(tempFilePath).FullName); }
private void threadedConnect(String addressName) { try { connection.Open(); Thread.Sleep(1000); } catch (ZebraPrinterConnectionException) { updateGuiFromWorkerThread("Unable to connect with printer", Color.Red); disconnect(); } catch (ZebraGeneralException e) { updateGuiFromWorkerThread(e.Message, Color.Red); disconnect(); } catch (Exception) { updateGuiFromWorkerThread("Error communicating with printer", Color.Red); disconnect(); } printer = null; if (connection != null && connection.IsConnected()) { try { printer = ZebraPrinterFactory.GetInstance(connection); PrinterLanguage pl = printer.GetPrinterControlLanguage(); updateGuiFromWorkerThread("Printer Language " + pl.ToString(), Color.LemonChiffon); Thread.Sleep(1000); updateGuiFromWorkerThread("Connected to " + addressName, Color.Green); Thread.Sleep(1000); this.connectionEstablished(); } catch (ZebraPrinterConnectionException) { updateGuiFromWorkerThread("Unknown Printer Language", Color.Red); printer = null; Thread.Sleep(1000); disconnect(); } catch (ZebraPrinterLanguageUnknownException) { updateGuiFromWorkerThread("Unknown Printer Language", Color.Red); printer = null; Thread.Sleep(1000); disconnect(); } } }
public static ZebraPrinter Connect(Connection connection, PrinterLanguage language) { ZebraPrinter printer = null; try { connection.Open(); if (connection.Connected) { printer = ZebraPrinterFactory.GetInstance(language, connection); Console.WriteLine($"Printer Connected"); } else { Console.WriteLine($"Printer Not Connected!"); } } catch (ConnectionException e) { throw new ConnectionException($"Error connecting to printer: {e.Message}"); } return(printer); }
/** * Check that macAddress is valid * Connect to device using BluetoothPrinterConnection class * **/ private void doConnectBT() { if (connection != null) { doDisconnect(); } updateButtonFromWorkerThread(false); if (this.macAddress == null || this.macAddress.Length != 12) { updateGuiFromWorkerThread("Invalid MAC Address", Color.Red); disconnect(); } else { updateGuiFromWorkerThread("Connecting... Please wait...", Color.Goldenrod); try { connection = new BluetoothPrinterConnection(this.macAddress); connection.Open(); Thread.Sleep(1000); } catch (ZebraPrinterConnectionException) { updateGuiFromWorkerThread("Unable to connect with printer", Color.Red); disconnect(); } catch (ZebraException) { updateGuiFromWorkerThread("COMM Error! Disconnected", Color.Red); disconnect(); } catch (Exception) { updateGuiFromWorkerThread("Error communicating with printer", Color.Red); disconnect(); } /** * Get printer from ZebraPrinterFactory * Get printer language * Thread.Sleep allow statusBar updates to be seen before they change * **/ printer = null; if (connection != null && connection.IsConnected()) { try { updateGuiFromWorkerThread("Getting printer...", Color.Goldenrod); Thread.Sleep(1000); printer = ZebraPrinterFactory.GetInstance(connection); updateGuiFromWorkerThread("Got Printer, getting Language...", Color.LemonChiffon); Thread.Sleep(1000); PrinterLanguage pl = printer.GetPrinterControlLanguage(); updateGuiFromWorkerThread("Printer Language " + pl.ToString(), Color.LemonChiffon); Thread.Sleep(1000); updateGuiFromWorkerThread("Connected to " + macAddress, Color.Green); Thread.Sleep(1000); } catch (ZebraPrinterConnectionException) { updateGuiFromWorkerThread("Unknown Printer Language", Color.Red); printer = null; Thread.Sleep(1000); disconnect(); } catch (ZebraPrinterLanguageUnknownException) { updateGuiFromWorkerThread("Unknown Printer Language", Color.Red); printer = null; Thread.Sleep(1000); disconnect(); } updateButtonFromWorkerThread(true); } } }
/***************************************************************************************************** Desc: This function is called when Retrieve button is clicked. Gets the list of label formats stored * at printer and display that list on UI using combobox. ******************************************************************************************************/ private void getListOfAllFormats() { //Get the instance of ZebraPrinter to obtain properties of Zebra printer if (zebraPrinterConnection != null) printer = ZebraPrinterFactory.GetInstance(zebraPrinterConnection); else { MessageBox.Show("Device isn't Connected."); return; } try { if (printer != null && isConnected) { String[] formats; /** * MADE CHANGES HERE!!!!!!!**************************** * * **/ //String printerLanguage = SGD.GET("device.languages", zebraPrinterConnection); PrinterLanguage lang = printer.GetPrinterControlLanguage(); // select the format type according to printer language // if ( printerLanguage == "zpl" ) if(lang == PrinterLanguage.ZPL) { formats = new String[] { "ZPL" }; } else { formats = new String[] { "FMT", "LBL" }; } /** * END CHANGES * **/ // Retrieve the list of format files store at printer and display it in combobox String[] formatNames = printer.GetFileUtil().RetrieveFileNames(formats); cbFormatList.DataSource = formatNames; } else { MessageBox.Show("Device isn't Connected."); return; } } catch (ZebraPrinterLanguageUnknownException) { MessageBox.Show("Printer Control Language Error: \r\n Make sure that printer has supported control language."); return; } catch (ZebraPrinterConnectionException) { MessageBox.Show("Communication Error:\r\n Check Printer is on Or Connection has been established.\r\n"); return; } catch (ZebraException) { MessageBox.Show("Zebra Printer Error: Can't retrieve formats stored at printer"); return; } catch (Exception) { MessageBox.Show("Error in Retrieving Formats"); return; } }
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); } } }); }