2016-09-07 4 views
0

Zebraプリンタに奇妙な問題があります。大きな画像では、キューから1つずつ印刷する項目を取得するコードがあります。したがって、印刷が開始され、キューに3つのアイテムがある場合、コードはキュー内の最初のデータをループして取り出し、プリンタに送信し、キュー内の最初のデータを削除します。並べ替えのような並べ替え。Zebraプリンタは、ユーザがタップした場合、連続した複数のラベルのみを印刷します。

問題は、ループしてデータをプリンタに直接送信するコードの場合、プリンタは最初の項目のみを印刷することです。 NSLogは、1つのアイテムごとに、プリンタ接続のオープン、データの送信、印刷の成功、プリンタの接続が閉じられたことを示していますが、次の項目はなくなりました。

しかし、コードが1つのラベルを印刷するたびに「次のラベルを印刷するためにOKを押してください」というメッセージボックスが表示されたら、ユーザーは[OK]ボタンをタップして2番目と残りのボタンをタップするたびにラベルを表示します。

これをエミュレートしようとしました。私は "ボタンをプログラムでプッシュ"するタイマを使用しようとしました[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]; 

    }); 
} 

答えて

0

申し訳ありませんが、私は解決策を見つけました。問題は、プリンタへの接続を開いてデータをプリンタに送信するまでには速すぎるということです。私は少し遅れていたが、遅れを間違った位置に置いた。

だから、現在、印刷するための手順は以下のとおりです。

  1. オープンBluetoothプリンタ接続
  2. データ
  3. に遅延
  4. 閉じるプリンタの接続正しいものがあるべき

を送信します。

  1. オープンBluetoothプリンタ接続
  2. 遅延
  3. はデータ
  4. 閉じるプリンタ接続
ここ

これは同じ問題で他の人々を助けることができるので、私は答えを置くを送信します。

-(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]; 

    }); 
} 
関連する問題