/** * 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); }
/** * Called when the frame has been closed. Closes the open connection to * a printer if one exists. * **/ public void onFrameClose(Object sender, CancelEventArgs args) { if (connection != null && connection.IsConnected()) { connection.Close(); connection = null; } }
/** * 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(); } } }
/** * When discoverButton is clicked, start new thread to discover nearby bluetooth * devices * **/ private void discoverButtonClicked(object sender, EventArgs e) { if (connection != null && connection.IsConnected()) { connection.Close(); } updateGuiFromWorkerThread("Discovery in progress...", Color.HotPink); discoveredPrintersListBox.Items.Clear(); Thread t = new Thread(doBluetoothDiscovery); t.Start(); }
private void statusUpdateThread() { try { ZebraPrinterConnection connection = getConnection(); ZebraPrinter printer = ZebraPrinterFactory.GetInstance(connection); String statusMessages; while (getConnection() != null && connection.IsConnected()) { PrinterStatus printerStatus = printer.GetCurrentStatus(); PrinterStatusMessages messages = new PrinterStatusMessages(printerStatus); String[] messagesArray = messages.GetStatusMessage(); statusMessages = "Ready to Print: " + Convert.ToString(printerStatus.IsReadyToPrint); statusMessages += "\r\nLabels in Batch: " + Convert.ToString(printerStatus.LabelsRemainingInBatch); statusMessages += "\r\nLabels in Buffer: " + Convert.ToString(printerStatus.NumberOfFormatsInReceiveBuffer) + "\r\n\r\n"; foreach (String message in messagesArray) { statusMessages += message + "\r\n"; } Invoke(new statusEventHandler(updateStatusMessage), statusMessages); Thread.Sleep(4000); } } catch (ZebraException) { disconnected(); updateGuiFromWorkerThread("COMM Error! Disconnected", Color.Red); } }
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 { 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); } 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; } }
/**************************************************************************************************** * 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; } }
public string getAnswerFromPrinter() { if (m_connection != null && m_connection.IsConnected()) { string result = null; for (int i = 0; i < 5; i++) { result = SGD.GET("file.dir", m_connection); Thread.Sleep(500); if (result != null && result.Length != 0) { break; } } return(result); } return(null); }
public void connectWithOptions(IReadOnlyDictionary <string, string> options, IMethodResult oResult) { Logger.Write("connectWithOptions call"); Logger.Write("options:", options); string valueObj = null; Int32 connectionTimeout = 0; if (m_connection != null && m_connection.IsConnected() && m_printer != null) { m_connection.Close(); Thread.Sleep(1000); m_connection = null; m_printer = null; } if (options.ContainsKey(ZebraConstants.HK_TIMEOUT)) { valueObj = options[ZebraConstants.HK_TIMEOUT]; if ((valueObj != null) && (valueObj is String)) { try { connectionTimeout = Int32.Parse(valueObj.ToString()); } catch (System.FormatException) { m_maxTimeoutForRead = 0; } } } if (options.ContainsKey(ZebraConstants.PROPERTY_MAX_TIMEOUT_FOR_READ)) { valueObj = options[ZebraConstants.PROPERTY_MAX_TIMEOUT_FOR_READ]; if ((valueObj != null) && (valueObj is String)) { try { m_maxTimeoutForRead = Int32.Parse(valueObj.ToString()); } catch (System.FormatException) { m_maxTimeoutForRead = 0; } } } if (options.ContainsKey(ZebraConstants.PROPERTY_TIME_TO_WAIT_FOR_MORE_DATA)) { valueObj = options[ZebraConstants.PROPERTY_TIME_TO_WAIT_FOR_MORE_DATA]; if ((valueObj != null) && (valueObj is String)) { try { m_timeToWaitForMoreData = Int32.Parse(valueObj.ToString()); } catch (System.FormatException) { m_maxTimeoutForRead = 0; } } } ConnecttionJob job = new ConnecttionJob(); job.Address = ID; job.Port = Port; job.ConnectionType = connectionType; job.MaxTimeoutForRead = m_maxTimeoutForRead; job.TimeToWaitForMoreData = m_timeToWaitForMoreData; if (connectionTimeout == 0) { job.Connect(); } else { if (!job.Connect(connectionTimeout)) { oResult.set(ZebraConstants.PRINTER_STATUS_ERR_TIMEOUT); return; } } if (job.Connection != null && job.Printer != null) { m_connection = job.Connection; m_printer = job.Printer; oResult.set(ZebraConstants.PRINTER_STATUS_SUCCESS); return; } oResult.set(ZebraConstants.PRINTER_STATUS_ERR_NOT_CONNECTED); }
/** * 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); } } }