4

I'm using a Zebra KR403 receipt printer for a project and I need to programatically read the status from the printer (out of paper, paper near-end, printhead open, paper jam, etc). In the ZPL documentation I found that I need to send a ~HQES command and the printer responds with its status information.

In the project the printer is connected via USB, but I figured it may be easier to get it to work connecting it via COM port and work from there to get it to work over USB. I am able to open communication with the printer and send commands to it (I can print test receipts), but whenever I try to read anything back it simply hangs forever and never gets to read anything.

Here's the code I'm using:

public Form1()
{
    InitializeComponent();
    SendToPrinter("COM1:", "^XA^FO50,10^A0N50,50^FDKR403 PRINT TEST^FS^XZ", false); // this prints OK
    SendToPrinter("COM1:", "~HQES", true); // read is never completed
}

[DllImport("kernel32.dll", SetLastError = true)]
static extern SafeFileHandle CreateFile(
    string lpFileName, 
    FileAccess dwDesiredAccess,
    uint dwShareMode, 
    IntPtr lpSecurityAttributes, 
    FileMode dwCreationDisposition,
    uint dwFlagsAndAttributes, 
    IntPtr hTemplateFile);

private int SendToPrinter(string port, string command, bool readFromPrinter)
{
    int read = -2;

    // Create a buffer with the command
    Byte[] buffer = new byte[command.Length];
    buffer = System.Text.Encoding.ASCII.GetBytes(command);

    // Use the CreateFile external func to connect to the printer port
    using (SafeFileHandle printer = CreateFile(port, FileAccess.ReadWrite, 0, IntPtr.Zero, FileMode.Open, 0, IntPtr.Zero))
    {
        if (!printer.IsInvalid)
        {
            using (FileStream stream = new FileStream(printer, FileAccess.ReadWrite))
            {
                stream.Write(buffer, 0, buffer.Length);

                // tries to read only one byte (for testing purposes; in reality many bytes will be read with the complete message)
                if (readFromPrinter)
                {
                    read = stream.ReadByte(); // THE PROGRAM ALWAYS HANGS HERE!!!!!!
                }

                stream.Close();
            }
        }
    }

    return read;
}

I've found out that when I print the test receipt (first call to SendToPrinter()) nothing gets printed until I close the handle with stream.Close(). I've made these tests but to no avail:

  • calling stream.Flush() after calling stream.Write(), but still nothing gets read (and nothing gets printed either until I call stream.Close())
  • only send command and then close the stream, immediately reopen and try to read
  • open two handles, write on handle 1, close handle 1, read handle 2. nothing

Has anyone has had any luck reading back status from a Zebra printer? Or anyone has any idea of what I may be doing wrong?

MarioVW
  • 2,225
  • 3
  • 22
  • 28
  • I think you'll get more traction (better control) using the [SerialPort](http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.aspx) class than a generic FileStream for this. – 500 - Internal Server Error Mar 07 '13 at 18:46
  • @500-InternalServerError right now I'm connecting it via COM port because I figured that would be easier than USB to start with, but in the actual project the printer is connected via USB so SerialPort class is not an option here. – MarioVW Mar 07 '13 at 19:22
  • Why not? As the name implies, USB ports are serial ports too. – 500 - Internal Server Error Mar 07 '13 at 19:25
  • @500-InternalServerError the SerialPort class is for handling RS232/COM ports, USB is a bus (not a port) and its internal workings are completely different from RS232 (and cannot be accessed through SerialPort class, unless you have a Virtual COM port but that's a different story) – MarioVW Mar 07 '13 at 19:39
  • Good point - I was thinking of the virtual COM port scenario, which the USB printers I've encountered have in fact provided, but I guess they don't have to. – 500 - Internal Server Error Mar 07 '13 at 19:43
  • 1
    This looks to have already been answered by someone trying to do exactly the same as you [over here](http://stackoverflow.com/questions/15363941/which-sdk-should-i-use-for-kr403-zebra-printer). – l33tmike May 29 '13 at 15:44
  • @l33tmike please add an answer with the link so I can mark it as accepted for further reference. I had already looked at that question but before the update with the link was posted. – MarioVW May 30 '13 at 15:50
  • As a general rule for RS232 communications, double check your Baud Rate, Stop Bits & Parity settings and make sure they match those of the device. Plus, not all RS232 cables are created equally. – Phill Jul 31 '13 at 15:42

1 Answers1

0

As pointed out by @l33tmike in the comments, the answer to this question has already been posted on another question: Which SDK should I use for KR403 Zebra Printer

Community
  • 1
  • 1
MarioVW
  • 2,225
  • 3
  • 22
  • 28