Hi
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".
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.
V-COM(Virtual COM port) DriversV-COM_3.0.0.12-3.0.0.19_E.zipFile size 985k bytesThis 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.
Citizien also offers a POS for .NET driver: http://www.citizen-systems.com/Pages/UisSupport/drivers/pos_dot_net/pos_dot_net_drivers.aspx. 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.
Hello
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?
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.
Hopefully the last link solves your problem and let you go on universal.
~josef
0
An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.
One of a set of tools we're offering as a way of saying thank you for being a part of the community.
Hallo,
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); EndPagePrinter(hPrinter); EndDocPrinter(hPrinter); // EndPage and EndDoc here, otherwise ReadPrinter ist always null Marshal.FreeCoTaskMem(pStatusRequest); } // 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); Marshal.FreeCoTaskMem(pStatus); } //EndPagePrinter(hPrinter); //EndDocPrinter(hPrinter); } ClosePrinter(hPrintJob); } } ClosePrinter(hPrinter); } return status;}
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.
An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.
One of a set of tools we're offering as a way of saying thank you for being a part of the community.
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 http://msdn.microsoft.com/en-us/library/windows/hardware/ff561757%28v=vs.85%29.aspx).
But Citizen also provide virtual com port for there POS printers (I assume you have a CT S300 and not CS 300): https://www.citizen-systems.co.jp/english/support/download/printer/driver/windows/drvdown-e.htm and look for "V-COM(Virtual COM port) Drivers":
Open in new window
direct link https://www.citizen-systems.co.jp/english/support/download/printer/driver/windows/V-COM_3.0.0.12-3.0.0.19_E.zip
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: http://www.citizen-systems.com/Pages/UisSupport/downloads/commandRef/CommandReference015E_120607.pdf. 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: http://www.citizen-systems.com/Pages/UisSupport/drivers/pos_dot_net/pos_dot_net_drivers.aspx. 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.