/**************************************************************************************************** * Description: Sends a selected ZPL command to the Zebra printer over bluetooth and reads response. *****************************************************************************************************/ private void bluetoothBtnSend_Click(object sender, EventArgs e) { String cmdSend = null; String readResponse = null; int nBytes = 0; if (thePrinterConn != null) { if (bluetoothSndList.SelectedIndex >= 0) { cmdSend = bluetoothSndList.SelectedItem.ToString(); thePrinterConn.Write(Encoding.Default.GetBytes(cmdSend)); statusString = "Command Sent: " + cmdSend.Trim(); bluetoothListResponse.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; bluetoothListResponse.Items.Add(statusString); } else { MessageBox.Show("Select command to be sent."); } } else { MessageBox.Show("The printer isn't connected. Please connect the printer before sending commnd..."); } }
/**************************************************************************************************** * 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; } }