retrieve printer data (status)

Posted on 2014-04-07
Last Modified: 2014-05-06
i use a POS/receipt Printer (e.g. Citizen CS 300).
For special functions the printer has special ESC-Sequences.
E.g. Cut Paper, Open a connected Cash Drawer etc.

I Use winspool.drv functions in c# to send the esc-sequence as raw data to the printer.
[DllImport("winspool.Drv", EntryPoint = "OpenPrinterA", .....
[DllImport("winspool.Drv", EntryPoint = "StartDocPrinterA", ...
[DllImport("winspool.Drv", EntryPoint = "StartPagePrinter"....
[DllImport("winspool.Drv", EntryPoint = "WritePrinter"...
Sending the ESC-sequences - to use the special functions - works fine.

Now my question.
The printer supports also specific status information, e.g. status of an connected cash drawer (open ore close), paper sensor etc.

The way to get these Information is to send a status request (ESC-Sequence) to the printer, then the printer "transmit the requested information".

How can I read these "transmitted" Information.

Thank you

(pls: no wmi)
Question by:PeterInStingbert
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
LVL 16

Expert Comment

ID: 39985141

to read data back from printer you need a bi-directional communication port. Although USB is a bi-directional comm channel, it is normaly occupied by the printer driver and a second app may not open the same comm channel.

Possibly the driver exposes a direct communication channel to the printer wrapping ReadPort and WritePort (see

But Citizen also provide virtual com port for there POS printers (I assume you have a CT S300 and not CS 300): and look for "V-COM(Virtual COM port) Drivers":

V-COM(Virtual COM port) Drivers
File size 985k bytes
This is for the software to support serial port only.
Using this driver, USB port can be used as serial port.
* Installation manual and Release Note in English are included in ZIP file.

Open in new window

direct link

I assume this driver offers a bi-directional direct comm channel to the USB connectd printer.

If you have the oportunity to connect the printer via serial (RS232) you may just use the provided COMx port to directly communicate with the printer.

If you start to comm directly with the printer you should NOT use driver functions any more. Instead send data to print using the native printer language. A cmd ref guide is availalable here: The printer language is based on the simple to use ESCP standard extended with special printer commands.

Citizien also offers a POS for .NET driver: I am not sure what it exactly provides, but chances that it supports direct communication too. It does not mention CT-S300, whereas citizen talks about CT-300/310 on other pages and both printers share the same cmd ref guide. Please check with citizen if that driver supports your printer.

I would first go with the .NET driver. If that does not meet your needs you may go on with the V_COM driver and the cmd ref guide. Remember that for both solutions there is no need to use winspool functions any more.

Author Comment

ID: 39985433
Is the fact that the printer driver occupy the comm channel why ReadPrinter fails ?  (error 6 invalid handle) or is ReadPrinter for something else ?

My wish is "a simple/universal" solution based on the given windows printer because we use also other printers (epson, bixolon, ibm, tbi...).

Do you have an c# ReadPort/WrtiePort example for me?

LVL 16

Expert Comment

ID: 39985576

MS is not detailed about the winspool ReadPrinter API call, but keep in mind that this a winspool API, the spooler is not the printer direct communication channel. The spooler is a queue buffer with data to (and from?) the printer.

OTOH there are sites with info about error 6 with ReadPrinter(): The problem is the MS doc, ReadPrinter seems to be only callable with a printjob handle BUT NOT with a printer handle:!msg/microsoft.public.win32.programmer.gdi/C-uBnDGfwOo/G0lDqm5zKq0J

Hopefully the last link solves your problem and let you go on universal.

Save the day with this special offer from ATEN!

Save 30% on the CV211 using promo code EXPERTS30 now through April 30th. The ATEN CV211 connects a laptop directly to any server allowing you instant access to perform data maintenance and local operations, for quick troubleshooting, updating, service and repair.


Author Comment

ID: 39986197
I have played with the solution. (s. code below)

Yes, I Can read from the print job handle
BUT the result ist not the transmited status, the result is always the value that was sent to the printer (the status-Request-Command)!

Any Ideas ????

public static byte[] ReadBytesFromPrinter(string szPrinterName, byte[] statusRequest)
    bool bSuccess = false;
    IntPtr hPrinter = new IntPtr(0);
    IntPtr hPrintJob = new IntPtr(0);
    byte[] status = new byte[0];

    // Open the printer.
    if (OpenPrinter(szPrinterName.Normalize(), out hPrinter, IntPtr.Zero))
        // Start a document.
        DOCINFOA di = new DOCINFOA() { pDocName = "Peters RAW PrintDocument", pDataType = "RAW" };

        Int32 jobId = StartDocPrinter(hPrinter, 1, di);

        if (jobId > 0)
            // We can't read from a printer handle, but we can read from a printer job handle, 
            // So the trick is to create a Job using StartDocPrinter, then open a handle to the printer job...
            string jobName = string.Format("{0}, Job {1}", szPrinterName, jobId);

            if (OpenPrinter(jobName.Normalize(), out hPrintJob, IntPtr.Zero))

                // Start a page and print the Status-Request Sequence
                if (StartPagePrinter(hPrinter))
                        Int32 dwCount = statusRequest.Length;
                        IntPtr pStatusRequest = Marshal.AllocCoTaskMem(dwCount);
                        Marshal.Copy(statusRequest, 0, pStatusRequest, dwCount);
                        Int32 dwWritten = 0;

                        bSuccess = WritePrinter(hPrinter, pStatusRequest, dwCount, out dwWritten);

                        EndDocPrinter(hPrinter);                        // EndPage and EndDoc here, otherwise ReadPrinter ist always null

                     // System.Threading.Thread.Sleep(2000);              // Force ReadPrinter-Error 0x3F

                    if (bSuccess)
                        //read request from "Job Handle"
                        Int32 bufLen = 32;
                        IntPtr pStatus = Marshal.AllocCoTaskMem(bufLen);        
                        Int32 statusLen = 0;

                        bSuccess = ReadPrinter(hPrintJob, pStatus, bufLen, out statusLen);

                        int err = Marshal.GetLastWin32Error();          // Sometimes is error 0x3F : Your file waiting to be printed was deleted.

                        status = new byte[statusLen];
                        if (statusLen > 0)
                            Marshal.Copy(pStatus, status, 0, statusLen);


    return status;

Open in new window

LVL 16

Accepted Solution

hjgode earned 500 total points
ID: 39986562
Hello Peter

that is similar to what I esspct to get from a spooler, it does not read the printer status but the spooler 'status'.

The only valid approach is using a RS232 or Network or USB V-COM connection. But you need to write wrappers for the various models you want to control and to print on.

I am sorry, but there is no general read print status solution for all printer types. The status is normally queried inside the printer driver and there is no standard to read that back into an application (the specialized printer driver status monitor software are able to show ink levels etc, as the manufaturer know there drivers).
There are only some general errors that are reported back into the windows system as PaperOut etc.



Author Comment

ID: 39988804
Hello Josef
thank you.

What is readport/writeport (printmonitor) ? - or is this a wrong track ?

LVL 16

Expert Comment

ID: 39989340
You  might give ReadPort/WritePort a try, but as MSDN says, it "should do...".

Let me know your results. I cannot check for your printers.
LVL 38

Expert Comment

ID: 40046460
I've requested that this question be closed as follows:

Accepted answer: 500 points for hjgode's comment #a39986562

for the following reason:

This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.

Author Comment

ID: 40046459
Sorry for the delay....

I didn't checked ReadPort/WritePort ...

I use  FileIO.CreateFile(...) in combination with setupapi.dll to evaluate the "devicename".
It works but only with USB Devices

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Save json data from URL using SSIS 1 77
ComboBox to String Not Working 5 43
C# DateTime Help 6 46
convert Absolute path to relative path of FTP location 7 50
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Printers have changed substantially in the last 30 or so years, not just in technical capabilities but in cost and usage as well.  Printers were originally used for interfacing with the operator, not necessarily for printing copy or pictures. In …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit If you want to manage em…
In an interesting question ( here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question