iOS only prints once using Epos2Printer
Asked Answered
I

5

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;
    }
  }
Indign answered 10/2, 2017 at 7:45 Comment(2)
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.Indign
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.Marlenmarlena
P
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()
}
Posterior answered 20/2, 2017 at 20:36 Comment(2)
This is exactly the solution I needed.Indign
What happens if sendData were to fail?Gemmell
C
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!

Cutlor answered 16/2, 2017 at 1:37 Comment(6)
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?Indign
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.Cutlor
That's very enlightening, thanks a lot for the explanation! I'll try it asap.Indign
Hi, please see my code snippet incase you have still encountering any error and for other who experienced same issue as yours. Cheers!Cutlor
@Cutlor 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..Bushire
@Cutlor 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.Blackness
C
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!

Cutlor answered 17/2, 2017 at 4:59 Comment(4)
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.Giavani
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 @DavidHMulhouse
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..Bushire
@KaushikMovaliya is your issue fixed? Please help me to fix if you got any solution.Marlenmarlena
D
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.
    }
}
Darb answered 13/2, 2017 at 10:10 Comment(7)
Thanks for your answer but the app still freezes this way.Indign
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..Bushire
@KaushikMovaliya, other answers have more function executions, have you tried them?Darb
@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"Quaver
@RichardMcCluskey please check your code and get the delegate method name as " onPtrReceive ".Bushire
@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.Bushire
@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...Bushire
C
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)
}
Cutlor answered 16/2, 2017 at 1:30 Comment(1)
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..Bushire
P
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()
}
Posterior answered 20/2, 2017 at 20:36 Comment(2)
This is exactly the solution I needed.Indign
What happens if sendData were to fail?Gemmell

© 2022 - 2024 — McMap. All rights reserved.