我对Zebra打印机有一个奇怪的问题。从大局来看,我有代码从队列中逐个获取要打印的项目。因此,如果在打印时启动,并且队列中有3个项目,代码将循环并获取队列中的第一个数据,将其发送到打印机,并删除队列中的第一个数据。有点像出列。
问题是,如果它是循环并直接将数据发送到打印机的代码,则打印机只会打印第一个项目。即使NSLog显示打印机连接已打开,数据已发送,打印成功,打印机连接已关闭,但下一个项目已消失。
但是如果每次代码打印一个标签,然后应用程序显示消息框,如"按OK打印下一个标签",然后用户点击OK按钮,它可以打印第二个和每按一下按钮后,标签的其余部分。
然后我试图模仿这个。我已经尝试使用计时器来按顺序按下按钮" [btnPrint sendActionsForControlEvents:UIControlEventTouchUpInside]
,我也使用timer直接调用函数,或者给线程延迟,但没有一个工作。它必须从一个人体触摸的按钮开始。我不知道为什么。
以下是代码:
// main function to print
-(void) printLabel {
if ([dataToPrint count] > 0) {
[self printWithData:[dataToPrint objectAtIndex:0]];
}
}
-(void)printWithData:(NSString*) data;
{
NSString *zplString = data;
// do something with zplString
NSLog(@"Sending data to printer");
printHandler = [[PrintingHandler alloc] init];
[printHandler setDelegate:self];
[printHandler initialize];
[printHandler printToSerial:bluetoothSerialNumber withData:zplString];
}
// delegate to call if the print is success
-(void) printIsSuccess
{
[dataToPrint removeObjectAtIndex:0];
// in here, I just use sleep code instead of button tap emulation to avoid unnecessarily too long code
[NSThread sleepForTimeInterval:2.0f];
[self printLabel];
}
// this is method inside PrintingHandler class that get called by PrintingHandler (printToSerial:)
-(void) printLabelWithData:(NSString*) zplData toPrinter:(NSString*) serial withSender:(id) sender
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^ {
// Instantiate connection to Zebra Bluetooth accessory
id<ZebraPrinterConnection, NSObject> thePrinterConn = [[MfiBtPrinterConnection alloc] initWithSerialNumber:serial];
// Open the connection - physical connection is established here.
BOOL success = [thePrinterConn open];
NSError *error = nil;
// Send the data to printer as a byte array.
success = success && [thePrinterConn write:[zplData dataUsingEncoding:NSUTF8StringEncoding] error:&error];
[NSThread sleepForTimeInterval:1.0f];
//Dispath GUI work back on to the main queue!
dispatch_async(dispatch_get_main_queue(), ^{
if (success != YES || error != nil) {
[delegate printFailed];
UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:@"Error" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[errorAlert show];
[errorAlert release];
}
else if (success != YES) {
NSLog(@"Print is not success, but no error raised");
[delegate printSuccess];
}
else
{
NSLog(@"Print is success");
[delegate printSuccess];
}
});
// Close the connection to release resources.
NSLog(@"printer connection closed");
[thePrinterConn close];
[thePrinterConn release];
});
}
答案 0 :(得分:0)
抱歉,我找到了解决方案。问题是打开与打印机的连接和将数据发送到打印机之间的速度太快了。我拖延了一些,但我把延误放在了错误的位置。
所以当前打印的步骤是:
正确的应该是:
我在这里给出答案,这样可以帮助其他有相同问题的人。
-(void) printLabelWithData:(NSString*) zplData toPrinter:(NSString*) serial withSender:(id) sender
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^ {
// Instantiate connection to Zebra Bluetooth accessory
id<ZebraPrinterConnection, NSObject> thePrinterConn = [[MfiBtPrinterConnection alloc] initWithSerialNumber:serial];
// Open the connection - physical connection is established here.
BOOL success = [thePrinterConn open];
NSError *error = nil;
[NSThread sleepForTimeInterval:1.0f]; // this is the important one
// Send the data to printer as a byte array.
success = success && [thePrinterConn write:[zplData dataUsingEncoding:NSUTF8StringEncoding] error:&error];
//Dispath GUI work back on to the main queue!
dispatch_async(dispatch_get_main_queue(), ^{
if (success != YES || error != nil) {
[delegate printFailed];
UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:@"Error" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[errorAlert show];
[errorAlert release];
}
else if (success != YES) {
NSLog(@"Print is not success, but no error raised");
[delegate printSuccess];
}
else
{
NSLog(@"Print is success");
[delegate printSuccess];
}
});
// Close the connection to release resources.
NSLog(@"printer connection closed");
[thePrinterConn close];
[thePrinterConn release];
});
}