public class DriverPrinterConnection extends ConnectionA
package test.zebra.sdk.comm.examples;
import com.zebra.sdk.comm.Connection;
import com.zebra.sdk.comm.ConnectionException;
import com.zebra.sdk.comm.DriverPrinterConnection;
public class DriverPrinterConnectionExample {
public static void main(String[] args) throws Exception {
DriverPrinterConnectionExample example = new DriverPrinterConnectionExample();
example.sendZplOverUsb("ZDesigner QLn320");
example.sendCpclOverUsb("ZDesigner QLn320");
}
private void sendZplOverUsb(String printerName) throws ConnectionException {
// Instantiate connection for ZPL USB port for given printer name
Connection thePrinterConn = new DriverPrinterConnection(printerName);
try {
// Open the connection - physical connection is established here.
thePrinterConn.open();
// This example prints "This is a ZPL test." near the top of the label.
String zplData = "^XA^FO20,20^A0N,25,25^FDThis is a ZPL test.^FS^XZ";
// Send the data to printer as a byte array.
thePrinterConn.write(zplData.getBytes());
} catch (ConnectionException e) {
// Handle communications error here.
e.printStackTrace();
} finally {
// Close the connection to release resources.
thePrinterConn.close();
}
}
private void sendCpclOverUsb(String printerName) throws ConnectionException {
// Instantiate connection for CPCL USB port for given printer name
Connection thePrinterConn = new DriverPrinterConnection(printerName);
try {
// Open the connection - physical connection is established here.
thePrinterConn.open();
// This example prints "This is a CPCL test." near the top of the label.
String cpclData = "! 0 200 200 210 1\r\n" + "TEXT 4 0 30 40 This is a CPCL test.\r\n" + "FORM\r\n" + "PRINT\r\n";
// Send the data to printer as a byte array.
thePrinterConn.write(cpclData.getBytes());
} catch (ConnectionException e) {
// Handle communications error here.
e.printStackTrace();
} finally {
// Close the connection to release resources.
thePrinterConn.close();
}
}
}
Constructor and Description |
---|
DriverPrinterConnection(String printerName)
Initializes a new instance of the
DriverPrinterConnection class. |
DriverPrinterConnection(String printerName,
int maxTimeoutForRead,
int timeToWaitForMoreData)
Initializes a new instance of the
DriverPrinterConnection class. |
Modifier and Type | Method and Description |
---|---|
int |
bytesAvailable()
Returns the number of bytes available on the connection.
|
void |
close()
Closes this connection and releases any system resources associated with the connection.
|
ConnectionReestablisher |
getConnectionReestablisher(long thresholdTime)
Returns a
ConnectionReestablisher which allows for easy recreation of a connection which may have
been closed. |
String |
getPrinterName()
Returns the name of the printer.
|
String |
getSimpleConnectionName()
Return the printer name as the description.
|
void |
open()
Opens the connection to a device.
|
byte[] |
read()
Reads all the available data from the connection.
|
int |
readChar()
Reads the next byte of data from the connection, similar to a Java InputStream.
|
String |
toString()
See the classes which implement this method for the format of the description string.
|
void |
write(byte[] data,
int offset,
int length)
Writes
length bytes from data starting at offset . |
addWriteLogStream, getMaxTimeoutForRead, getTimeToWaitForMoreData, isConnected, read, sendAndWaitForResponse, sendAndWaitForResponse, sendAndWaitForValidResponse, sendAndWaitForValidResponse, setMaxTimeoutForRead, setTimeToWaitForMoreData, waitForData, write, write
public DriverPrinterConnection(String printerName) throws ConnectionException
DriverPrinterConnection
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.DriverPrinterConnection(String, int, int)
.printerName
- the printer nameConnectionException
public DriverPrinterConnection(String printerName, int maxTimeoutForRead, int timeToWaitForMoreData) throws ConnectionException
DriverPrinterConnection
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.printerName
- the printer namemaxTimeoutForRead
- maximum milliseconds to wait for the initial data to be receivedtimeToWaitForMoreData
- maximum milliseconds to wait for additional data to be receivedConnectionException
public String getPrinterName()
public void open() throws ConnectionException
Connection
open
method is called when this connection has already been
opened, this call is ignored. When a handle to the connection is no longer needed, you must call Connection.close()
to free up system resources.open
in interface Connection
open
in class ConnectionA
ConnectionException
- if the connection cannot be established.Connection.open()
public void close() throws ConnectionException
Connection
close
in interface Connection
close
in class ConnectionA
ConnectionException
- if an I/O error occurs.Connection.close()
public byte[] read() throws ConnectionException
Connection
byte[] data = printerConnection.read();
read
in interface Connection
read
in class ConnectionA
ConnectionException
- if an I/O error occurs.
Performing reads using DriverPrinterConnection and UsbConnection concurrently could cause indeterminate reads
public int readChar() throws ConnectionException
Connection
int singleCharacter = printerConnection.readChar();
readChar
in interface Connection
readChar
in class ConnectionA
ConnectionException
- if an I/O error occurs.Connection.readChar()
public void write(byte[] data, int offset, int length) throws ConnectionException
Connection
length
bytes from data
starting at offset
. The connection must be
open before this method is called. If write
is called when a connection is closed, a
ConnectionException
is thrown.write
in interface Connection
write
in class ConnectionA
data
- the data.offset
- the start offset in the data
.length
- number of bytes to write.ConnectionException
- if an I/O error occurs.Connection.write(byte[], int, int)
public int bytesAvailable() throws ConnectionException
bytesAvailable
in interface Connection
bytesAvailable
in class ConnectionA
ConnectionException
- if an I/O error occurs.Connection.bytesAvailable()
public String toString()
Connection
toString
in interface Connection
toString
in class Object
Object.toString()
public String getSimpleConnectionName()
Connection.getSimpleConnectionName()
public ConnectionReestablisher getConnectionReestablisher(long thresholdTime) throws ConnectionException
Connection
ConnectionReestablisher
which allows for easy recreation of a connection which may have
been closed.getConnectionReestablisher
in interface Connection
getConnectionReestablisher
in class ConnectionA
thresholdTime
- 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.