2017-02-10 69 views
9

我正在使用以下代码在Epson TM-T20上使用Epson ePOS SDK for iOSSDK打印某些内容。问题是该应用程序只打印一次。应用程序必须重新启动才能重新打印。代码有什么问题?iOS仅使用Epos2打印机打印一次

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

无论我叫断开(),它总是冻结的应用程序。如果我不使用它,打印机似乎被应用程序占用,以致在我之后没有其他人可以打印几分钟。 – Linus

回答

1

我使用了Yun Chen和rjcruz提供的类似代码。我的打印机也是Epson 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() 
} 
+0

这正是我需要的解决方案。 – Linus

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. 
    } 
} 
+0

感谢您的回答,但应用程序仍然以这种方式冻结。 – Linus

+0

请显示您的更新代码。 –

+0

我用更新后的代码更新了我的问题。 – Linus

2

我有同样的问题,你必须实现Epos2PtrReceiveDelegate和使用onPtrReceive符合它的协议,包括打印机该委托内部断路。

下面是一个代码示例:

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

4

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

以下是样本:

希望它能帮助你!

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

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

    } 

干杯!

+0

感谢您的回答。那么是否只能在委托方法内部使用断开连接?如果不是异步使用它不是一个问题吗? – Linus

+0

是的兄弟,你不需要异步方法,因为在你正确实现委托之后,它总是在sendData完成时被调用。因此,在该代表中,您可以断开当前的打印机对象,以便下一次再次调用打印机时它将生成一个新的打印机对象。 – rjcruz

+0

这非常有启发性,非常感谢解释!我会尽快尝试。 – Linus

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


} 

干杯!

+0

我注意到你在委托消息onPtrReceive中发送消息printerObj。请注意,Epson SDK文档明确表示此时不会发送消息。我派出一个块供以后执行。 –

+0

在我的情况下,我面临的一个问题是,从右侧收到的不是打印整个问题。它的一点点修剪在纸和仍然纸从两边有空间。我的收据是纸面的中心。所以请帮助我@DavidH –

相关问题