You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS SwiftUI应用调用Epson热敏打印机引发网络访问阻塞问题排查

Epson热敏打印机TCP连接阻塞问题排查

我使用SwiftUI开发了一款iOS应用,通过Epson SDK与Epson热敏打印机通信。实现的PrinterManager类基于Epson SDK提供的UIKit示例,采用TCP连接并向打印机发送数据。

目前遇到的问题是:当我的应用进行打印时,会阻塞热敏打印机的网络访问,导致应用多实例或其他应用无法同时打印,但直接运行SDK示例程序时不会出现此阻塞行为。

简化版PrinterManager类代码

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

问题详情

每当我的应用通过此类执行打印操作时,会阻塞热敏打印机的网络访问,导致我的应用多实例或其他应用无法同时打印。

求助问题

审查我的PrinterManager类,找出导致阻塞行为的原因,实现多实例应用同时打印且不阻塞其他应用访问的需求。

已尝试操作

  • 确认connectPrinter和disconnectPrinter方法已被正确调用;
  • 将自身实现与SDK示例进行对比,二者逻辑相似。

预期结果

两个应用实例均可成功打印。

实际结果

仅一个应用能连接打印机并打印,其他应用将永久无法连接该打印机。

内容的提问来源于stack exchange,提问作者KIEU BUI

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 21:54:52