iOS仅使用Epos2Printer打印一次

时间:2017-02-10 07:45:56

标签: ios epson

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

    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)

尽管在文档中使用,但使用printer?.disconnect()会冻结应用程序,因此我不得不对其进行评论。

如果您想查看API文档,请在the SDK's download内找到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;
    }
  }

5 个答案:

答案 0 :(得分:5)

我遇到同样的问题,你必须实现 Epos2PtrReceiveDelegate 并使用 onPtrReceive 符合其协议,并包括断开该代理中的打印机。

以下是样本:

希望它会对你有所帮助!

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

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

    }

干杯!

答案 1 :(得分:3)

供将来参考。

请参阅示例代码:

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))

        }
    }

}

在此实施委托方法

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


}

干杯!

答案 2 :(得分:2)

根据SDK中的iOS演示,断开操作应该在子线程中,然后您可以避免应用程序被冻结。我想在成功调用disconnect()函数后,它将能够多次打印。

尝试(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.
    }
}

答案 3 :(得分:2)

我遇到了同样的问题,你必须使用 onPtrReceive 来实现 Epos2PtrReceiveDelegate 并符合其协议,并包括在该委托中断开打印机。

以下是代码示例:



public func onPtrReceive(_ printerObj: Epos2Printer!, code: Int32, status: Epos2PrinterStatusInfo!, printJobId: String!) {
        
    printerObj.endTransaction()
    printerObj.disconnect()
    printerObj.clearCommandBuffer()
    printerObj.setReceiveEventDelegate(nil)
}




答案 4 :(得分:2)

我使用了Yun Chen和rjcruz提供的类似代码。我的打印机也是爱普生TM-T20。我发现,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()
}