7

Problem

Is there a simple ZPL code or way to get an error message back from a Zebra printer to determine if the labels did not print successfully or that there was some kind of error?

Progress

Here is a nice function I built to send a printer job to the zebra printer:

public static void SendToPrinter(string zplString, string ipAddress = "127.0.0.1", int port = 1337)
        {
            // Open connection
            TcpClient tcpClient = new TcpClient();
            tcpClient.Connect(ipAddress, port);

            // Write ZPL String to connection
            using (System.IO.StreamWriter writer = new System.IO.StreamWriter(tcpClient.GetStream()))
            {
                writer.Write(zplString);
                writer.Flush();
                writer.Close();
            }
            // Close Connection
            tcpClient.Close();
        }

There is a lot of magic happening in the zplString, but basically it consists of the ZPL code we all have come to love. The problem in my approach is that it seems this is a rather one way ticket to the printer. A lot of work went into the above and I am hoping we can somehow modify it to listen for a response if I somehow had the appropriate ZPL code to listen to a response?

I simply have not seen any literature or forum discuss how to receive a response back from a zebra printer and determine if it was successful?

Issues

Ideally, I would like a way to understand the printer using ZPL wrapped inside C# and .NET if the printer succeeded or failed in some way. Otherwise, I might have to manually query the user "did it print?". This is not ideal, but I have not yet found anything in my manual that indicated an easy way to detect that there was an error in the print job using ZPL?

Thanks for your patience, assistance, and for reading this question.

hlyates
  • 1,279
  • 3
  • 22
  • 44

3 Answers3

6

Use ~HS or Host Status Command, see page 227 of the ZPL Manual.

Zebra provides a C# Socket example.

The printer will give you the status of the following:

• MEDIA OUT

• RIBBON OUT

• HEAD OPEN

• REWINDER FULL

• HEAD OVER-TEMPERATURE

Elton Saunders
  • 1,238
  • 1
  • 12
  • 23
  • Thanks for the head start. This is extremely useful. I'll work on it this week and will be sure to mark as answer after verifying. :) – hlyates Nov 07 '16 at 14:33
  • 2
    If you have a peel plate, you can also get "LABEL WAITING" from ~HS – StingyJack May 25 '18 at 00:33
  • This command is useful for get status before printing, but how to know if the print job successfully ends? – Tobia Nov 10 '18 at 06:46
  • 1
    The C# link is dead. – Silas Reinagel Apr 01 '20 at 17:23
  • 2
    @SilasReinagel Zebra would like people to use their SDK, so the link now goes to GitHub. But, if you prefer the simple socket example they used to have, they have summary here: https://github.com/Zebra/Zebra-Printer-Samples/issues/1 – Elton Saunders Apr 02 '20 at 18:42
4

The command ~HS or Host Status Command only gives you the status of the printer, but it does not tell you if the print was succesfully

  • 2
    Any answer that gets the asker going in the right direction is helpful, but do try to mention any limitations, assumptions or simplifications in your answer. Brevity is acceptable, but fuller explanations are better. – baduker Apr 13 '18 at 13:15
3

The logic to check if a printer has printed and is in a good state after uses the ~HS command. The code below is using the Link-OS SDK commands to get status, but you can parse the ~HS to get the same info. The "printerStatus.isReadyToPrint" just verifies there are no errors as defined by the ~HQES documentation. This code is useful for if you know your app is likely the only thing interacting with the printer. If you have multiple apps or connections sending print jobs to the same printer, this is not going to work as well.

    public static bool CheckStatusAfter(ZebraPrinter printer)
    {
        try
        {
            printerStatus = printer.GetCurrentStatus();
            while ((printerStatus.numberOfFormatsInReceiveBuffer > 0) && (printerStatus.isReadyToPrint))
            {
                Thread.Sleep(500);
                printerStatus = printer.GetCurrentStatus();
            }
        }
        catch (ConnectionException e)
        {
            Console.WriteLine($"Error getting status from printer: {e.Message}");
        }
        if (printerStatus.isReadyToPrint)
        {
            Console.WriteLine($"Print Success");
            return true;
        }
        else
        {
            Console.WriteLine($"Cannot Print because the printer is in error.");
        }
        return false;
    }

From: https://github.com/Zebra/devtalks/blob/121317-LinkOS_CSharp/BasicWpfPrintApp

RWest
  • 374
  • 1
  • 5