I have developed an iOS app using SwiftUI that communicates with an Epson thermal printer through the Epson SDK. The printer class (PrinterManager) I've implemented is based on the UIKit example provided in the Epson SDK, which uses TCP to connect and send data to the thermal printer.
The issue I'm facing is that when my app prints to the thermal printer, it seems to block network access to the printer for other applications. However, when I run the SDK sample directly, this blocking behavior doesn't occur.
Here is a simplified version of my PrinterManager class:
class PrinterManager: NSObject, Epos2PtrReceiveDelegate {
func onPtrReceive(_ printerObj: Epos2Printer!, code: Int32, status: Epos2PrinterStatusInfo!, printJobId: String!) {
self.disconnectPrinter()
}
private var valuePrinterSeries: Epos2PrinterSeries
private var valuePrinterModel: Epos2ModelLang
private var printerTarget: String
private var printer: Epos2Printer?
init(printerSeries: Epos2PrinterSeries, printerModel: Epos2ModelLang, printerTarget: String) {
self.valuePrinterSeries = printerSeries
self.valuePrinterModel = printerModel
self.printerTarget = printerTarget
}
override init() {
self.valuePrinterSeries = EPOS2_TM_T88VII
self.valuePrinterModel = EPOS2_MODEL_ANK
self.printerTarget = ""
}
@discardableResult
func runPrinterReceiptSequence(receipt: Receipt) -> Bool {
initializePrinterObject()
if !createReceiptData(receipt: receipt) {
print("failed at create receipt data")
return false
}
if !printData() {
print("failed at print data")
return false
}
return true
}
func disconnectPrinter() {
var result: Int32 = EPOS2_SUCCESS.rawValue
if printer == nil {
return
}
//Note: This API must be used from background thread only
printer!.setReceiveEventDelegate(nil)
printer!.clearCommandBuffer()
result = printer!.disconnect()
if result != EPOS2_SUCCESS.rawValue {
// DispatchQueue.main.async(execute: {
// MessageView.showErrorEpos(result, method:"disconnect")
// })
print("failed to disconnect printer")
}
}
private func initializePrinterObject() -> Bool {
let thePrinter = Epos2Printer(printerSeries: valuePrinterSeries.rawValue, lang: valuePrinterModel.rawValue)
if thePrinter == nil {
return false
}
printer = thePrinter
return true
}
func printData() -> Bool {
if printer == nil {
print("printer is actually nil")
return false
}
if !connectPrinter() {
printer!.clearCommandBuffer()
return false
}
let result = printer!.sendData(Int(EPOS2_PARAM_DEFAULT))
if result != EPOS2_SUCCESS.rawValue {
print("failed at sending data to the printer")
printer!.clearCommandBuffer()
// MessageView.showErrorEpos(result, method:"sendData")
printer!.disconnect()
return false
}
printer!.clearCommandBuffer()
return true
}
func connectPrinter() -> Bool {
var result: Int32 = EPOS2_SUCCESS.rawValue
if printer == nil {
return false
}
//Note: This API must be used from background thread only
result = printer!.connect(printerTarget, timeout:Int(EPOS2_PARAM_DEFAULT))
if result != EPOS2_SUCCESS.rawValue {
// MessageView.showErrorEpos(result, method:"connect")
return false
}
return true
}
Problem: Whenever my app prints using this class, it blocks network access to the thermal printer, preventing other instances of my app or other applications from printing simultaneously.
Question: Can someone please review my PrinterManager class and help me identify the issue causing the blocking behavior? I want to allow multiple instances of my app to print to the thermal printer concurrently without blocking access for other applications.
Any insights or suggestions would be greatly appreciated!
What I have tried:
- I've ensured that the connectPrinter and disconnectPrinter methods are being called appropriately.
- I've compared my implementation to the SDK sample, and it seems similar.
What I am expecting I can have two instances of my app both printing successfully
What I am actually getting Only one app can connect to the thermal printer and print. Any other app will be blocked from connecting to the thermal printer forever
References: Link to the Epson SDK: https://download.epson-biz.com/modules/pos/index.php?page=single_soft&cid=7423&scat=58&pcat=52