简体   繁体   English

有没有办法使用 ESC 命令获取热敏打印机的状态?

[英]Is there a way to get the status of a thermal printer using ESC commands?

I am adding a method to the library escpos-coffee , which returns the status of a thermal printer, ie whether it is online/offline, whether the paper is ending or the paper is finished, or whether the cash drawer is open/closed.我正在向库escpos-coffee添加一个方法,该方法返回热敏打印机的状态,即是否在线/离线,纸张是否结束或纸张已完成,或者现金抽屉是否打开/关闭。

I have added a method "showPrinterStatus" to the escpos-coffee library, which is based on the ESC c 3 command, which sends the command to the printer in byte form.我在 escpos-coffee 库中添加了一个方法“showPrinterStatus”,它基于ESC c 3命令,它将命令以字节形式发送到打印机。 The Method supposedly enables the Roll paper near end sensor, as well as the Roll paper end sensor.该方法应该启用卷纸即将用完传感器以及卷纸用完传感器。 Furthermore, I have added another method "transmitStatus", based on the GS r command, which transmits the paper sensor status for n=1 and n=49, and the status of the cash drawer for n=2 and n=50.此外,我添加了另一种方法“transmitStatus”,基于GS r命令,它传输 n=1 和 n=49 的纸传感器状态,以及 n=2 和 n=50 的钱箱状态。 Here's the code:这是代码:

 /**
     *
     * @param nSignal
     * @return
     * @throws IOException
     * Method decides whether the printer should return an output paper-end signal to a parallel interface or not
     * input 1,2 4,8 to enable, 0 to disable
     */

    public EscPos showPrinterStatus(int nSignal)  throws IOException {
        write(27);
        write('c');
        write('3');
        write(nSignal);
        return this;
    }

    /**
     *
     * @param n
     * @return
     * @throws IOException
     * returns the status of the printer, 1 or 49 returns paper sensor status, 2 or 50 returns drawer kick-out connector status
     */
    public EscPos transmitStatus(int n) throws IOException{
      write(29);
      write('r');
      return this;
    }

I am using Device Monitoring Studio, and excpected that there would be some visible communication.我正在使用 Device Monitoring Studio,并希望会有一些可见的通信。 It looks like the showPrinterStatus Method is sending a signal to the thermal printer, but the transmitStatus method doesn't seem to cause any communication at all.看起来showPrinterStatus 方法正在向热敏打印机发送信号,但transmitStatus 方法似乎根本没有引起任何通信。 Also, if I check the cash drawer status und leave the cash drawer open, there is no communication at all, and the request is simply queued.此外,如果我检查现金抽屉状态并让现金抽屉保持打开状态,则根本没有通信,请求只是排队。 Once I push the cash drawer back in, it takes 5-10 minutes for the command to be executed by the printer, which is still in the queue all this time.一旦我将钱箱推回,打印机执行命令需要 5-10 分钟,打印机一直在队列中。

Is there something I am forgetting in my implementation, or is there a better way than Device Monitoring Studio, to display the printer status?在我的实施中是否有什么我忘记了,或者有没有比 Device Monitoring Studio 更好的方法来显示打印机状态?

I had the same problem, but I was connected via the usb, try with the serial port and then read from it.我遇到了同样的问题,但我是通过 USB 连接的,尝试使用串行端口,然后从中读取。 Im not a java developer but here is my solution in python我不是 Java 开发人员,但这是我在 Python 中的解决方案

from serial import Serial
serial = Serial('/dev/ttyUSB0', 115200, timeout=.03)
serial.write(b'\x10\x04\x01')
serial.read()

another approach is via the terminal(if you are using linux)另一种方法是通过终端(如果您使用的是 linux)

echo -n '\x10\x04\x01' > /dev/usb/lp0 #assuming lp0 is your printer 

cat /dev/usb/lp0 

it outputs the data in the buffer it does not print it on the paper它输出缓冲区中的数据 它不会将其打印在纸上

If you are using usb interface, you can try to use usb4java lib, then you can read bytes from the printer, for me it works on linux and show when door is open or paper end..., but i haven't tested it on windows...如果您使用的是 usb 接口,您可以尝试使用usb4java lib,然后您可以从打印机读取字节,对我来说,它可以在 linux 上运行并显示门何时打开或纸张用完...,但我还没有测试过在窗户上...

    UsbEndpoint endpointIn = iface.getUsbEndpoint(endpointAddressIn);
    UsbPipe pipeIn = endpointIn.getUsbPipe();
    pipeIn.open();
    byte[] data = new byte[1024];
    int received;
    received = pipeIn.syncSubmit(data);
    pipeIn.close();

complete code is on github: Usb Printer Status完整代码在 github 上: Usb 打印机状态

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM