DantSu / ESCPOS-ThermalPrinter-Android

Useful library to help Android developpers to print with (Bluetooth, TCP, USB) ESC/POS thermal printer.
MIT License
1.18k stars 359 forks source link

buzzer integrated #525

Open fabry84 opened 3 months ago

fabry84 commented 3 months ago

Hi, congratulations, your project also works very well with Italian printers. Can I ask you if you have also planned to activate the buzzer integrated into the printers when cutting the paper? or is it possible to send escpos commands directly to the printer?

Thank you

fabry84 commented 3 months ago

Hi Dantsu, could you give me some help? I found the code for the internal buzzer, but when I send it I get an error. I attach the code and error, what am I doing wrong? Thank you TcpConnection printerConnection = new TcpConnection("192.168.1.151",9100); printerConnection.write(new byte[]{0x1b,0x42,0x02,0x01}); printerConnection.send(1);

The error is in the line printerConnection.send(1);

Caused by: com.dantsu.escposprinter.exceptions.EscPosConnectionException: Unable to send data to device.

kelalaka153 commented 1 month ago

To send the commands, you need to make the printerConnection public, then you can send commands easily; For example, the below can be used to turn the printer into paging mode, and auto-feed to the next page on reset.

class HME300PrinterCommands(
    printerConnection : DeviceConnection,
    charsetEncoding : EscPosCharsetEncoding
) :
    EscPosPrinterCommands(
        printerConnection,
        charsetEncoding)
{
    /**
     * Reset printers parameters.
     */
    override fun reset(): EscPosPrinterCommands {
        if (printerConnection.isConnected) {
            return this
        }
        super.reset()
        // ACTIVE HERE PAGE MODE AND DIRECTION
        printerConnection.write(HME300PrinterCommands.ENABLE_PAGE_MODE)
        printerConnection.write(HME300PrinterCommands.PAGE_MODE_DIRECTION)
        printerConnection.send(100)
        return this
    }

    override fun disconnect() {
        newpage()
        super.disconnect()
    }

    override fun cutPaper(): EscPosPrinterCommands {
        return super.cutPaper()
    }

    private fun newpage() {
        printerConnection.write(HME300PrinterCommands.PRINT_AND_FEED)
        printerConnection.send(100)
    }

    companion object {
        val ENABLE_PAGE_MODE: ByteArray = byteArrayOf(0x1B, 0x4C)
        val PAGE_MODE_DIRECTION: ByteArray = byteArrayOf(0x1B, 0x54, 0x00)
        val PRINT_AND_FEED: ByteArray = byteArrayOf(0x0C)
    }
}

To use it, you need to construct the EscPosPrinter as


 val printer = EscPosPrinter(

                        HME300PrinterCommands(
                            printerIsFound as DeviceConnection,
                            EscPosCharsetEncoding("windows-1256", 16)),
                        203,
                        72f,
                        42,

                    )
``

Hope helps