iOS 使用 Epos2Printer 只打印一次
iOS only prints once using Epos2Printer
我正在使用以下代码使用 Epson ePOS SDK for iOS
SDK 在 Epson TM-T20
上打印内容。问题是该应用程序只打印一次。该应用程序必须重新启动才能再次打印。代码有什么问题?
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;
}
}
根据SDK中的iOSdemo,断开连接动作应该在sub-thread中,这样就可以避免应用被冻结。估计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.
}
}
我遇到了同样的问题,您必须实施 Epos2PtrReceiveDelegate 并使用 onPtrReceive 符合其协议并包括断开连接该委托内的打印机。
这是一个代码示例:
public func onPtrReceive(_ printerObj: Epos2Printer!, code: Int32, status: Epos2PrinterStatusInfo!, printJobId: String!) {
printerObj.endTransaction()
printerObj.disconnect()
printerObj.clearCommandBuffer()
printerObj.setReceiveEventDelegate(nil)
}
我有同样的问题,您必须实施 Epos2PtrReceiveDelegate 并使用 onPtrReceive 符合其协议并包括断开连接该委托内的打印机。
示例如下:
希望对您有所帮助!
public func onPtrReceive(_ printerObj: Epos2Printer!, code: Int32, status: Epos2PrinterStatusInfo!, printJobId: String!) {
printerObj.endTransaction()
printerObj.disconnect()
printerObj.clearCommandBuffer()
printerObj.setReceiveEventDelegate(nil)
}
干杯!
供日后参考。
请看示例代码:
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)
})
}
干杯!
我使用了 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()
}
我正在使用以下代码使用 Epson ePOS SDK for iOS
SDK 在 Epson TM-T20
上打印内容。问题是该应用程序只打印一次。该应用程序必须重新启动才能再次打印。代码有什么问题?
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;
}
}
根据SDK中的iOSdemo,断开连接动作应该在sub-thread中,这样就可以避免应用被冻结。估计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.
}
}
我遇到了同样的问题,您必须实施 Epos2PtrReceiveDelegate 并使用 onPtrReceive 符合其协议并包括断开连接该委托内的打印机。
这是一个代码示例:
public func onPtrReceive(_ printerObj: Epos2Printer!, code: Int32, status: Epos2PrinterStatusInfo!, printJobId: String!) {
printerObj.endTransaction()
printerObj.disconnect()
printerObj.clearCommandBuffer()
printerObj.setReceiveEventDelegate(nil)
}
我有同样的问题,您必须实施 Epos2PtrReceiveDelegate 并使用 onPtrReceive 符合其协议并包括断开连接该委托内的打印机。
示例如下:
希望对您有所帮助!
public func onPtrReceive(_ printerObj: Epos2Printer!, code: Int32, status: Epos2PrinterStatusInfo!, printJobId: String!) {
printerObj.endTransaction()
printerObj.disconnect()
printerObj.clearCommandBuffer()
printerObj.setReceiveEventDelegate(nil)
}
干杯!
供日后参考。
请看示例代码:
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)
})
}
干杯!
我使用了 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()
}