我需要开发一个应用程序来打印到热敏打印机,例如 Epson ESC/POS 协议或 ZPL Zebra 协议。我可以使用网络打印机、串行端口打印机以及窗口的假脱机打印机将原始代码写入打印机。当我需要“查询”打印机以获取其状态(就绪、错误、纸张用完等)时,我的麻烦就开始了,在这种情况下,我需要反向通信,我发送一个特殊命令,打印机用一些状态字节回答. 使用网络/套接字打印机或串行端口打印机,一切都很清楚......使用 Windows 打印机我无法理解如何读取打印机状态......
有什么解决办法吗?
更新
按照汉斯的建议,我正在尝试使用 Windows API 读取打印机。这是我对 ESC/POS 协议的测试。
public class WinPrinter
{
// Structure and API declarions:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public class DOCINFOA
{
[MarshalAs(UnmanagedType.LPStr)]
public string pDocName;
[MarshalAs(UnmanagedType.LPStr)]
public string pOutputFile;
[MarshalAs(UnmanagedType.LPStr)]
public string pDataType;
}
[DllImport("winspool.Drv", EntryPoint = "OpenPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter, out IntPtr hPrinter, IntPtr pd);
[DllImport("winspool.Drv", EntryPoint = "ClosePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool ClosePrinter(IntPtr hPrinter);
[DllImport("winspool.Drv", EntryPoint = "StartDocPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool StartDocPrinter(IntPtr hPrinter, Int32 level, [In, MarshalAs(UnmanagedType.LPStruct)] DOCINFOA di);
[DllImport("winspool.Drv", EntryPoint = "EndDocPrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool EndDocPrinter(IntPtr hPrinter);
[DllImport("winspool.Drv", EntryPoint = "StartPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool StartPagePrinter(IntPtr hPrinter);
[DllImport("winspool.Drv", EntryPoint = "EndPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool EndPagePrinter(IntPtr hPrinter);
[DllImport("winspool.Drv", EntryPoint = "WritePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool WritePrinter(IntPtr hPrinter, IntPtr pBytes, Int32 dwCount, out Int32 dwWritten);
[DllImport("winspool.Drv", EntryPoint = "ReadPrinter", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool ReadPrinter(IntPtr hPrinter, [MarshalAs(UnmanagedType.LPStr)] StringBuilder pBytes, Int32 dwCount, out Int32 dwNReadBytes);
public static bool SendBytesToPrinter(string szPrinterName, IntPtr pBytes, Int32 dwCount)
{
Int32 dwError = 0, dwWritten = 0;
IntPtr hPrinter = new IntPtr(0);
DOCINFOA di = new DOCINFOA();
bool bSuccess = false; // Assume failure unless you specifically succeed.
di.pDocName = "My C#.NET RAW Document";
di.pDataType = "RAW";
// Open the printer.
if (OpenPrinter(szPrinterName.Normalize(), out hPrinter, IntPtr.Zero))
{
// Start a document.
if (StartDocPrinter(hPrinter, 1, di))
{
// Start a page.
if (StartPagePrinter(hPrinter))
{
// Write your bytes.
bSuccess = WritePrinter(hPrinter, pBytes, dwCount, out dwWritten);
EndPagePrinter(hPrinter);
}
EndDocPrinter(hPrinter);
}
string reaDstrinG = "";
Int32 buf = 0;
Int32 pcRead;
System.Text.StringBuilder sb = new System.Text.StringBuilder();
bool read = ReadPrinter(hPrinter, sb.Append(reaDstrinG), buf, out pcRead);
ClosePrinter(hPrinter);
}
// If you did not succeed, GetLastError may give more information
// about why not.
if (bSuccess == false)
{
dwError = Marshal.GetLastWin32Error();
}
return bSuccess;
}
static void Main(string[] args)
{
String test = "\u0010\u0004\u0001";
IntPtr pBytes=Marshal.StringToCoTaskMemAnsi(test);
Int32 dwCount = test.Length;
WinPrinter.SendBytesToPrinter("BIXOLON SRP-350 (Copia 1)",pBytes,dwCount);
}
}
我确定“\u0010\u0004\u0001”(DLE EOT 1)是在 ESCPOS 协议中请求打印机状态的推荐,我用串行连接的同一台打印机对其进行了测试,我得到了一个状态字节。
但是,对于上面的代码,我总是从 ReadPrinter 方法中得到错误。我检查了打印机端口是否启用了“双向”选项,并且我也尝试过不使用后台处理程序选项。