Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS only prints once using Epos2Printer

Tags:

ios

epson

I'm using the following code to print something on an Epson TM-T20 using the Epson ePOS SDK for iOS SDK. The problem is that the app only prints once. The app has to be restarted in order to be able to print again. What's wrong with the code?

    printer = Epos2Printer(printerSeries: 2, lang: 1)
    printer?.setReceiveEventDelegate(self)
    printer?.addText("text")

    printer!.connect("TCP:192.168.1.185", timeout:Int(EPOS2_PARAM_DEFAULT))
    printer!.beginTransaction()

    printer?.sendData(Int(EPOS2_PARAM_DEFAULT))
    printer?.endTransaction()
    // printer?.disconnect()
    printer?.clearCommandBuffer()
    printer?.setReceiveEventDelegate(nil)

Despite being used in the documentation, using printer?.disconnect() makes the app freeze, so I had to comment it out.

If you want to take a look at the API documentation, there's a PDF inside the SDK's download.

Update: updated code based on answer (the app still freezes):

func printReceipt() {
    var printer: Epos2Printer?
    printer = Epos2Printer(printerSeries: 2, lang: 1)
    if printer == nil {
      print(“Printer not found!! 11")
    }
    printer?.setReceiveEventDelegate(self)

    printer?.addTextFont(2)
    printer?.addTextSize(1, height: 1)
    printer?.addText(“My Text")
    printer?.addFeedUnit(10)
    printer?.addCut(0)

    var result: Int = Int(EPOS2_SUCCESS.rawValue)

    result = Int(printer!.connect("TCP:192.168.1.185", timeout:Int(EPOS2_PARAM_DEFAULT)));
    result = Int(printer!.beginTransaction())

    printer?.sendData(Int(EPOS2_PARAM_DEFAULT))

    DispatchQueue.global(qos: DispatchQoS.QoSClass.default).async {
      printer?.clearCommandBuffer()
      printer?.setReceiveEventDelegate(nil)
      printer?.endTransaction()
      printer?.disconnect()
      printer = nil;
    }
  }
like image 215
Linus Avatar asked Feb 10 '17 07:02

Linus


3 Answers

I had 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 is a code sample:

public func onPtrReceive(_ printerObj: Epos2Printer!, code: Int32, status: Epos2PrinterStatusInfo!, printJobId: String!) {
        
    printerObj.endTransaction()
    printerObj.disconnect()
    printerObj.clearCommandBuffer()
    printerObj.setReceiveEventDelegate(nil)
}
like image 90
rjcruz Avatar answered Oct 19 '22 05:10

rjcruz


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()
}
like image 33
danishhotdog Avatar answered Oct 19 '22 07:10

danishhotdog


According to the iOS demo in SDK, the disconnect action should be in a sub-thread, then you could avoid the app being freeze. I guess it will be able to print more than once time after the disconnect() function being called successfully.

Try(Swift3):

DispatchQueue.global(qos: DispatchQoS.QoSClass.default).async {
    printer?.endTransaction()
    printer?.disconnect()

    DispatchQueue.main.async {
        //Do UI updating work in Main thread, like enable Print button again.
    }
}
like image 2
Yun CHEN Avatar answered Oct 19 '22 07:10

Yun CHEN