/** * 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); }
public bool Print() { bool error = true; try { // Open the connection - physical connection is established here. ThePrinterConn.Open(); printer = ZebraPrinterFactory.GetInstance(ThePrinterConn); PrinterStatus printerStatus = printer.GetCurrentStatus(); bool ready = printerStatus.IsReadyToPrint; if (ready == false) { return(error); } //stvaranje oblika za slanje na printer String output = "! 0 200 200" + " " + (lastYPos + 100).ToString() + " " + "1\r\n"; //output += "IN-MILLIMETERS\r\n"; foreach (String s in buffer) { output += s + "\r\n"; } output += "PRINT\r\n"; // Send the data to the printer as a byte array. ThePrinterConn.Write(Encoding.UTF8.GetBytes(output)); //thePrinterConn.Close(); error = false; } catch (Exception e) { Logger.Logger.Log(e); error = true; } finally { //brisi buffer buffer.Clear(); //na kraju, baci gresku ako nije izvrseno do kraja... } return(error); }
/** * 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(); } } }
/**************************************************************************************************** Desc: Called when Connect button is clicked. Open a bluetooth connection with printer using MAC address *****************************************************************************************************/ private void connectButton_Click(object sender, EventArgs e) { // Validate bluetooth MAC address if (macAddrBox.Text.Length != 12) { MessageBox.Show("Enter valid MAC address!"); return; } // Open the connetion for further communication if (zebraPrinterConnection == null && macAddrBox.Text.Length == 12) { zebraPrinterConnection = new BluetoothPrinterConnection(macAddrBox.Text); statusLabel.BackColor = Color.Gold; statusLabel.Text = "Status : Connecting to..." + macAddrBox.Text; Application.DoEvents(); try { zebraPrinterConnection.Open(); while (!zebraPrinterConnection.IsConnected()) { } } catch (ZebraPrinterConnectionException) { statusLabel.BackColor = Color.Red; statusLabel.Text = "Status : Disconnected."; zebraPrinterConnection = null; isConnected = false; MessageBox.Show("Unable to connect with printer."); return; } catch (ZebraException) { statusLabel.BackColor = Color.Red; statusLabel.Text = "Status : Disconnected."; zebraPrinterConnection = null; isConnected = false; MessageBox.Show("Unable to connect with printer.\r\nCheck that printer is on."); return; } catch (Exception) { statusLabel.BackColor = Color.Red; statusLabel.Text = "Status : Disconnected."; zebraPrinterConnection = null; isConnected = false; MessageBox.Show("Connection Failed."); return; } statusLabel.BackColor = Color.Green; statusLabel.Text = "Status : Connected." + macAddrBox.Text; isConnected = true; Retrieve.Enabled = true; } }
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); }
/**************************************************************************************************** * Desc: Called when Connect button is clicked. Open a bluetooth connection with printer using MAC address *****************************************************************************************************/ private void connectButton_Click(object sender, EventArgs e) { //Check validity of mac address if (macAddrBox.Text.Length != 12) { MessageBox.Show("Enter valid MAC address!"); return; } //Creating a bluetooth connection with zebra printer if (zebraPrinterConnection == null && macAddrBox.Text.Length == 12) { zebraPrinterConnection = new BluetoothPrinterConnection(macAddrBox.Text); statusLabel.Text = "Status: Connecting to..." + macAddrBox.Text; statusLabel.BackColor = System.Drawing.Color.Gold; Application.DoEvents(); try { zebraPrinterConnection.Open(); while (!zebraPrinterConnection.IsConnected()) { } } catch (ZebraPrinterConnectionException) { statusLabel.Text = "Status: Disconnected."; statusLabel.ForeColor = System.Drawing.Color.Red; zebraPrinterConnection = null; MessageBox.Show("Unable to connect with printer."); return; } catch (ZebraException) { statusLabel.BackColor = Color.Red; statusLabel.Text = "Status : Disconnected."; zebraPrinterConnection = null; MessageBox.Show("Unable to connect with printer.\r\nCheck that printer is on."); return; } catch (Exception) { statusLabel.BackColor = Color.Red; statusLabel.Text = "Status : Disconnected."; zebraPrinterConnection = null; MessageBox.Show("Connection Failed."); return; } statusLabel.Text = "Status: Connected to " + macAddrBox.Text; statusLabel.BackColor = System.Drawing.Color.Green; } }
/**************************************************************************************************** * Description: Opens a bluetooth connection with the Zebra printer. *****************************************************************************************************/ private void btConnect_Click(object sender, EventArgs e) { if (btAddress.Text.Length < 12) { MessageBox.Show("Please Enter valid MAC address"); } else { try { // Instantiate a connection for given Bluetooth(R) MAC Address. thePrinterConn = new BluetoothPrinterConnection(btAddress.Text); // Open the connection - physical connection is established here. thePrinterConn.Open(); statusString = "Connected"; bluetoothListResponse.Items.Add(statusString); } catch (ZebraPrinterConnectionException) { // Handle communications error here. MessageBox.Show("Unable to connect with printer."); return; } catch (ZebraException) { MessageBox.Show("Unable to connect with printer.\r\nCheck that printer is on or it supports Bluetooth."); return; } catch (Exception) { MessageBox.Show("Exception Thrown"); return; } } }
/**************************************************************************************************** * Description: Opens a usb connection with the printer. Sends selected zpl command to the printer and reads * response. *****************************************************************************************************/ private void usbBtnSend_Click(object sender, EventArgs e) { String cmdSend = null; String readResponse = null; int nBytes = 0; if (usbSndList.SelectedIndex >= 0) { try { // Instantiate connection for ZPL USB port with a given port name. thePrinterConn = new UsbPrinterConnection("LPT1:"); // "LPT" is the default prefix for most Windows CE/Mobile USB ports // Open the connection - physical connection is established here. thePrinterConn.Open(); // This example prints "This is a ZPL test." near the top of the label. cmdSend = usbSndList.SelectedItem.ToString() + "\r\n"; // Send the data to printer as a byte array. thePrinterConn.Write(Encoding.Default.GetBytes(cmdSend)); statusString = "Command Sent: " + cmdSend.Trim(); usbListResponse.Items.Add(statusString); Thread.Sleep(500); nBytes = thePrinterConn.BytesAvailable(); byte[] bData = new byte[nBytes]; bData = thePrinterConn.Read(); readResponse = System.Text.ASCIIEncoding.ASCII.GetString(bData, 0, bData.Length); statusString = "Received Data: " + readResponse; // If response is longer than devide the received byte stream into multiple // streams of 46 characters to diplay it properly in listbox. if (statusString.Length < 46) { usbListResponse.Items.Add(statusString); } else { String tempStatusString = statusString; while (tempStatusString.Length > 0) { if ((tempStatusString.Length - 46) > 0) { usbListResponse.Items.Add(tempStatusString.Substring(0, 46)); tempStatusString = tempStatusString.Remove(0, 46); } else { usbListResponse.Items.Add(tempStatusString.Substring(0, tempStatusString.Length)); tempStatusString = tempStatusString.Remove(0, tempStatusString.Length); } } } usbListResponse.Refresh(); // Close the connection to release resources. thePrinterConn.Close(); thePrinterConn = null; } catch (ZebraPrinterConnectionException) { // Handle communications error here. MessageBox.Show("Unable to connect with printer."); return; } catch (ZebraException) { MessageBox.Show("Unable to connect with printer.\r\nCheck that printer is on."); return; } catch (Exception) { MessageBox.Show("Exception Thrown"); return; } } else { MessageBox.Show("Please select a command to be Sent..."); return; } }
/** * 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); } } }