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;
}
}