Ready to Print? Zebra QLN 420 C# and Java Code?

Y Yanis Dalabiras 3 years 7 months ago
120 2 0

Hi all
 
Anyone have a piece of code for check the status of the printer?
i want check via ip, actually the ip of the printer is: 172.16.11.207
in C# and Java, i have this code:
 

public String getStatus (String Printerchosen)
 
//Printerchosen = Printer Driver Name
      {
            try{
            com.zebra.sdk.comm.Connection myconnection = new com.zebra.sdk.comm.DriverPrinterConnection(Printerchosen,1000,1000);
            myconnection.open();
            com.zebra.sdk.printer.ZebraPrinter myprinter =ZebraPrinterFactory.getInstance(myconnection);
    
            com.zebra.sdk.printer.PrinterStatus printStatus = myprinter.getCurrentStatus();
            myconnection.close();
            if (printStatus.isReadyToPrint)
            {
                  return"READY TO PRINT";
            }
            else
            {   
                  return"Not Ready!!";
            }
          
          
            }
            catch (Exception f)
            {
                  f.printStackTrace();
          
                  return"Problem found";
            }

But isnt for a ip printer.

Thanks
          
 
    
      }

Please Register or Login to post a reply

2 Replies

B Bob LobLob

...

Y Yanis Dalabiras

Hi Miguel,
Try this code for a TCP connection to get the status of a printer.  It's not pretty, but it will work for nearly any Zebra ZPL printer with an IP address.  This will return a list of strings with the status of the printer. The printer is possible of giving multiple status conditions at the same time.  The same process should work for Java as well.

    public static class GetStatus
    {
        public static List Run(IPAddress ipAddr)
        {
            // create tcp client and connect server on port 9100
            TcpClient client = new TcpClient();
            List statuses = new List();

            try
            {
                client.Connect(ipAddr, 9100);
            }
            catch (SocketException se)
            {
                statuses.Add("Unable to connect to printer");
                statuses.Add(se.Message);

                return statuses;
            }

            // create tcp stream
            NetworkStream stream = client.GetStream();
            byte[] buffer = new byte[client.ReceiveBufferSize];
            StreamWriter sw = new StreamWriter(stream);

            // send Host Status Return request
            sw.Write("~HQES");
            sw.Flush();

            // wait for response
            int i = 0;
            while (stream.DataAvailable == false)
            {
                Thread.Sleep(10);
                i++;
                if (i == 3000)
                {  //timeout after 3 sec
                    statuses.Add("Timeout while Connecting to printer");
                    return statuses;
                }
            }

            // read response, Sometimes it may not pull in the entire
            //    response, so keep trying till you get everything ~144 bytes
            StreamReader sr = new StreamReader(stream);
            int length = 0;
            while ((stream.DataAvailable) || (length < 140))
            {
                length += stream.Read(buffer, length, (buffer.Length - length));
            }

            // close connection
            sr.Close();
            sw.Close();
            client.Close();

            // parse results
            string results = (Encoding.UTF8.GetString(buffer, 0, length));
            ParseStatus.GetAll(results, out statuses);
            return statuses;
        }
    }

    public static class ParseStatus
    {
        public static bool GetAll(string message, out List status)
        {

            // initially, no errors
            status = new List();
            bool ok = false;

            try
            {
                // get the flags for different status conditions
                int is_error = Convert.ToInt32(message.Substring(70, 1));
                int media = Convert.ToInt32(message.Substring(88, 1));
                int head = Convert.ToInt32(message.Substring(87, 1));
                int pause = Convert.ToInt32(message.Substring(84, 1));

                // check each flag that prevents printing
                if (is_error == 0)
                {
                    ok = true;
                    status.Add("Ready to Print");
                }
                if (media == 1)
                    status.Add("Error: Paper out");
                if (media == 2)
                    status.Add("Error: Ribbon Out");
                if (media == 4)
                    status.Add("Error: Media Door Open");
                if (media == 8)
                    status.Add("Error: Cutter Fault");
                if (head == 1)
                    status.Add("Error: Printhead Overheating");
                if (head == 2)
                    status.Add("Error: Motor Overheating");
                if (head == 4)
                    status.Add("Error: Printheat Fault");
                if (head == 8)
                    status.Add("Error: Incorrect Printhead");
                if (pause == 1)
                    status.Add("Printer Paused");
                if ((!ok) && (status.Count == 0))
                    status.Add("Error: Unknown Error");
            }
            catch (FormatException)
            {
                status.Add("Unexpected response from printer, unable to get status");
                status.Add(message);
            }
            return ok;
        }
    }

CONTACT
Can’t find what you’re looking for?