Example #1
0
 /*****************************************************************************************************
  * 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.";
 }
Example #2
0
        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);
            }
        }
 /**
  * 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;
     }
 }
        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;
        }
Example #5
0
       /****************************************************************************************************
       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;
                
            }

        }
 /****************************************************************************************************
  * 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;
 }
Example #7
0
 private void doConnectTcp()
 {
     updateGuiFromWorkerThread("Connecting... Please wait...", Color.Goldenrod);
     try {
         connection = new TcpPrinterConnection(this.ipAddress, this.portNumber);
         threadedConnect(this.ipAddress);
     } catch (ZebraException) {
         updateGuiFromWorkerThread("COMM Error! Disconnected", Color.Red);
         connectionClosed();
     }
 }
Example #8
0
 private void doConnectUsb()
 {
     updateGuiFromWorkerThread("Connecting... Please wait...", Color.Goldenrod);
     try {
         connection = new UsbPrinterConnection(this.usbPort);
         threadedConnect(this.usbPort);
     } catch (ZebraException) {
         updateGuiFromWorkerThread("COMM Error! Disconnected", Color.Red);
         connectionClosed();
     }
 }
Example #9
0
 private void doConnectSerial()
 {
     updateGuiFromWorkerThread("Connecting... Please wait...", Color.Goldenrod);
     try {
         connection = new SerialPrinterConnection(this.portName, this.baudRate, this.dataBits, this.parity, this.stopBits, this.handshake);
         threadedConnect(this.portName);
     } catch (ZebraException) {
         updateGuiFromWorkerThread("COMM Error! Disconnected", Color.Red);
         connectionClosed();
     }
 }
Example #10
0
 public void Reconnect()
 {
     if (ThePrinterConn != null)
     {
         ThePrinterConn.Close();
     }
     //thePrinterConn = new SerialPrinterConnection(comPort, 19200, 8, Parity.None, StopBits.One, Handshake.None);
     ThePrinterConn = new BluetoothPrinterConnection("0022583bc091");
     clearBuffer();
     lastYPos = 0;
 }
Example #11
0
        public Printer()
        {
            // com port

            // Conect to the printer using Bluetooth to serial port (in PDA) and serial connection in SDK
            ThePrinterConn = ThePrinterConn = new BluetoothPrinterConnection("0022583bc091", 10000, 1000);


            //ostale inicijalizacije...
            buffer   = new List <String>();
            lastYPos = 0;
        }
Example #12
0
        /****************************************************************************************************
         * 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;
            }
        }
        /**
         * Check that macAddress is valid
         * Connect to device using BluetoothPrinterConnection class
         * **/

        private void doConnectBT()
        {
            if (connection != null)
            {
                Thread t = disconnect();
                t.Join();
            }
            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
                {
                    try
                    {
                        connection = new BluetoothPrinterConnection(this.macAddress);
                        threadedConnect(this.macAddress);
                    }
                    catch (ZebraException)
                    {
                        updateGuiFromWorkerThread("COMM Error! Disconnected", Color.Red);
                        Thread.Sleep(1000);
                        disconnect();
                    }
                }
                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();
                }
                updateButtonFromWorkerThread(true);
            }
            updateButtonFromWorkerThread(true);
        }
Example #14
0
 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);
     connectionClosed();
     updateGuiFromWorkerThread("Not Connected", Color.Red);
     connection = null;
 }
Example #15
0
            public void disconnect(IMethodResult oResult)
            {
                Logger.Write("disconnect call");

                if (m_connection != null && m_connection.IsConnected())
                {
                    Thread.Sleep(1000);
                    m_connection.Close();
                    Thread.Sleep(1000);
                }

                m_connection = null;
                m_printer    = null;

                oResult.set(ZebraConstants.PRINTER_STATUS_SUCCESS);
            }
        private void GetListOfFormats()
        {
            ZebraPrinterConnection connection = getConnection();
            ZebraPrinter           printer    = ZebraPrinterFactory.GetInstance(connection);

            String[] zplFormatExtensions  = new String[] { "ZPL" };
            String[] cpclFormatExtensions = new String[] { "FMT", "LBL" };
            String[] extensions           = printer.GetPrinterControlLanguage() == PrinterLanguage.ZPL ? zplFormatExtensions : cpclFormatExtensions;

            List <String> formatList = new List <String>();

            foreach (String format in printer.GetFileUtil().RetrieveFileNames(extensions))
            {
                formatList.Add(format);
            }
            Invoke(new FormatListEventHandler(DisplayNames), new object[] { formatList });
        }
        /**
         * Check that macAddress is valid
         * Connect to device using BluetoothPrinterConnection class
         * **/

        private void doConnectBT()
        {
            /**
             * If a connection already exists, disconnect it
             * **/
            if (connection != null)
            {
                Thread t = disconnect();
                t.Join();
            }
            if (this.macAddress == null || this.macAddress.Length != 12)
            {
                updateGuiFromWorkerThread("Invalid MAC Address", Color.Red);
                disconnect();
            }
            else
            {
                /**
                 * Attempt to connect to printer via Bluetooth
                 * **/
                updateGuiFromWorkerThread("Connecting... Please wait...", Color.Goldenrod);
                try
                {
                    connection = new BluetoothPrinterConnection(this.macAddress);
                    threadedConnect(this.macAddress);
                }
                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();
                }
            }
        }
