public class TcpConnection extends ConnectionA implements IpAddressable, com.zebra.sdk.comm.internal.ConnectionI
package test.zebra.sdk.comm.examples;
import com.zebra.sdk.comm.*;
import com.zebra.sdk.common.card.containers.*;
import com.zebra.sdk.common.card.exceptions.ZebraCardException;
import com.zebra.sdk.common.card.printer.*;
import com.zebra.sdk.settings.SettingsException;
public class TcpConnectionExample {
public static void main(String[] args) throws Exception {
new TcpConnectionExample().getPrinterStatusOverTcp("1.2.3.4");
new TcpConnectionExample().getPrinterInfoUsingDnsName("PrinterName");
}
private void getPrinterStatusOverTcp(String theIpAddress) throws ConnectionException, SettingsException, ZebraCardException {
Connection connection = new TcpConnection(theIpAddress, 9100);
ZebraCardPrinter zebraCardPrinter = null;
try {
connection.open();
zebraCardPrinter = ZebraCardPrinterFactory.getInstance(connection);
PrinterStatusInfo printerStatusInfo = zebraCardPrinter.getPrinterStatus();
System.out.format("Status: %s%n", printerStatusInfo.status);
System.out.format("Alarm: %s (%s)%n", printerStatusInfo.alarmInfo.value, printerStatusInfo.alarmInfo.description);
System.out.format("Error: %s (%s)%n", printerStatusInfo.errorInfo.value, printerStatusInfo.errorInfo.description);
System.out.format("Total jobs: %s%n", printerStatusInfo.jobsTotal);
System.out.format("Pending jobs: %s%n", printerStatusInfo.jobsPending);
System.out.format("Active jobs: %s%n", printerStatusInfo.jobsActive);
System.out.format("Completed jobs: %s%n%n", printerStatusInfo.jobsComplete);
} catch (ConnectionException e) {
// Handle communications error here.
e.printStackTrace();
} finally {
// Release resources and close the connection
zebraCardPrinter.destroy();
connection.close();
}
}
private void getPrinterInfoUsingDnsName(String dnsName) throws ConnectionException, SettingsException, ZebraCardException {
Connection connection = new TcpConnection(dnsName, 9100);
ZebraCardPrinter zebraCardPrinter = null;
try {
connection.open();
zebraCardPrinter = ZebraCardPrinterFactory.getInstance(connection);
PrinterInfo printerInfo = zebraCardPrinter.getPrinterInformation();
System.out.format("Vendor: %s%n", printerInfo.vendor);
System.out.format("Model: %s%n", printerInfo.model);
System.out.format("SerialNumber: %s%n", printerInfo.serialNumber);
System.out.format("OEM Code: %s%n", printerInfo.oemCode);
System.out.format("Firmware Version: %s%n%n", printerInfo.firmwareVersion);
} catch (ConnectionException e) {
// Handle communications error here.
e.printStackTrace();
} finally {
// Release resources and close the connection
zebraCardPrinter.destroy();
connection.close();
}
}
}
| Modifier and Type | Field and Description |
|---|---|
static int |
DEFAULT_CPCL_TCP_PORT
The default TCP port for CPCL devices.
|
static int |
DEFAULT_ZPL_TCP_PORT
The default TCP port for ZPL devices.
|
| Constructor and Description |
|---|
TcpConnection(String address,
int port)
Initializes a new instance of the
TcpConnection class. |
TcpConnection(String address,
int port,
int maxTimeoutForRead,
int timeToWaitForMoreData)
Initializes a new instance of the
TcpConnection class. |
| Modifier and Type | Method and Description |
|---|---|
String |
getAddress()
Returns the address which was passed into the constructor.
|
com.zebra.sdk.comm.ConnectionReestablisher |
getConnectionReestablisher(long thresholdTime)
Returns a
ConnectionReestablisher which allows for easy recreation of a connection which may have
been closed. |
String |
getPortNumber()
Returns the port number which was passed into the constructor.
|
String |
getSimpleConnectionName()
Return the IP address as the description.
|
String |
toString()
Returns
TCP:[address]:[port number].The address and port number are the parameters which were passed into the constructor. |
addWriteLogStream, bytesAvailable, close, getManufacturer, getMaxDataToWrite, getMaxTimeoutForRead, getTimeToWaitForMoreData, isConnected, open, read, read, read, read, readChar, sendAndWaitForResponse, sendAndWaitForResponse, sendAndWaitForValidResponse, sendAndWaitForValidResponse, setMaxDataToWrite, setMaxTimeoutForRead, setReadTimeout, setTimeToWaitForMoreData, waitForData, write, write, writegetManufacturer, getMaxDataToWrite, read, read, setMaxDataToWrite, setReadTimeoutbytesAvailable, close, getMaxTimeoutForRead, getTimeToWaitForMoreData, isConnected, open, read, read, readChar, sendAndWaitForResponse, sendAndWaitForResponse, sendAndWaitForValidResponse, sendAndWaitForValidResponse, setMaxTimeoutForRead, setTimeToWaitForMoreData, waitForData, write, write, writepublic static final int DEFAULT_ZPL_TCP_PORT
public static final int DEFAULT_CPCL_TCP_PORT
public TcpConnection(String address, int port)
TcpConnection class. This constructor will use the default
timeouts for Connection.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.TcpConnection(String, int, int, int).address - the IP Address or DNS Hostname.port - the port number.public TcpConnection(String address, int port, int maxTimeoutForRead, int timeToWaitForMoreData)
TcpConnection class. This constructor will use the specified
timeouts for Connection.read(). 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.address - the IP Address or DNS Hostname.port - the 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.public String toString()
TCP:[address]:[port number].address and port number are the parameters which were passed into the constructor.toString in interface ConnectiontoString in class ObjectConnection.toString()public String getSimpleConnectionName()
getSimpleConnectionName in interface ConnectionConnection.getSimpleConnectionName()public String getAddress()
IpAddressablegetAddress in interface IpAddressableIpAddressable.getAddress()public String getPortNumber()
IpAddressablegetPortNumber in interface IpAddressableIpAddressable.getPortNumber()public com.zebra.sdk.comm.ConnectionReestablisher getConnectionReestablisher(long thresholdTime)
throws ConnectionException
ConnectionConnectionReestablisher which allows for easy recreation of a connection which may have
been closed.
getConnectionReestablisher in interface ConnectiongetConnectionReestablisher in class ConnectionAthresholdTime - how long the Connection reestablisher will wait before attempting to reconnection to the
printerConnectionException - if the ConnectionReestablisher could not be created.ConnectionA.getConnectionReestablisher(long thresholdTime)
© 2016 ZIH Corp. All Rights Reserved.