Rectangle 27 0

If you have the chance to use kernel32.dll and leaving out the usb-driver-bound winspool.srv you could use this vanilla approach:

using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using Microsoft.Win32.SafeHandles;

{
    public class USB
    {
        [DllImport("kernel32.dll", SetLastError = true)]
        internal static extern Int32 CancelIo(SafeFileHandle hFile);

        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        internal static extern IntPtr CreateEvent(IntPtr SecurityAttributes,
                                                  Boolean bManualReset,
                                                  Boolean bInitialState,
                                                  String lpName);

        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        internal static extern Boolean GetOverlappedResult(SafeFileHandle hFile,
                                                           IntPtr lpOverlapped,
                                                           ref Int32 lpNumberOfBytesTransferred,
                                                           Boolean bWait);

        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        internal static extern Boolean ReadFile(SafeFileHandle hFile,
                                                IntPtr lpBuffer,
                                                Int32 nNumberOfBytesToRead,
                                                ref Int32 lpNumberOfBytesRead,
                                                IntPtr lpOverlapped);

        [DllImport("kernel32.dll", SetLastError = true)]
        internal static extern Int32 WaitForSingleObject(IntPtr hHandle,
                                                         Int32 dwMilliseconds);

        [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        internal static extern SafeFileHandle CreateFile(String lpFileName,
                                                         UInt32 dwDesiredAccess,
                                                         Int32 dwShareMode,
                                                         IntPtr lpSecurityAttributes,
                                                         Int32 dwCreationDisposition,
                                                         Int32 dwFlagsAndAttributes,
                                                         Int32 hTemplateFile);

        [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        internal static extern Boolean WriteFile(SafeFileHandle hFile,
                                                 ref byte lpBuffer,
                                                 Int32 nNumberOfBytesToWrite,
                                                 ref Int32 lpNumberOfBytesWritten,
                                                 IntPtr lpOverlapped);

        [DllImport("kernel32.dll", SetLastError = true)]
        internal static extern int GetLastError();

        private const Int32 FILE_FLAG_OVERLAPPED = 0X40000000;
        private const Int32 FILE_SHARE_READ = 1;
        private const Int32 FILE_SHARE_WRITE = 2;
        private const UInt32 GENERIC_READ = 0X80000000;
        private const UInt32 GENERIC_WRITE = 0X40000000;
        private const Int32 OPEN_EXISTING = 3;
        private const Int32 WAIT_OBJECT_0 = 0;
        private const Int32 WAIT_TIMEOUT = 0x102;
        private const Int32 ReadBufferSize = 200;

        private readonly string _devicePathName;

        public USB(string devicePathName)
        {
            this._devicePathName = devicePathName;
        }

        public void Send(string data)
        {
            var bData = this.Encoding.GetBytes(data);
            this.Send(bData);
        }

        public void Send(byte[] data)
        {
            try
            {
                var eventObject = CreateEvent(IntPtr.Zero,
                                              false,
                                              false,
                                              String.Empty);
                var hidOverlapped = GetHidOverlapped(eventObject);

                var unManagedBuffer = Marshal.AllocHGlobal(data.Length);
                var unManagedOverlapped = Marshal.AllocHGlobal(Marshal.SizeOf(hidOverlapped));
                Marshal.StructureToPtr(hidOverlapped,
                                       unManagedOverlapped,
                                       false);

                using (var writeHandle = this.GetWriteFileHandle())
                {
                    var numberOfBytesWritten = 0;
                    var success = WriteFile(writeHandle,
                                            ref data[0],
                                            data.Length,
                                            ref numberOfBytesWritten,
                                            unManagedOverlapped);
                    if (!success)
                    {
                        var result = WaitForSingleObject(eventObject,
                                                         100);
                        switch (result)
                        {
                            case WAIT_OBJECT_0:
                                success = true;
                                break;
                            case WAIT_TIMEOUT:
                                CancelIo(writeHandle);
                                break;
                        }
                    }
                }

                Marshal.FreeHGlobal(unManagedOverlapped);
                Marshal.FreeHGlobal(unManagedBuffer);
            }
            catch (Exception ex)
            {
                // TODO add logging and enhance the try/catch-closure to a smaller one
            }
        }

        private Encoding Encoding
        {
            get
            {
                return Encoding.ASCII;
            }
        }

        public string Read()
        {
            var receivedBytes = 0;
            var receiveBuffer = new byte[ReadBufferSize];

            string data;

            try
            {
                var eventObject = CreateEvent(IntPtr.Zero,
                                              false,
                                              false,
                                              String.Empty);
                var hidOverlapped = GetHidOverlapped(eventObject);

                var unManagedBuffer = Marshal.AllocHGlobal(ReadBufferSize);
                var unManagedOverlapped = Marshal.AllocHGlobal(Marshal.SizeOf(hidOverlapped));

                Marshal.StructureToPtr(hidOverlapped,
                                       unManagedOverlapped,
                                       false);

                using (var readHandle = CreateFile(this._devicePathName,
                                                   GENERIC_READ,
                                                   FILE_SHARE_READ /* | FILE_SHARE_WRITE*/,
                                                   IntPtr.Zero,
                                                   OPEN_EXISTING,
                                                   FILE_FLAG_OVERLAPPED,
                                                   0))
                {
                    var success = ReadFile(readHandle,
                                           unManagedBuffer,
                                           receiveBuffer.Length,
                                           ref receivedBytes,
                                           unManagedOverlapped);
                    if (!success)
                    {
                        var result1 = WaitForSingleObject(eventObject,
                                                          300);
                        switch (result1)
                        {
                            case WAIT_OBJECT_0:
                                GetOverlappedResult(readHandle,
                                                    unManagedOverlapped,
                                                    ref receivedBytes,
                                                    false);
                                break;
                            case WAIT_TIMEOUT:
                            default:
                                //CancelIo(_readHandle);
                                break;
                        }
                    }
                }

                if (receivedBytes > 0)
                {
                    Array.Resize(ref receiveBuffer,
                                 receivedBytes);
                    Marshal.Copy(unManagedBuffer,
                                 receiveBuffer,
                                 0,
                                 receivedBytes);
                    data = this.Encoding.GetString(receiveBuffer);
                }
                else
                {
                    data = null;
                }

                Marshal.FreeHGlobal(unManagedOverlapped);
                Marshal.FreeHGlobal(unManagedBuffer);
            }
            catch (Exception ex)
            {
                // TODO add logging and enhance the try/catch-closure to a smaller one
                data = null;
            }

            return data;
        }

        private SafeFileHandle GetWriteFileHandle()
        {
            var writeHandle = CreateFile(this._devicePathName,
                                         GENERIC_WRITE | GENERIC_READ,
                                         FILE_SHARE_READ | FILE_SHARE_WRITE,
                                         IntPtr.Zero,
                                         OPEN_EXISTING,
                                         0,
                                         0);

            return writeHandle;
        }

        private static NativeOverlapped GetHidOverlapped(IntPtr eventObject)
        {
            return new NativeOverlapped
            {
                OffsetLow = 0,
                OffsetHigh = 0,
                EventHandle = eventObject
            };
        }
    }
}
GetPrinter
PRINTER_INFO_2

printing - Reading status from Zebra Printer - Stack Overflow

printing label barcode zebra-printers zpl-ii
Rectangle 27 0

ReadPrinter will not help in this situation. It will read back the print job you have submitted to the printer, not the printer's response. However, for the sake of completeness: In order to use ReadPrinter, you must open the printer again, using the combined "printer name - job id" syntax:

OpenPrinter("Zebra,Job 12345", ...);
ReadPrinter(hPrinter, ...);

This will only work if the job 12345 has not been removed yet.

As for answering the question, you have to use WriteFile to send data and ReadFile to get the response. To use those functions, you need to open the printer with CreateFile. After you've done that, the rest is absolutely trivial.

The problem here is getting the device path that must be passed to CreateFile in order to open the printer. If your printer is an LPT one, that's as simple as "LPT:", but for a USB printer you have to obtain the device path, which looks like this:

\\?\usb#vid_0a5f&pid_0027#46a072900549#{28d78fad-5a12-11d1-ae5b-0000f803a8c2}

I have found a way to obtain this path, but it only works if you have just one printer installed. If you have more, you will need a relation between the device path and the printer name you see in the control panel, and that relation is something I haven't figured yet. I've created a question for that: Figuring which printer name corresponds to which device ID.

WriteFile
ReadFile

are you sure that you can't read data with .ReadPrinter?? msdn.microsoft.com/en-us/library/windows/desktop/ says: "The ReadPrinter function retrieves data from the specified printer."

@AndreasNiedermair Yes, it retrieves data from the printer, but it is the data you put in that printer yourself (your printjob), not the data printer may generate in response to the printjob. WriteFile and ReadFile are general purpose Windows functions that work on many different objects including printers, they are in kernel32.dll.

thanks for your input. so you are basically saying that with winspool.drv there's no chance to read a response, but one should fall back to the basic kernel32.dll (which is vanilla usb without any driver)?

@AndreasNiedermair It is so as far as I know (you can also check out the linked question of mine). There is a driver involved though, because to use it you need to have the printer installed properly, which requires a driver.

printing - Reading status from Zebra Printer - Stack Overflow

printing label barcode zebra-printers zpl-ii
Rectangle 27 0

The solution to the problem that we ended up utilizing was to create a WinUSB driver for the printer. This way the device is treated as a USB device. A ZebraUSB object was created using the driver and a method called WriteRead was created. Using the WriteRead method we sent the ~HQES query to the printer and received a response. Sometimes there is some lag time between the query and the response. To combat this, we set the response to a variable and retrieve it using a different method.

I'm not sure of the specifics of the code because I did not code the WinUSB driver, and I do not have access to its code.

The main point of this answer is that we had to create a WinUSB driver for the printer before any of the status queries could work.

printing - Querying Zebra printer status using RawPrinterHelper class ...

printing status zebra-printers thermal-printer zpl
Rectangle 27 0

I'm facing the same problem. Did you already manage anything on this subject?

-my printer (zebra 2030) doesn't support ZPL, so as far as I know the only way is to send unicode to it

-I made a list of characters I need e.g.

string enq = Convert.ToChar(5).ToString();
string esc = Convert.ToChar(27).ToString();
string nul = Convert.ToChar(0).ToString();
string rs = Convert.ToChar(30).ToString();
string lf = Convert.ToChar(10).ToString();
string cr = Convert.ToChar(13).ToString();

(get those int values from en.wikipedia.org/wiki/ASCII)

-compose the command - e.g. sb.Append(esc + enq + Convert.ToChar(7).ToString()); (from the printer manual, the command < ESC>< ENQ><7> should get the firmware version)

RawPrinterHelper.SendStringToPrinter(printerName, sb.ToString());

and reading? ... this is what this question is all about...

@AndreasNiedermair it was a few years ago, so I don't remember the details. Please see if this experimental project helps dropbox.com/s/2h6gj0o08eksbxu/PrintLabel.zip?dl=0

printing - Reading status from Zebra Printer - Stack Overflow

printing label barcode zebra-printers zpl-ii
Rectangle 27 0

At the time we communicated with the printer over RS-232 (? standard serial comms), which worked well, all information came back from the printer in a timely and accurate fashion.

Recently I'd to work with Epson tally printers, and found the windows printer drivers clumsy and inefficient. I dropped down a level and communicated directly with the printer through GDI, and everything worked to my satisification.

I say take out the middle man, if you drop down a level and communicate with the printer directly, rather than communicating through windows printer drivers, you'll have more success.

printing - Reading status from Zebra Printer - Stack Overflow

printing label barcode zebra-printers zpl-ii
Rectangle 27 0

I've done something very similar and i can tell you that there is almost no way at all to monitor print jobs in .NET.

I've gotten close though, doing the following:

Adding in code to show how its done using the .NET print queue object:

PrintQueue me = Queue; 
if (me != null)
{
    me.Refresh();
    //in this if else,
    //i purposefully put the ones that error out first
    //so that if multiple can be true at the same time
    //the errors will definitely take precedence
    if (me.HasPaperProblem)
    {
        _SystemDotPrintingStatus = new PrinterStatus(PrinterStatusType.Error, "Error: Has paper problem");
    }
    else if (me.IsDoorOpened)
    {
        _SystemDotPrintingStatus = new PrinterStatus(PrinterStatusType.Error, "Error: Door is open");
    }
    else if (me.IsManualFeedRequired)
    {
        _SystemDotPrintingStatus = new PrinterStatus(PrinterStatusType.Error, "Error: Printer needs manually fed");
    }
    else if (me.IsNotAvailable)
    {
        _SystemDotPrintingStatus = new PrinterStatus(PrinterStatusType.Error, "Error: Printer not available");
    }

    else if (me.IsOutOfMemory)
    {
        _SystemDotPrintingStatus = new PrinterStatus(PrinterStatusType.Error, "Error: Printer is out of memory");
    }
    else if (me.IsOutOfPaper)
    {
        _SystemDotPrintingStatus = new PrinterStatus(PrinterStatusType.Error, "Error: Printer is out of paper");
    }
    else if (me.IsOutputBinFull)
    {
        _SystemDotPrintingStatus = new PrinterStatus(PrinterStatusType.Error, "Error: Printer output bin is full");
    }
    else if (me.IsPaperJammed)
    {
        _SystemDotPrintingStatus = new PrinterStatus(PrinterStatusType.Error, "Error: Paper jam");
    }
    else if (me.IsOffline)
    {
        _SystemDotPrintingStatus = new PrinterStatus(PrinterStatusType.Offline, "Offline");
    }
    else if (me.IsBusy)
    {
        _SystemDotPrintingStatus = new PrinterStatus(PrinterStatusType.Busy, "Busy");
    }
    else if (me.IsInitializing)
    {
        _SystemDotPrintingStatus = new PrinterStatus(PrinterStatusType.Busy, "Initializing");
    }
    else if (me.IsIOActive)
    {
        _SystemDotPrintingStatus = new PrinterStatus(PrinterStatusType.Busy, "Sending and recieving data");
    }
    else if (me.IsProcessing)
    {
        _SystemDotPrintingStatus = new PrinterStatus(PrinterStatusType.Busy, "Processing");
    }
    else if (me.IsWarmingUp)
    {
        _SystemDotPrintingStatus = new PrinterStatus(PrinterStatusType.Busy, "Warming up");
    }
    else if (me.IsPendingDeletion)
    {
        _SystemDotPrintingStatus = new PrinterStatus(PrinterStatusType.Busy, "Deleting a job");
    }
    else if (me.IsPaused)
    {
        _SystemDotPrintingStatus = new PrinterStatus(PrinterStatusType.Paused, "Paused");
    }
    else if (me.IsPrinting)
    {
        _SystemDotPrintingStatus = new PrinterStatus(PrinterStatusType.Printing, "Printing");
    }
    else if (me.IsPowerSaveOn)
    {
        _SystemDotPrintingStatus = new PrinterStatus(PrinterStatusType.Ready, "In power save mode");
    }
    else
        _SystemDotPrintingStatus = new PrinterStatus(PrinterStatusType.Ready, "Ready");
}

Thanks for the answer Micah. I was able to query the WMI object and receive a code, but this did not enable me to receive as much information as the ~HQES query to the printer would. The ~HQES query would tell me if the printer was low on paper, if the header was blocked, and information from differing sensors. I'll do some research on this "PrinterDiagnosticFacade", and see what I can find.

The .NET PrintQueue has a ton of info like that too--check out msdn.microsoft.com/en-us/library/ to see all the sweet diagnostic bool values you can get from it. Although if you're satisfied with the ~HQES i wouldn't bother with my advice.

The .NET PrintQueue definitely has some nice bool values. I would try this in conjuction with the WMI Win32_Printer object, but I've been moved to a different project now. Is the PrintQueue available if I'm not using System.Print for my printing purposes?

Thanks for the demo. Seems really straight forward to use. I'll give it a try if I'm ever reassigned to that project. I'd +1, but I don't have enough reputation.

printing - Querying Zebra printer status using RawPrinterHelper class ...

printing status zebra-printers thermal-printer zpl