Example #18
0
 private void doConnectBT()
 {
     if (this.macAddress.Length != 12)
     {
         updateGuiFromWorkerThread("Invalid MAC Address", Color.Red);
         disconnect();
     }
     else
     {
         updateGuiFromWorkerThread("Connecting... Please wait...", Color.Goldenrod);
         try {
             connection = new BluetoothPrinterConnection(this.macAddress);
             threadedConnect(this.macAddress);
         } catch (ZebraException) {
             updateGuiFromWorkerThread("COMM Error! Disconnected", Color.Red);
             connectionClosed();
         }
     }
 }
Example #19
0
        public PrinterStatus getPrinterStatus(rho.PrinterZebraImpl.PrinterZebra.EPrinterConnectionType connType, string address, Int32 port)
        {
            Logger.Write("MainWindow.getPrinterStatus call, address=" + address + " port=" + port.ToString());

            PrinterStatus          status     = null;
            ZebraPrinterConnection connection = null;
            ZebraPrinter           printer    = null;

            if (connType == rho.PrinterZebraImpl.PrinterZebra.EPrinterConnectionType.eTCP)
            {
                connection = new TcpPrinterConnection(address, port);
            }
            else if (connType == rho.PrinterZebraImpl.PrinterZebra.EPrinterConnectionType.eBluetooth)
            {
                connection = new BluetoothPrinterConnection(address);
            }
            else if (connType == rho.PrinterZebraImpl.PrinterZebra.EPrinterConnectionType.eUSB)
            {
                connection = new UsbPrinterConnection(address);
            }
            else
            {
                Logger.Write("MainWindow.getPrinterStatus undifined connection type");
                return(null);
            }

            connection.Open();

            if (connection.IsConnected())
            {
                printer = ZebraPrinterFactory.GetInstance(PrinterLanguage.ZPL, connection);

                status = printer.GetCurrentStatus();
            }

            connection.Close();

            Logger.Write("MainWindow.getPrinterStatus call finished");

            return(status);
        }
 /**
  * If a connection already exists, disconnect it
  * Get new ZebreaPrinterConnection
  * **/
 private void doConnectTcp()
 {
     updateButtonFromWorkerThread(false);
     if (connection != null)
     {
         doDisconnect();
     }
     updateButtonFromWorkerThread(false);
     updateGuiFromWorkerThread("Connecting... Please wait...", Color.Goldenrod);
     try
     {
         connection = new TcpPrinterConnection(this.ipAddress, this.portNumber);
         threadedConnect(this.ipAddress);
     }
     catch (ZebraException)
     {
         updateGuiFromWorkerThread("COMM Error! Disconnected", Color.Red);
         Thread.Sleep(1000);
         disconnect();
     }
 }
        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);
            Thread.Sleep(1000);

            connection = null;
            Invoke(new EventHandler(enablebutton));
        }
Example #22
0
 /*****************************************************************************************************
 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;
     
 }
        /**
         * Close connection to printer
         * **/
        private void doDisconnect()
        {
            updateButtonFromWorkerThread(false);
            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);
            Thread.Sleep(1000);
            updateButtonFromWorkerThread(true);
            updateGuiFromWorkerThread("Ready", Color.Yellow);
            connection = null;
            updateButtonFromWorkerThread(true);
        }
        /****************************************************************************************************
         * 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;
                }
            }
        }
Example #25
0
 public RetriveParser(ZebraPrinterConnection connection)
 {
     m_connection = connection;
 }
Example #26
0
 public RetriveParser(ZebraPrinterConnection connection)
 {
     m_connection = connection;
 }
Example #27
0
            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);
            }
Example #28
0
        /**
         * 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);
                }
            }
        }
Example #29
0
        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);
        }
Example #30
0
        public void disconnect(IMethodResult oResult)
        {
            Logger.Write("disconnect call");

            if (m_connection != null && m_connection.IsConnected())
            {
                Thread.Sleep(1000);
                m_connection.Close();
                Thread.Sleep(1000);
            }

            m_connection = null;
            m_printer = null;

            oResult.set(ZebraConstants.PRINTER_STATUS_SUCCESS);
        }
        /****************************************************************************************************
         * 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;
            }
        }