Receipt printing connection close duration.

A Alexius Lim 3 years 7 months ago
23 1 0

I'm currently working on an IOS app written in swift to print receipts using the Zebra ZQ520 printer with linkOS v1.4.948. Previously I was facing a problem that the printer sometimes does not print the receipt. The problem seems to be the connection is closed before all the data is sent. So what I did was setting a sleep duration before closing the connection. However this method does not seem to be reliable. Is there any way to check if all the data is sent to the printer before closing the connection? The following is my code:
 
func printReceiptLabel(completionHandler: (Bool) -> Void) {
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)) { () -> Void in
            guard let conn = self.openConnection() else {
                dispatch_async(dispatch_get_main_queue()) {
                    self.displayAlertMessage("No printers found")
                }
                return
            }
           
            let open = conn.open()
            let receiptGenerator = ReceiptGenerator()
           
            if let receiptInfo = self.printReceipt {
                let receiptString = receiptGenerator.createReceiptString(receiptInfo)
                let receiptData = receiptString.dataUsingEncoding(NSUTF8StringEncoding) as! NSMutableData
               
                if open {
                    let blockSize = 1024
                    let totalSize = receiptData.length
                    var bytesRemaining = totalSize
                    var error: NSError?
                   
                    while bytesRemaining > 0 {
                        print("Bytes sent")
                        let bytesToSend = min(blockSize, bytesRemaining)
                        let range = NSMakeRange(0, bytesToSend)
                       
                       
                        let partialLabel = receiptData.subdataWithRange(range)
                        conn.write(partialLabel, error: &error)
                       
                        bytesRemaining -= bytesToSend
                       
                        receiptData.replaceBytesInRange(range, withBytes: nil, length: 0)
                    }
                   
                    NSThread.sleepForTimeInterval(NSTimeInterval(0.5))
                    conn.close()
                    dispatch_async(dispatch_get_main_queue(), { () -> Void in
                        completionHandler(true)
                    })
                } else {
                    dispatch_async(dispatch_get_main_queue()) {
                        completionHandler(false)
                        self.displayAlertMessage("Error connecting to printer")
                    }
                }
 
            }
        }
    }

Please Register or Login to post a reply

1 Replies

Y Yanis Dalabiras

Hi Alexius,
The newest version of the SDK 1.4.957 does attempt to fix this issue by introducing a standard delay before closing connection but we have had a very hard time testing this to ensure it fixes most of these issues because of the way iOS handles threads, GCD, and Bluetooth communication in general.  My recommendation is to try the new version to see if it fixes your issue.  Let us know if it doesn't.
Robin

CONTACT
Can’t find what you’re looking for?