Search code examples
javaprintingthermal-printerepsonescpos

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, i.e. whether it is online/offline, whether the paper is ending or the paper is finished, or whether the cash drawer is open/closed.

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. 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. 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. 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. 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.

Is there something I am forgetting in my implementation, or is there a better way than Device Monitoring Studio, to display the printer status?


Solution

  • I had the same problem, but I was connected via the usb, try with the serial port and then read from it. Im not a java developer but here is my solution in 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)

    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