/****************************************************************************************************
         * 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...");
            }
        }
        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);
        }
Example #3
0
 /**
  * Once connected, automatically generates a small test command and send
  * it to the printer for printing to demonstrate that a connection exists.
  * */
 public void sendLabel()
 {
     // Get the bytes data and send it to printer.
     if (printer != null)
     {
         byte[] configLabel = getConfigLabel(printer.GetPrinterControlLanguage());
         connection.Write(configLabel);
         updateGuiFromWorkerThread("Sending Data", Color.CornflowerBlue);
         Thread.Sleep(2000);
     }
     disconnect();
 }
Example #4
0
            public void printRawString(string command, IReadOnlyDictionary <string, string> options, IMethodResult oResult)
            {
                Logger.Write("printRawString call: " + command);
                Logger.Write("command: " + command);
                Logger.Write("options:", options);

                if (m_connection != null && m_printer != null)
                {
                    try
                    {
                        byte[] commandStr = Encoding.UTF8.GetBytes(command);
                        m_connection.Write(commandStr);

                        Thread.Sleep(500);

                        byte[] printerResponse    = m_connection.Read();
                        string printerResponseStr = Encoding.UTF8.GetString(printerResponse, 0, printerResponse.Length);

                        IReadOnlyDictionary <string, string> response = new IReadOnlyDictionary <string, string>();

                        response.Add(ZebraConstants.HK_STATUS, ZebraConstants.PRINTER_STATUS_SUCCESS);
                        response.Add(ZebraConstants.HK_STRING_RESPONCE, printerResponseStr);

                        oResult.set(response);
                    }
                    catch (Exception ex)
                    {
                        IReadOnlyDictionary <string, string> errResponse = new IReadOnlyDictionary <string, string>();

                        errResponse.Add(ZebraConstants.HK_STATUS, ZebraConstants.PRINTER_STATUS_ERROR);
                        errResponse.Add(ZebraConstants.HK_MESSAGE, ex.Message.ToString());

                        oResult.set(errResponse);
                    }
                }
                else
                {
                    oResult.set(ZebraConstants.PRINTER_STATUS_ERR_NOT_CONNECTED);
                }
            }
        /****************************************************************************************************
         * 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;
            }
        }