我有一个斑马打印机奇怪的问题。在大的图片上,我有代码来获取项目,并从队列中逐一打印。如果打印开始时,队列上有3个项目,代码将循环获取队列中的第一个数据,发送给打印机,并删除队列中的第一个数据。有点像排队。
问题是,如果是循环并将数据直接发送到打印机的代码,则打印机将只打印第一项。即使NSLog显示打印机连接打开,数据发送,打印成功,打印机连接关闭,下一个项目也消失了。
但如果每次代码打印一个标签,然后应用程序显示"按确定打印下一个标签"这样的消息框,然后用户点击确定按钮,它可以在每次点击按钮后打印第二个标签和剩余的标签。
我试着模仿这一点。我试图使用计时器来"按按钮编程"[btnPrint sendActionsForControlEvents:UIControlEventTouchUpInside]
,我也使用计时器来直接调用函数,或者给线程延迟,但没有工作。它必须从一个按钮启动,从人的触摸。我不知道为什么。
代码如下:
// 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];
});
}
对不起,我找到解决办法了。问题是打开连接到打印机和发送数据到打印机之间的速度太快了。
我把延迟放错位置了。所以当前要打印的步骤是:
- 打开蓝牙打印机连接
- 延迟
- 关闭打印机连接
正确的应该是:
- 打开蓝牙打印机连接 延迟
- 关闭打印机连接
我把答案放在这里,这样可以帮助其他有同样问题的人。
-(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];
});
}