Will Ullrich
Will Ullrich

Reputation: 2238

TCP Socket Connection Printing Blanks

I have a label printer, a Brady i3300, that I am connecting to from my iPad using a tcp socket (using the CocoaAsyncSocket framework). Each time I send data to the printer, it prints, but the label is always blank.

Process

First, I run connectToPrinter, and the didConnectToHost delegate fires. After the successful connection, I call printLabelToPrinter, where the printer is sent the data. The didWriteDataWithTag delegate is called just after this, the printer begins / finishes printing, but the label is blank.

It has not mattered thus far what content is in the data file (explained below), so it feels like a connection issue...Regardless, the printer seems to be receiving the request without any data along with it.

Why is the printer printing blank labels?


Code & Files

Here is my connection setup:

- (void)connectToPrinter {

    // Create the socket
    self.socket = [GCDAsyncSocket.alloc initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
    
    // Connect to the printer via IP and port
    NSError *err = nil;
    if (![self.socket connectToHost:@"10.0.0.147" onPort:9100 error:&err]) {
        // Something went wrong!
        NSLog(@"I goofed: %@", err);
    }

}

- (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port {
    
    NSLog(@"connected to host!: %@", host);
    [self printLabelToPrinter];

}

Note: the .zpl file ref below contains a ZPL printing script, one of the supported languages of Brady printers

- (void)printLabelToPrinter {

    // Get the ZPL file and convert it over to data
    NSString *filePath = [NSBundle.mainBundle pathForResource:@"label" ofType:@".zpl"];
    NSData *data = [NSFileManager.defaultManager contentsAtPath:filePath];

    // Write the data to the printer
    [self.socket writeData:data withTimeout:10 tag:1];

}

- (void)socket:(GCDAsyncSocket *)sock didWriteDataWithTag:(long)tag {
    NSLog(@"did write data: %ld", tag);
}

Here is the .zpl file

^XA
^CF0,60
^FO50,50^GB100,100,100^FS
^FO75,75^FR^GB100,100,100^FS
^FO50,300
^FO504,300^FDTest
^XZ

Upvotes: 1

Views: 327

Answers (0)

Related Questions