iOS仅使用Epos2Printer打印一次

Nik*_*iko 10 epson ios

我正在使用以下代码在Epson TM-T20使用Epson ePOS SDK for iOS SDK打印内容.问题是应用程序只打印一次.必须重新启动应用程序才能再次打印.代码有什么问题?

    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)
Run Code Online (Sandbox Code Playgroud)

尽管在文档中使用,使用printer?.disconnect()使应用程序冻结,所以我不得不评论它.

如果您想查看API文档,SDK下载中有一个PDF .

更新: 基于答案的更新代码(应用程序仍然冻结):

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;
    }
  }
Run Code Online (Sandbox Code Playgroud)

小智 5

我有同样的问题,你必须实现Epos2PtrReceiveDelegate并使用onPtrReceive符合其协议,并包括在该委托内断开打印机.

以下是样本:

希望它能帮到你!

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

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

    }
Run Code Online (Sandbox Code Playgroud)

干杯!


小智 2

我使用了 Yun Chen 和 rjcruz 提供的类似代码。我的打印机也是 Epson TM-T20。我发现,disconnect() 函数本身会使我的应用程序冻结,无论它放在哪里。唯一有效的解决方案是完全避免disconnect()。所以为了避免冻结,你可以尝试rjcruz提供的代码,只是删除disconnect()函数。希望这可以帮助!

示例代码:

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()
}
Run Code Online (Sandbox Code Playgroud)