【问题标题】:How to read back status from Zebra receipt printer?如何从 Zebra 收据打印机读回状态?
【发布时间】:2013-02-22 23:24:46
【问题描述】:

我在一个项目中使用 Zebra KR403 收据打印机,我需要以编程方式读取打印机的状态(缺纸、纸张快用完、打印头打开、卡纸等)。在 ZPL 文档中,我发现我需要发送一个~HQES 命令,打印机会用它的状态信息进行响应。

在项目中,打印机通过 USB 连接,但我认为通过 COM 端口连接打印机并从那里通过 USB 工作可能更容易。我能够打开与打印机的通信并向其发送命令(我可以打印测试收据),但是每当我尝试读回任何内容时,它就会永远挂起并且永远无法读取任何内容。

这是我正在使用的代码:

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;
}

我发现当我打印测试收据时(第一次调用SendToPrinter()),直到我用stream.Close() 关闭句柄之前什么都不会打印。我已经进行了这些测试,但无济于事:

  • 在调用stream.Write() 之后调用stream.Flush(),但仍然没有读取任何内容(在我调用stream.Close() 之前也没有打印任何内容)
  • 只发送命令然后关闭流,立即重新打开并尝试读取
  • 打开两个句柄,在句柄 1 上写入,关闭句柄 1,读取句柄 2。什么都没有

有没有人幸运地从 Zebra 打印机读回状态?或者有人知道我可能做错了什么吗?

【问题讨论】:

  • 我认为使用 SerialPort 类比使用通用 FileStream 会获得更多的牵引力(更好的控制)。
  • @500-InternalServerError 现在我通过 COM 端口连接它,因为我认为它比 USB 更容易开始,但在实际项目中,打印机是通过 USB 连接的,所以 SerialPort 类是这里不是一个选项。
  • 为什么不呢?顾名思义,USB口也是串口。
  • @500-InternalServerError SerialPort 类用于处理 RS232/COM 端口,USB 是总线(不是端口),其内部工作方式与 RS232 完全不同(不能通过 SerialPort 类访问,除非你有一个虚拟 COM 端口,但那是另一回事)
  • 这看起来已经有人试图做和你一样的人回答了over here

标签: c# .net zebra-printers zpl


【解决方案1】:

正如@l33tmike 在 cmets 中指出的,这个问题的答案已经发布在另一个问题上:Which SDK should I use for KR403 Zebra Printer

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-20
    相关资源
    最近更新 更多