Package com.zebra.sdk.comm
Class MultichannelTcpConnection
Object
com.zebra.sdk.comm.MultichannelConnection
com.zebra.sdk.comm.MultichannelTcpConnection
- All Implemented Interfaces:
Connection,ConnectionWithWriteLogging
Establishes a Multichannel TCP connection to a device
package test.zebra.sdk.comm.examples;
import com.zebra.sdk.comm.Connection;
import com.zebra.sdk.comm.ConnectionException;
import com.zebra.sdk.comm.MultichannelTcpConnection;
import com.zebra.sdk.printer.PrinterStatus;
import com.zebra.sdk.printer.ZebraPrinterFactory;
import com.zebra.sdk.printer.ZebraPrinterLinkOs;
public class MultichannelTcpConnectionExample {
public static void main(String[] args) throws Exception {
new MultichannelTcpConnectionExample().simpleExample("1.2.3.4");
new MultichannelTcpConnectionExample().nonBlockingStatusReporting("1.2.3.4");
}
private void simpleExample(String theIpAddress) throws ConnectionException {
// Instantiate Multichannel connection for simultaneous printing and status reporting at given address
Connection thePrinterConn = new MultichannelTcpConnection(theIpAddress, MultichannelTcpConnection.DEFAULT_MULTICHANNEL_PRINTING_PORT, MultichannelTcpConnection.DEFAULT_MULTICHANNEL_STATUS_PORT);
try {
// Opens the connection - physical connection is established here.
thePrinterConn.open();
// Creates a Link-OS printing with the given connection
ZebraPrinterLinkOs linkOsPrinter = ZebraPrinterFactory.getLinkOsPrinter(thePrinterConn);
// This is sent over the printing channel (9100 by default)
linkOsPrinter.printConfigurationLabel();
// This is sent over the status channel (9200 by default)
PrinterStatus status = linkOsPrinter.getCurrentStatus();
System.out.println("The printer PAUSED state is : " + status.isPaused);
} catch (ConnectionException e) {
// Handle communications error here.
e.printStackTrace();
} finally {
// Close the connection to release resources.
thePrinterConn.close();
}
}
private void nonBlockingStatusReporting(String theIpAddress) throws ConnectionException {
// Instantiate Multichannel connection for simultaneous printing and status reporting at given address
Connection thePrinterConn = new MultichannelTcpConnection(theIpAddress, MultichannelTcpConnection.DEFAULT_MULTICHANNEL_PRINTING_PORT, MultichannelTcpConnection.DEFAULT_MULTICHANNEL_STATUS_PORT);
try {
// Opens the connection - physical connection is established here.
thePrinterConn.open();
// Creates a Link-OS printing with the given connection
ZebraPrinterLinkOs linkOsPrinter = ZebraPrinterFactory.getLinkOsPrinter(thePrinterConn);
// This is sent over the printing channel (9100 by default) and will block the printing channel until the
// label format is completely sent.
String labelFormatStartCommand = "^XA";
linkOsPrinter.sendCommand(labelFormatStartCommand);
String labelBody = "^FO20,20^A0N,25,25^FDThis is a ZPL test.^FS";
linkOsPrinter.sendCommand(labelBody);
// This is sent over the status channel (9200 by default) and will return immediately even though the
// printing channel is in use.
// If a TcpConnection were used instead of a MultichannelTcpConnection, this would not be possible.
PrinterStatus status = linkOsPrinter.getCurrentStatus();
System.out.println("The printer PAUSED state is : " + status.isPaused);
// Send the end of label command to finish and print the label.
String labelFormatEndCommand = "^XZ";
linkOsPrinter.sendCommand(labelFormatEndCommand);
} catch (ConnectionException e) {
// Handle communications error here.
e.printStackTrace();
} finally {
// Close the connection to release resources.
thePrinterConn.close();
}
}
}
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe default Multichannel printing port for Link-OS devices.static final intThe default Multichannel status port for Link-OS devices. -
Constructor Summary
ConstructorsConstructorDescriptionMultichannelTcpConnection(DiscoveredPrinter discoveredPrinter) Initializes a new instance of theMultichannelTcpConnectionclass.MultichannelTcpConnection(DiscoveredPrinter discoveredPrinter, int maxTimeoutForRead, int timeToWaitForMoreData) Initializes a new instance of theMultichannelTcpConnectionclass.MultichannelTcpConnection(DiscoveredPrinter discoveredPrinter, int printingChannelMaxTimeoutForRead, int printingChannelTimeToWaitForMoreData, int statusChannelMaxTimeoutForRead, int statusChannelTimeToWaitForMoreData) Initializes a new instance of theMultichannelTcpConnectionclass.MultichannelTcpConnection(String ipAddress, int printingPort, int statusPort) Initializes a new instance of theMultichannelTcpConnectionclass.MultichannelTcpConnection(String ipAddress, int printingPort, int statusPort, int maxTimeoutForRead, int timeToWaitForMoreData) Initializes a new instance of theMultichannelTcpConnectionclass.MultichannelTcpConnection(String ipAddress, int printingPort, int statusPort, int printingChannelMaxTimeoutForRead, int printingChannelTimeToWaitForMoreData, int statusChannelMaxTimeoutForRead, int statusChannelTimeToWaitForMoreData) Initializes a new instance of theMultichannelTcpConnectionclass. -
Method Summary
Modifier and TypeMethodDescriptiongetConnectionReestablisher(long thresholdTime) Returns aConnectionReestablisherwhich allows for easy recreation of a connection which may have been closed.Return the IP address as the description.toString()ReturnsTCP_MULTI:[address]:[port number].
Theaddressandport numberare the parameters which were passed into the constructor.Methods inherited from class com.zebra.sdk.comm.MultichannelConnection
addWriteLogStream, bytesAvailable, close, closePrintingChannel, closeStatusChannel, getMaxTimeoutForRead, getPrintingChannel, getStatusChannel, getTimeToWaitForMoreData, isConnected, open, openPrintingChannel, openStatusChannel, read, read, readChar, sendAndWaitForResponse, sendAndWaitForResponse, sendAndWaitForValidResponse, sendAndWaitForValidResponse, setMaxTimeoutForRead, setTimeToWaitForMoreData, waitForData, write, write, write
-
Field Details
-
DEFAULT_MULTICHANNEL_PRINTING_PORT
public static final int DEFAULT_MULTICHANNEL_PRINTING_PORTThe default Multichannel printing port for Link-OS devices.- See Also:
-
DEFAULT_MULTICHANNEL_STATUS_PORT
public static final int DEFAULT_MULTICHANNEL_STATUS_PORTThe default Multichannel status port for Link-OS devices.- See Also:
-
-
Constructor Details
-
MultichannelTcpConnection
Initializes a new instance of theMultichannelTcpConnectionclass. This constructor will use the default timeouts forConnection.read(). 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:
MultichannelTcpConnection(DiscoveredPrinter, int, int)orMultichannelTcpConnection(DiscoveredPrinter, int, int, int, int).- Parameters:
discoveredPrinter- the discovered printer.
-
MultichannelTcpConnection
public MultichannelTcpConnection(DiscoveredPrinter discoveredPrinter, int maxTimeoutForRead, int timeToWaitForMoreData) Initializes a new instance of theMultichannelTcpConnectionclass. This constructor will use the specified timeouts forConnection.read()for both channels. The timeout is a maximum ofmaxTimeoutForReadmilliseconds for any data to be received. If no more data is available aftertimeToWaitForMoreDatamilliseconds the read operation is assumed to be complete. If you wish to specify different timeouts for each channel, useMultichannelTcpConnection(DiscoveredPrinter, int, int, int, int).- Parameters:
discoveredPrinter- the discovered printer.maxTimeoutForRead- the maximum time, in milliseconds, to wait for any data to be received.timeToWaitForMoreData- the maximum time, in milliseconds, to wait in-between reads after the initial read.
-
MultichannelTcpConnection
public MultichannelTcpConnection(DiscoveredPrinter discoveredPrinter, int printingChannelMaxTimeoutForRead, int printingChannelTimeToWaitForMoreData, int statusChannelMaxTimeoutForRead, int statusChannelTimeToWaitForMoreData) Initializes a new instance of theMultichannelTcpConnectionclass. This constructor will use the specified timeouts forConnection.read()for the channels. The timeout is a maximum ofprintingChannelMaxTimeoutForRead/statusChannelMaxTimeoutForReadmilliseconds for any data to be received on the printing/status channels respectively. If no more data is available afterprintingChannelTimeToWaitForMoreData/statusChannelTimeToWaitForMoreDatamilliseconds on the printing/status channels respectively the read operation is assumed to be complete.- Parameters:
discoveredPrinter- the discovered printer.printingChannelMaxTimeoutForRead- the maximum time, in milliseconds, to wait for any data to be received on the printing channel.printingChannelTimeToWaitForMoreData- the maximum time, in milliseconds, to wait in-between reads after the initial read on the printing channel.statusChannelMaxTimeoutForRead- the maximum time, in milliseconds, to wait for any data to be received on the status channel.statusChannelTimeToWaitForMoreData- the maximum time, in milliseconds, to wait in-between reads after the initial read on the status channel.
-
MultichannelTcpConnection
Initializes a new instance of theMultichannelTcpConnectionclass. This constructor will use the default timeouts forConnection.read(). 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:
MultichannelTcpConnection(String, int, int, int,int)orMultichannelTcpConnection(String, int, int, int, int, int, int).- Parameters:
ipAddress- the IP Address or DNS Hostname.printingPort- the printing port number.statusPort- the status port number.
-
MultichannelTcpConnection
public MultichannelTcpConnection(String ipAddress, int printingPort, int statusPort, int maxTimeoutForRead, int timeToWaitForMoreData) Initializes a new instance of theMultichannelTcpConnectionclass. This constructor will use the specified timeouts forConnection.read()for both channels. The timeout is a maximum ofmaxTimeoutForReadmilliseconds for any data to be received. If no more data is available aftertimeToWaitForMoreDatamilliseconds the read operation is assumed to be complete. If you wish to specify different timeouts for each channel, useMultichannelTcpConnection(String, int, int, int, int, int, int).- Parameters:
ipAddress- the IP Address or DNS Hostname.printingPort- the printing port number.statusPort- the status port number.maxTimeoutForRead- the maximum time, in milliseconds, to wait for any data to be received.timeToWaitForMoreData- the maximum time, in milliseconds, to wait in-between reads after the initial read.
-
MultichannelTcpConnection
public MultichannelTcpConnection(String ipAddress, int printingPort, int statusPort, int printingChannelMaxTimeoutForRead, int printingChannelTimeToWaitForMoreData, int statusChannelMaxTimeoutForRead, int statusChannelTimeToWaitForMoreData) Initializes a new instance of theMultichannelTcpConnectionclass. This constructor will use the specified timeouts forConnection.read()for the channels. The timeout is a maximum ofprintingChannelMaxTimeoutForRead/statusChannelMaxTimeoutForReadmilliseconds for any data to be received on the printing/status channels respectively. If no more data is available afterprintingChannelTimeToWaitForMoreData/statusChannelTimeToWaitForMoreDatamilliseconds on the printing/status channels respectively the read operation is assumed to be complete.- Parameters:
ipAddress- the IP Address or DNS Hostname.printingPort- the printing port number.statusPort- the status port number.printingChannelMaxTimeoutForRead- the maximum time, in milliseconds, to wait for any data to be received on the printing channel.printingChannelTimeToWaitForMoreData- the maximum time, in milliseconds, to wait in-between reads after the initial read on the printing channel.statusChannelMaxTimeoutForRead- the maximum time, in milliseconds, to wait for any data to be received on the status channel.statusChannelTimeToWaitForMoreData- the maximum time, in milliseconds, to wait in-between reads after the initial read on the status channel.
-
-
Method Details
-
getConnectionReestablisher
public ConnectionReestablisher getConnectionReestablisher(long thresholdTime) throws ConnectionException Description copied from interface:ConnectionReturns aConnectionReestablisherwhich allows for easy recreation of a connection which may have been closed.- This call should be made while there is still an active connection to the printer (prior to issuing a command which will make the printer non-responsive).
- Specified by:
getConnectionReestablisherin interfaceConnection- Overrides:
getConnectionReestablisherin classMultichannelConnection- Parameters:
thresholdTime- how long the Connection reestablisher will wait before attempting to reconnection to the printer- Returns:
- ConnectionReestablisher
- Throws:
ConnectionException- if the ConnectionReestablisher could not be created.- See Also:
-
toString
ReturnsTCP_MULTI:[address]:[port number].
Theaddressandport numberare the parameters which were passed into the constructor.- Specified by:
toStringin interfaceConnection- Overrides:
toStringin classObject- Returns:
- the connection description string.
- See Also:
-
getSimpleConnectionName
Return the IP address as the description.- Returns:
- a human-readable description of the connection.
- See Also:
-