public class ConnectionBuilder extends Object
The description string may be of the explicit forms: "TCP:192.168.1.4:6101" -- creates a TCP connection to the device with IP address 192.168.1.4 on port 6101 "TCP:192.168.1.4" -- creates a TCP connection to the device with IP address 192.168.1.4 on default port 9100 "TCP:dnsName:6101" -- creates a TCP connection to the device with 'dnsName' on port 6101 "TCP:dnsName" -- creates a TCP connection to the device with 'dnsName' on default port 9100 "TCP_MULTI:192.168.1.4" -- creates a Multichannel TCP connection to the device with '192.168.1.4' using the default ports for both the printing channel (9100) and the status channel(9200) "TCP_MULTI:192.168.1.4:1234" -- creates a Multichannel TCP connection to the device with '192.168.1.4' using the given port for the printing channel (1234) and the default port for the status channel(9200) "TCP_MULTI:192.168.1.4:1234:5678" -- creates a Multichannel TCP connection to the device with '192.168.1.4' using the given ports for the printing channel (1234) and the status channel(5678) "TCP_MULTI:dnsName:1234:5678" -- creates a Multichannel TCP connection to the device with 'dnsName' using the given ports for the printing channel (1234) and the status channel(5678) "TCP_STATUS:192.168.1.4:1234" -- creates a TCP status only connection to the device with IP address 192.168.1.4 on port 1234 "TCP_STATUS:192.168.1.4" -- creates a TCP status only connection to the device with IP address 192.168.1.4 on the default status port 9200 "REMOTE:serialNumber" -- creates a remote connection to the device with 'serialNumber' which is accessible via the RMI Server running on the default RMI port (11995). The printer must be registered for use with a Zebra Weblink server. "REMOTE:serialNumber:rmiPort" -- creates a remote connection to the device with 'serialNumber' which is accessible via the RMI Server running on port 'rmiPort'. The printer must be registered for use with a Zebra Weblink server. "REMOTE_MULTI:serialNumber:rmiPort" -- creates a multichannel remote connection to the device with 'serialNumber' which is accessible via the RMI Server running on port 'rmiPort'. The printer must be registered for use with a Zebra Weblink server. "REMOTE_STATUS:serialNumber:rmiPort" -- creates a status only remote connection to the device with 'serialNumber' which is accessible via the RMI Server running on port 'rmiPort'. The printer must be registered for use with a Zebra Weblink server. "USB:deviceName" -- creates a USB connection (through the ZebraDesigner driver) to the device with printer name 'deviceName' "USB_DIRECT:deviceName" -- creates a USB connection to the device with 'device.unique_id' or 'device.product_name' equal to 'deviceName'Generic text may also be used to attempt to specify a device. For example a description string of "genericText" will attempt to connect to a device using the following priority:
package test.zebra.sdk.comm.examples;
import com.zebra.sdk.comm.Connection;
import com.zebra.sdk.comm.ConnectionBuilder;
import com.zebra.sdk.comm.ConnectionException;
import com.zebra.sdk.printer.PrinterStatus;
import com.zebra.sdk.printer.ZebraPrinterFactory;
import com.zebra.sdk.printer.ZebraPrinterLinkOs;
import com.zebra.sdk.printer.discovery.DiscoveredUsbPrinter;
import com.zebra.sdk.printer.discovery.UsbDiscoverer;
public class ConnectionBuilderExample {
public static void usbDriverlessTest() throws ConnectionException {
System.out.println("Discovered USB driver list:\r\n");
for (DiscoveredUsbPrinter printer : UsbDiscoverer.getZebraUsbPrinters()) {
System.out.println(printer);
}
System.out.println("End USB driver list\r\n");
Connection imz = ConnectionBuilder.build("40J132100244");
try {
imz.open();
byte[] hi_return = imz.sendAndWaitForResponse("~HI".getBytes(), 5000, 10000, "V");
System.out.println(new String(hi_return));
} finally {
imz.close();
}
}
public static void main(String[] args) throws Exception {
usbDriverlessTest();
new ConnectionBuilderExample().nonBlockingStatusReportingOverMultichannel("1.2.3.4");
new ConnectionBuilderExample().sendZplOverTcp("1.2.3.4");
new ConnectionBuilderExample().sendZplOverUsb("ZDesigner GK420t");
}
private void nonBlockingStatusReportingOverMultichannel(String theIpAddress) throws ConnectionException {
// Instantiate Multichannel connection for simultaneous printing and status reporting at given address
Connection thePrinterConn = ConnectionBuilder.build("TCP_MULTI:" + theIpAddress + ":9100:9200");
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();
}
}
private void sendZplOverTcp(String theIpAddress) throws ConnectionException {
// Instantiate connection for ZPL TCP port at given address
Connection thePrinterConn = ConnectionBuilder.build("TCP:" + theIpAddress + ":9100");
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 sendZplOverUsb(String usbDriverName) throws ConnectionException {
// Instantiate USB connection for ZPL printer through its driver
Connection thePrinterConn = ConnectionBuilder.build("USB:" + usbDriverName);
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();
}
}
}
Modifier and Type | Method and Description |
---|---|
static void |
addConnectionType(Class<? extends Connection> c)
Add a connection type to the ConnectionBuilder
|
static Connection |
build(String descriptionString)
Creates a Connection type based on the contents of
descriptionString . |
public static void addConnectionType(Class<? extends Connection> c)
c
- connection classpublic static Connection build(String descriptionString) throws ConnectionException
descriptionString
. descriptionString
- The format of the input string is: [prefix:] address [: port_number(s)]
ConnectionException
- if a connection could not be established for the given descriptionString.
© 2015 ZIH Corp. All Rights Reserved.