ZSDK_API  1.5.1049
 All Classes Functions Enumerations Enumerator Properties Pages
TcpPrinterConnection Class Reference

Establishes a TCP connection to a printer. More...

#import <TcpPrinterConnection.h>

Inheritance diagram for TcpPrinterConnection:
<ZebraPrinterConnection>

Instance Methods

(id) - initWithAddress:andWithPort:
 Initializes a new instance of the TcpPrinterConnection class.
 
(id) - initWithAddress:withPort:withMaxTimeoutForRead:andWithTimeToWaitForMoreData:
 Initializes a new instance of the TcpPrinterConnection class.
 
(void) - setMaxTimeoutForOpen:
 The default time, in milliseconds, to wait for a TCP connection to open.
 
- Instance Methods inherited from <ZebraPrinterConnection>
(NSString *) - toString
 See the classes which implement this method for the format of the description string.
 
(NSInteger) - getMaxTimeoutForRead
 Returns the maximum time, in milliseconds, to wait for any data to be received.
 
(NSInteger) - getTimeToWaitForMoreData
 Returns the maximum time, in milliseconds, to wait between reads after the initial read.
 
(void) - setMaxTimeoutForRead:
 Set the maximum time, in milliseconds, to wait for any data to be received.
 
(void) - setTimeToWaitForMoreData:
 Set the maximum time, in milliseconds, to wait in-between reads after the initial read.
 
(BOOL) - isConnected
 Returns YES if the connection is open.
 
(BOOL) - open
 Opens the connection to a device.
 
(void) - close
 Closes this connection and releases any system resources associated with the connection.
 
(NSInteger) - write:error:
 Writes the number of bytes from data to the connection.
 
(NSInteger) - write:withOffset:andWithLength:error:
 Writes length bytes from data starting at offset.
 
(NSInteger) - writeStream:error:
 Writes all available bytes from the data source to the connection.
 
(NSData *) - read:
 Reads all the available data from the connection.
 
(void) - read:error:
 Reads all the available data from the connection and write it to destinationStream.
 
(NSString *) - getSimpleConnectionName
 Return a human-readable description of the connection.
 
(BOOL) - hasBytesAvailable
 Returns YES if at least one byte is available for reading from this connection.
 
(void) - waitForData:
 Causes the currently executing thread to sleep until hasBytesAvailable equals YES, or for a maximum of maxTimeout milliseconds.
 
(NSData *) - sendAndWaitForResponse:withResponseValidator:withError:
 Sends data and returns the response data.
 
(NSData *) - sendAndWaitForResponse:withMaxTimeoutForRead:andWithTimeToWaitForMoreData:withResponseValidator:andWithError:
 Sends data and returns the response data.
 

Class Methods

(NSInteger) + DEFAULT_MAX_TIMEOUT_FOR_READ
 The default time, in milliseconds, to wait for any data to be received.
 
(NSInteger) + DEFAULT_TIME_TO_WAIT_FOR_MORE_DATA
 The default time, in milliseconds, to wait in-between reads after the initial read.
 

Detailed Description

Establishes a TCP connection to a printer.

