如何实时检查爱普生收据打印机状态?
目前,我使用预定的计时器每 10 秒运行一次 Epos2Discovery.start 以检查打印机的可用性。
printerCheckTimer = Timer.scheduledTimer(timeInterval: 10, target: self, selector: #selector(checkPrinter), userInfo: nil, repeats: true)
// Listen for printer connection
@objc func checkPrinter() {
// Stop previous epson printer discovery if any, and reset scanned epson printers array
var result = EPOS2_SUCCESS.rawValue
result = Epos2Discovery.stop()
// Search for epson printers
result = Epos2Discovery.start(filterOption, delegate: self)
if result != EPOS2_SUCCESS.rawValue {
print(result)
}
}
// Delegate for epson printer discovery
func onDiscovery(_ deviceInfo: Epos2DeviceInfo!) {
// Loop all the connected epson printers port, and see if they exists in the nearby ports
for (printerId, connectedPrinterPort) in self.connectedEpsonPrinters {
if connectedPrinterPort == deviceInfo.target {
onlineEpsonPrinters[printerId] = Int(Date().timeIntervalSince1970)
}
}
}
该解决方案适用于较旧的 TM-T82(序列号:UEHF...)。但是,对于 TM-T82(序列号:X4XQ...)的较新版本,这似乎是有问题的。
运行 Epos2Discovery.start 计时器的并发设备数量似乎正在影响打印性能。
使用 1 台设备时,它可以正常工作,打印速度也一样快。但是,对于 2 台设备,打印速度变慢(可能慢了 5 秒)。
使用 3 台设备时,打印机状态会在在线和离线状态之间闪烁,打印一半会出现打印失败错误提示。似乎在具有重复计时器的多个设备上同时运行的 Epos2Discovery 导致打印机检测出现问题。
这只发生在 TM 型号的较新版本上,它对我之前购买的旧打印机(TM-T88、TM-U220B、TM-T81)工作得非常好。
我想知道是否有任何其他方法可以实时检查打印机状态?