12

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;
    }
  }
Linus
  • 4,643
  • 8
  • 49
  • 74
  • No matter where I call disconnect(), it always freezes the app. If I don't use it, the printer seems to be occupied by the app so that no one else can print for several minutes after me. – Linus Feb 17 '17 at 17:35
  • Is your issuer fixed? I also facing same issue. In my case, it prints only once, when I try to print the second time, it throwing connection error. I am using the same code provided in the demo for objective c. – Vikram Chaudhary Feb 20 '20 at 05:51

5 Answers5

6

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!

rjcruz
  • 161
  • 3
  • Thanks for your answer. So is it only possible to use disconnect inside the delegate method? And isn't it a problem then to not use it asynchronously? – Linus Feb 16 '17 at 07:36
  • Yes bro, you don't need asynchronous method because after you implemented the delegate properly it is always called when the sendData finished. Therefore in that delegate you can disconnect your current printer object so that the next time you call again the printer it will generate a new printer object. – rjcruz Feb 16 '17 at 07:42
  • That's very enlightening, thanks a lot for the explanation! I'll try it asap. – Linus Feb 16 '17 at 08:06
  • Hi, please see my code snippet incase you have still encountering any error and for other who experienced same issue as yours. Cheers! – rjcruz Feb 17 '17 at 05:00
  • @rjcruz I am facing the same issue, I have iPad pro with ios12 OS. I already use your code and using this code I can print 2 or 3 times but after that, it will automatically disconnect. my code is in objective c and facing "EPOS2_ERR_CONNECT" this error every time. please help.. – Kaushik Movaliya Oct 04 '18 at 05:22
  • @rjcruz you are a life savior, i searched for this solution in the epson decumentation but never found this. The disconnect() method should only be implemented inside the onPtrReceive() callback. – OussaMah Jul 16 '20 at 14:30
3

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!

Himanshu Moradiya
  • 4,769
  • 4
  • 25
  • 49
rjcruz
  • 161
  • 3
  • I notice you messaging the printerObj in the delegate message onPtrReceive. Note that the Epson SDK documentation explicitly says to NOT message it at this time. I dispatch a block for later execution. – David H Nov 07 '17 at 20:46
  • in my case i face one problem that receipt from right side its not print whole one. its little bit trim in paper and still paper have space from both side . my receipt aliment is center in paper. so please help me @DavidH – Himanshu Moradiya Jan 27 '18 at 09:01
  • I am facing the same issue, I have iPad pro with ios12 OS. I already use your code and using this code I can print 2 or 3 times but after that, it will automatically disconnect. my code is in objective c and facing "EPOS2_ERR_CONNECT" this error every time. please help.. – Kaushik Movaliya Oct 04 '18 at 05:27
  • @KaushikMovaliya is your issue fixed? Please help me to fix if you got any solution. – Vikram Chaudhary Feb 20 '20 at 06:47
2

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.
    }
}
Yun CHEN
  • 6,450
  • 3
  • 30
  • 33
  • Thanks for your answer but the app still freezes this way. – Linus Feb 14 '17 at 20:23
  • I am facing the same issue, I have iPad pro with ios12 OS. I already use your code and using this code I can print 2 or 3 times but after that, it will automatically disconnect. my code is in objective c and facing "EPOS2_ERR_CONNECT" this error every time. please help.. – Kaushik Movaliya Oct 04 '18 at 05:27
  • @KaushikMovaliya, other answers have more function executions, have you tried them? – Yun CHEN Oct 04 '18 at 10:17
  • @KaushikMovaliya how did you resolve it? I'm facing the same issue right now, pre iOS 12 my code would print without problems. Now it only prints once or twice and I get "EPOS2_ERR_TIMEOUT" – Richard McCluskey Apr 29 '19 at 15:15
  • @RichardMcCluskey please check your code and get the delegate method name as " onPtrReceive ". – Kaushik Movaliya Apr 30 '19 at 10:42
  • @RichardMcCluskey you have to disconnect and printer on that method and after the disconnect one printer, you have to reconnect the new second one printer. – Kaushik Movaliya Apr 30 '19 at 10:45
  • @RichardMcCluskey you can do it using the time delay function or using kind of block methods. You can not connect 2 or more printer at a time so you have to connect it one by one using connects & disconnect... – Kaushik Movaliya Apr 30 '19 at 10:47
2

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)
}
tomahh
  • 13,441
  • 3
  • 49
  • 70
rjcruz
  • 161
  • 3
  • I am facing the same issue, I have iPad pro with ios12 OS. I already use your code and using this code I can print 2 or 3 times but after that, it will automatically disconnect. my code is in objective c and facing "EPOS2_ERR_CONNECT" this error every time. please help.. – Kaushik Movaliya Oct 04 '18 at 05:27
2

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()
}
Linus
  • 4,643
  • 8
  • 49
  • 74