Package com.zebra.sdk.comm
Class DriverPrinterConnection
Object
com.zebra.sdk.comm.ConnectionA
com.zebra.sdk.comm.DriverPrinterConnection
- All Implemented Interfaces:
Connection,ConnectionWithWriteLogging
Establishes a USB connection to a printer. This class is only supported on Windows PC platforms.
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 Summary
ConstructorsConstructorDescriptionDriverPrinterConnection(String printerName) Initializes a new instance of theDriverPrinterConnectionclass.DriverPrinterConnection(String printerName, int maxTimeoutForRead, int timeToWaitForMoreData) Initializes a new instance of theDriverPrinterConnectionclass. -
Method Summary
Modifier and TypeMethodDescriptionintReturns the number of bytes available on the connection.voidclose()Closes this connection and releases any system resources associated with the connection.getConnectionReestablisher(long thresholdTime) Returns aConnectionReestablisherwhich allows for easy recreation of a connection which may have been closed.Returns the name of the printer.Return the printer name as the description.voidopen()Opens the connection to a device.byte[]read()Reads all the available data from the connection.intreadChar()Reads the next byte of data from the connection, similar to a Java InputStream.toString()See the classes which implement this method for the format of the description string.voidwrite(byte[] data, int offset, int length) Writeslengthbytes fromdatastarting atoffset.Methods inherited from class com.zebra.sdk.comm.ConnectionA
addWriteLogStream, getManufacturer, getMaxDataToWrite, getMaxTimeoutForRead, getTimeToWaitForMoreData, isConnected, read, read, read, sendAndWaitForResponse, sendAndWaitForResponse, sendAndWaitForValidResponse, sendAndWaitForValidResponse, setMaxDataToWrite, setMaxTimeoutForRead, setReadTimeout, setTimeToWaitForMoreData, waitForData, write, write
-
Constructor Details
-
DriverPrinterConnection
Initializes a new instance of theDriverPrinterConnectionclass. 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:
DriverPrinterConnection(String, int, int).- Parameters:
printerName- the printer name- Throws:
ConnectionException
-
DriverPrinterConnection
public DriverPrinterConnection(String printerName, int maxTimeoutForRead, int timeToWaitForMoreData) throws ConnectionException Initializes a new instance of theDriverPrinterConnectionclass. This constructor will use the specified timeouts forConnection.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.- Parameters:
printerName- the printer namemaxTimeoutForRead- maximum milliseconds to wait for the initial data to be receivedtimeToWaitForMoreData- maximum milliseconds to wait for additional data to be received- Throws:
ConnectionException
-
-
Method Details
-
getPrinterName
Returns the name of the printer.- Returns:
- The name of the printer.
-
open
Description copied from interface:ConnectionOpens the connection to a device. If theopenmethod 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 callConnection.close()to free up system resources.- Specified by:
openin interfaceConnection- Overrides:
openin classConnectionA- Throws:
ConnectionException- if the connection cannot be established.- See Also:
-
close
Description copied from interface:ConnectionCloses this connection and releases any system resources associated with the connection. If the connection is already closed then invoking this method has no effect.- Specified by:
closein interfaceConnection- Overrides:
closein classConnectionA- Throws:
ConnectionException- if an I/O error occurs.- See Also:
-
read
Description copied from interface:ConnectionReads all the available data from the connection. This call is non-blocking.byte[] data = printerConnection.read();
- Specified by:
readin interfaceConnection- Overrides:
readin classConnectionA- Returns:
- the bytes read from the connection.
- Throws:
ConnectionException- if an I/O error occurs.- See Also:
-
readChar
Description copied from interface:ConnectionReads the next byte of data from the connection, similar to a Java InputStream. The value byte is returned as an int in the range of 0 to 255. If no byte is available on the connection the value -1 is returned.int singleCharacter = printerConnection.readChar();
- Specified by:
readCharin interfaceConnection- Overrides:
readCharin classConnectionA- Returns:
- the next byte from the connection
- Throws:
ConnectionException- if an I/O error occurs.- See Also:
-
write
Description copied from interface:ConnectionWriteslengthbytes fromdatastarting atoffset. The connection must be open before this method is called. Ifwriteis called when a connection is closed, aConnectionExceptionis thrown.- Specified by:
writein interfaceConnection- Overrides:
writein classConnectionA- Parameters:
data- the data.offset- the start offset in thedata.length- number of bytes to write.- Throws:
ConnectionException- if an I/O error occurs.- See Also:
-
bytesAvailable
Returns the number of bytes available on the connection. Note that this is a blocking call.- Specified by:
bytesAvailablein interfaceConnection- Overrides:
bytesAvailablein classConnectionA- Returns:
- estimated number of bytes available.
- Throws:
ConnectionException- if an I/O error occurs.- See Also:
-
toString
Description copied from interface:ConnectionSee the classes which implement this method for the format of the description string.- Specified by:
toStringin interfaceConnection- Overrides:
toStringin classObject- Returns:
- the connection description string.
- See Also:
-
getSimpleConnectionName
Return the printer name as the description.- Returns:
- a human-readable description of the connection.
- See Also:
-
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 classConnectionA- 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:
-