public void Close() { // Make sure the data got to the printer before closing the connection Thread.Sleep(100); // Close the connection to release resources. ThePrinterConn.Close(); ThePrinterConn = null; }
/** * 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(); }
/***************************************************************************************************** * Desc: Called when Disconnect button is clicked. Close the Bluetooth connection with printer. ******************************************************************************************************/ private void disconnectButton_Click(object sender, EventArgs e) { try { // Close the connection if (zebraPrinterConnection != null) { zebraPrinterConnection.Close(); } } catch (ZebraPrinterConnectionException) { MessageBox.Show("Unable to disconnect with printer."); return; } catch (ZebraException) { MessageBox.Show("Unable to disconnect with printer.\r\nCheck that printer is on."); return; } catch (Exception) { MessageBox.Show("Disconnect Failed."); return; } zebraPrinterConnection = null; statusLabel.BackColor = Color.Red; statusLabel.Text = "Status : Disconnected."; }
/**************************************************************************************************** * Description: Closes the bluetooth connection with the Zebra printer. *****************************************************************************************************/ private void btDisconnect_Click(object sender, EventArgs e) { if (thePrinterConn != null) { statusString = "Not Connected"; bluetoothListResponse.Items.Add(statusString); thePrinterConn.Close(); } thePrinterConn = null; }
/***************************************************************************************************** Desc: Called when Disconnect button is clicked.Close the Bluetooth connection with printer. ******************************************************************************************************/ private void disconnectButton_Click(object sender, EventArgs e) { try { // Close the connection if (zebraPrinterConnection != null) { zebraPrinterConnection.Close(); } } catch (ZebraPrinterConnectionException) { MessageBox.Show("Unable to disconnect with printer."); return; } catch (ZebraException) { MessageBox.Show("Unable to disconnect with printer.\r\nCheck that printer is on."); return; } catch (Exception) { MessageBox.Show("Disconnect Failed."); return; } zebraPrinterConnection = null; RemoveVariableFields(); statusLabel.BackColor = Color.Red; statusLabel.Text = "Status : Disconnected."; isConnected = false; macAddrBox.Text = ""; cbFormatList.DataSource = null; quantityLabel.Location = new Point(5, 5); quantity.Location = new Point(110,5); Retrieve.Enabled = false; }
private void doDisconnect() { try { if (connection != null && connection.IsConnected()) { updateGuiFromWorkerThread("Disconnecting", Color.Honeydew); connection.Close(); } } catch (ZebraException) { updateGuiFromWorkerThread("COMM Error! Disconnected", Color.Red); } Thread.Sleep(1000); updateGuiFromWorkerThread("Not Connected", Color.Red); connection = null; updateButtonFromWorkerThread(true); }
/**************************************************************************************************** * 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; } }
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); }
/** * Called when the frame has been closed. Closes the open connection to * a printer if one exists. * **/ private void OnClose(object sender, EventArgs e) { connection.Close(); }