iOS only prints once using Epos2Printer

I use a similar code as provided by Yun Chen and rjcruz. My printer is also an Epson TM-T20. I have found, that the disconnect() function in itself makes my app freeze, no matter where it's placed. The only working solution has been to avoid disconnect() altogether. So in order to avoid the freeze, you can try the code provided by rjcruz, just with the disconnect() function removed. Hope this helps!

Sample code:

func print() {
        printer = Epos2Printer(printerSeries: 2, lang: 1)
        printer?.setReceiveEventDelegate(self)
        printer?.addTextSize(2, height: 2)
        printer?.addText("My text")            
        printer?.addFeedUnit(10)
        printer?.addCut(0)
        printer!.connect("TCP:192.168.1.185", timeout:Int(EPOS2_PARAM_DEFAULT))
        printer!.beginTransaction()
        printer?.sendData(Int(EPOS2_PARAM_DEFAULT))
}

public func onPtrReceive(_ printerObj: Epos2Printer!, code: Int32, status: Epos2PrinterStatusInfo!, printJobId: String!) {
        printer?.clearCommandBuffer()
        printer?.setReceiveEventDelegate(nil)
        printer?.endTransaction()
}

For future reference.

Please see sample code:

class ReportsViewController: UIViewController, Epos2PtrReceiveDelegate {

    func printReport() {

        let header = printer_model.getReportHeader()
        let content = printer_model.getReportContent()

        printer = Epos2Printer(printerSeries: EPOS2_TM_T88.rawValue, lang: EPOS2_MODEL_ANK.rawValue)

        printer!.setReceiveEventDelegate(self) 

        printer!.addFeedLine(1)

        printer!.addTextFont(1)
        printer!.addTextAlign(1)

        let logoData = UIImage(named: "logo.png")

        printer!.add(logoData!, x: 0, y:0,
                     width:Int(logoData!.size.width),
                     height:Int(logoData!.size.height),
                     color:EPOS2_COLOR_1.rawValue,
                     mode:EPOS2_MODE_MONO.rawValue,
                     halftone:EPOS2_HALFTONE_DITHER.rawValue,
                     brightness:Double(EPOS2_PARAM_DEFAULT),
                     compress:EPOS2_COMPRESS_AUTO.rawValue)

        printer!.addText("\n")
        printer!.addText(header)
        printer!.addText(content)
        printer!.addText(constants.PRINTER_LINE)

        printer!.addFeedLine(1)

        printer!.addCut(EPOS2_CUT_FEED.rawValue)

        let status = printer!.connect("TCP:\(PRINTER_IP_ADDRESS)", timeout: PRINTER_TIMEOUT)

        if status != 0 {

            // error
            // handle your logic here if you cannot connect to the printer

            self.printerErrorPrompt()

        } else {

            // send your data to the printer

            printer!.beginTransaction()
            printer!.sendData(Int(EPOS2_PARAM_DEFAULT))

        }
    }

}

Implement here the delegate method

public func onPtrReceive(_ printerObj: Epos2Printer!, code: Int32, status: Epos2PrinterStatusInfo!, printJobId: String!) {
      DispatchQueue.global(qos: DispatchQoS.QoSClass.default).async(execute: {
          printerObj.endTransaction()
          printerObj.disconnect()
          printerObj.clearCommandBuffer()
          printerObj.setReceiveEventDelegate(nil)
      })


}

Cheers!


I have the same issue, you have to implement the Epos2PtrReceiveDelegate and conforms to its protocol using the onPtrReceive and includes the disconnecting of the printer inside that delegate.

Here are the sample:

Hope it will help you!

public func onPtrReceive(_ printerObj: Epos2Printer!, code: Int32, status: Epos2PrinterStatusInfo!, printJobId: String!) {

        printerObj.endTransaction()
        printerObj.disconnect()
        printerObj.clearCommandBuffer()
        printerObj.setReceiveEventDelegate(nil)

    }

Cheers!

Tags:

Ios

Epson