Remarks
Each TcpPrinterConnection object should only be used on a single thread. The read and write streams are bound to the originating thread. Accessing the read and write streams on a different thread results in undefined behavior.
#import "TcpPrinterConnection.h"
#import "ZebraPrinterConnection.h"
#import <UIKit/UIKit.h>
-(void)sendZplOverTcp:(NSString*)theIpAddress {
// Instantiate connection for ZPL TCP port at given address.
id<ZebraPrinterConnection, NSObject> thePrinterConn = [[TcpPrinterConnection alloc] initWithAddress:theIpAddress andWithPort:9100];
// Open the connection - physical connection is established here.
BOOL success = [thePrinterConn open];
// This example prints "This is a ZPL test." near the top of the label.
NSString *zplData = @"^XA^FO20,20^A0N,25,25^FDThis is a ZPL test.^FS^XZ";
NSError *error = nil;
// Send the data to printer as a byte array.
success = success && [thePrinterConn write:[zplData dataUsingEncoding:NSUTF8StringEncoding] error:&error];
if (success != YES || error != nil) {
UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:@"Error" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[errorAlert show];
[errorAlert release];
}
// Close the connection to release resources.
[thePrinterConn close];
[thePrinterConn release];
}
-(void)sendCpclOverTcp:(NSString*) theIpAddress {
// Instantiate connection for CPCL TCP port at given address.
id<ZebraPrinterConnection,NSObject> thePrinterConn = [[TcpPrinterConnection alloc] initWithAddress:theIpAddress andWithPort:6101];
// Open the connection - physical connection is established here.
BOOL success = [thePrinterConn open];
// This example prints "This is a CPCL test." near the top of the label.
NSString *cpclData = @"! 0 200 200 210 1\r\nTEXT 4 0 30 40 This is a CPCL test.\r\nFORM\r\nPRINT\r\n";
NSError *error = nil;
// Send the data to printer as a byte array.
success = success && [thePrinterConn write:[cpclData dataUsingEncoding:NSUTF8StringEncoding] error:&error];
if (success != YES || error != nil) {
UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:@"Error" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[errorAlert show];
[errorAlert release];
}
// Close the connection to release resources.
[thePrinterConn close];
[thePrinterConn release];
}
-(void)sampleWithGCD {
//Dispatch this task to the default queue
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^ {
// Instantiate connection to Tcp port at a given address
id<ZebraPrinterConnection, NSObject> thePrinterConn = [[TcpPrinterConnection alloc] initWithAddress:@"192.168.1.5" andWithPort:9100];
// Open the connection - physical connection is established here.
BOOL success = [thePrinterConn open];
// This example prints "This is a ZPL test." near the top of the label.
NSString *zplData = @"^XA^FO20,20^A0N,25,25^FDThis is a ZPL test.^FS^XZ";
NSError *error = nil;
// 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) {
UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:@"Error" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[errorAlert show];
[errorAlert release];
}
});
// Close the connection to release resources.
[thePrinterConn close];
[thePrinterConn release];
});
}

Method Documentation

- (id) initWithAddress: (NSString *)  anAddress
andWithPort: (NSInteger)  aPort 

Initializes a new instance of the TcpPrinterConnection class.

This constructor will use the default timeouts for read: (ZebraPrinterConnection-p). The default timeout is a maximum of 5 seconds for any data to be received. If no more data is available after 500 milliseconds the read operation is assumed to be complete.
To specify timeouts other than the defaults, use:
initWithAddress:withPort:withMaxTimeoutForRead:andWithTimeToWaitForMoreData:

Parameters
anAddressThe IP Address or DNS Hostname.
aPortThe port number.
- (id) initWithAddress: (NSString *)  anAddress
withPort: (NSInteger)  aPort
withMaxTimeoutForRead: (NSInteger)  aMaxTimeoutForRead
andWithTimeToWaitForMoreData: (NSInteger)  aTimeToWaitForMoreData 

Initializes a new instance of the TcpPrinterConnection class.

This constructor will use the specified timeouts for read: (ZebraPrinterConnection-p). The timeout is a maximum of maxTimeoutForRead milliseconds for any data to be received. If no more data is available after timeToWaitForMoreData milliseconds the read operation is assumed to be complete.

Parameters
anAddressThe IP Address or DNS Hostname.
aPortThe port number.
aMaxTimeoutForReadThe maximum time, in milliseconds, to wait for any data to be received.
aTimeToWaitForMoreDataThe maximum time, in milliseconds, to wait in-between reads after the initial read.
- (void) setMaxTimeoutForOpen: (int)  aTimeout

The default time, in milliseconds, to wait for a TCP connection to open.

The default is set to the platform default.

Parameters
aTimeoutTime to wait in milliseconds

The documentation for this class was generated from the following file: