UniPRT SDK  v1.0.0.0
com.UniPRT.Sdk.Comm.TcpConnection Class Reference

Class that implements the TCP specific communication and extends the AComm class. More...

Inheritance diagram for com.UniPRT.Sdk.Comm.TcpConnection:
com.UniPRT.Sdk.Comm.AComm com.UniPRT.Sdk.Comm.IComm

Classes

enum  DescriptorPortType
 Types for requesting descriptors with default port values. More...
 

Public Member Functions

int GetPort ()
 
 TcpConnection (String descriptor)
 Initialize new class instance based on descriptor string. More...
 
 TcpConnection (String ipAddress, int port)
 
String IpAddress ()
 IP address of connection. More...
 
int Port ()
 Port Number of connection. More...
 
int BytesAvailable ()
 
boolean Connected ()
 
String Descriptor ()
 Returns a string description of the connection. Format: "<b>TCP</b>:ip:port" e.g. "<b>TCP</b>:127.0.0.1:9100". More...
 
void Close ()
 
void Open ()
 
byte[] Read ()
 
void Write (byte[] dataOut)
 
- Public Member Functions inherited from com.UniPRT.Sdk.Comm.AComm
void Read (OutputStream binDataIn) throws IOException
 
void WaitForData (int msTimeOut)
 Waits until data available BytesAvailable in current thread. Current thread sleeps until data received or timeout reached. Blocking call. More...
 
void Write (InputStream binReader) throws IOException
 Write from input stream to output stream. More...
 
void WriteAndWaitForResponse (OutputStream binDataIn, InputStream binDataOut, int responseStartTimeOut, int responseEndTimeOut, String completetionToken) throws IOException
 Write binDataOut stream data to output stream and return data received in binDataIn stream. Data returned is any data received or up to completion token if received. Wait for response timeout before returning. More...
 
byte[] WriteAndWaitForResponse (byte[] dataOut, int responseStartTimeOut, int responseEndTimeOut, String completetionToken) throws IOException
 Write byte data to output stream and return data received. Data returned is any data received or up to completion token if received. Wait for response timeout before returning. More...
 

Static Public Member Functions

static String DescriptorValidate (String descriptorHint, DescriptorPortType portTypeHint)
 Validate/build descriptor string from descriptorHint that may or may not have a port. Returns valid descriptor or empty "" string. More...
 

Static Public Attributes

static final int DEFAULT_DATA_PORT = 9100
 Port for sending print data. More...
 
static final int DEFAULT_STATUS_PORT = 3002
 Port for listening to status messages from printer. More...
 
static final int DEFAULT_MGMT_PORT = 3007
 Port used to control and manage printers. More...
 

Private Member Functions

void ConnectionSettings (String ipAddress, int port)
 

Private Attributes

String _ipAddress = ""
 
int _port = 0
 
Socket _client
 
InputStream _inStream
 
OutputStream _outStream
 

Detailed Description

Class that implements the TCP specific communication and extends the AComm class.

Examples

package com.test.UniPRTSdk;
import java.io.*;
public class commTcpSnippet {
public static void test() {
String prtIp = "127.0.0.1";
sendPrintFile(prtIp); // send file over default printer data port
sendPrintString(prtIp); // send print data over default printer data port
}
public static void sendPrintFile(String ipAddress) {
String fileName = "C:\\testFiles\\Hello.pgl";
int defaultDataPort = TcpConnection.DEFAULT_DATA_PORT;
TcpConnection ptrTcpComm = new TcpConnection(ipAddress, defaultDataPort); // sending through default data port
try {
ptrTcpComm.Open();
if (new File(fileName).exists()) {
try (InputStream binReader = new BufferedInputStream(new FileInputStream(fileName))) {
System.out.println("Sending \"" + fileName + "\" to printer");
byte[] buffer = new byte[1024];
while ((binReader.read(buffer)) != -1) {
ptrTcpComm.Write(buffer);
}
}
} else {
System.out.println("File \"" + fileName + "\" not found");
}
} catch (Exception e) {
System.out.println("Exception Msg: " + e.getMessage());
} finally {
ptrTcpComm.Close();
}
}
public static void sendPrintString(String ipAddress) {
String dataToPrint =
"~CREATE;C39;72\n" +
"SCALE;DOT\n" +
"PAGE;30;40\n" +
"ALPHA\n" +
"C10;1;33;0;0;@HELLO@\n" +
"C16;54;37;0;0;@*World*@\n" +
"STOP\n" +
"BARCODE\n" +
"C128C;XRD3:3:6:6:9:9:12:12;H6;10;32\n" +
"@World@\n" +
"STOP\n" +
"END\n" +
"~EXECUTE;C39\n" +
"~NORMAL\n";
int defaultDataPort = TcpConnection.DEFAULT_DATA_PORT;
TcpConnection ptrTcpComm = new TcpConnection(ipAddress, defaultDataPort); // sending through default data port
try {
ptrTcpComm.Open();
if (ptrTcpComm.Connected()) {
byte[] outBytes = dataToPrint.getBytes("US-ASCII");
ptrTcpComm.Write(outBytes);
} else {
System.out.println("Not connected to printer");
}
} catch (Exception e) {
System.out.println("Exception Msg: " + e.getMessage());
} finally {
ptrTcpComm.Close();
}
}
}

Constructor & Destructor Documentation

◆ TcpConnection() [1/2]

com.UniPRT.Sdk.Comm.TcpConnection.TcpConnection ( String  descriptor)

Initialize new class instance based on descriptor string.

Parameters
[in]descriptorColon (:) separated string description of connection.
Acceptable Formats:
  • <ip>
  • IP address in #.#.#.# format. e.g. 127.0.0.1
  • Uses default data port (DEFAULT_DATA_PORT)
  • <ip>:<port>
  • IP address and specific port number in #.#.#.#:# format. e.g. 127.0.0.1:9100
  • <type>:<ip>:<port>
  • IP address and specific port number in "TCP":#.#.#.#:# format. e.g. TCP:127.0.0.1:9100
  • Note that the type of connection will always be of "TCP" independent of what is passed in.

References com.UniPRT.Sdk.Comm.TcpConnection.DescriptorPortType.DATA, and com.UniPRT.Sdk.Comm.TcpConnection.DescriptorValidate().

◆ TcpConnection() [2/2]

com.UniPRT.Sdk.Comm.TcpConnection.TcpConnection ( String  ipAddress,
int  port 
)

Member Function Documentation

◆ BytesAvailable()

int com.UniPRT.Sdk.Comm.TcpConnection.BytesAvailable ( )

Get the number of bytes available to read.

Returns
Number of bytes available to read.

Implements com.UniPRT.Sdk.Comm.IComm.

References com.UniPRT.Sdk.Comm.TcpConnection._inStream.

◆ Close()

void com.UniPRT.Sdk.Comm.TcpConnection.Close ( )

Close the connection.

Implements com.UniPRT.Sdk.Comm.IComm.

References com.UniPRT.Sdk.Comm.TcpConnection._client.

◆ Connected()

boolean com.UniPRT.Sdk.Comm.TcpConnection.Connected ( )

Check if connection is established.

Returns
true if connection is established, otherwise false.

Implements com.UniPRT.Sdk.Comm.IComm.

References com.UniPRT.Sdk.Comm.TcpConnection._client.

◆ ConnectionSettings()

void com.UniPRT.Sdk.Comm.TcpConnection.ConnectionSettings ( String  ipAddress,
int  port 
)
private

◆ Descriptor()

String com.UniPRT.Sdk.Comm.TcpConnection.Descriptor ( )

Returns a string description of the connection. Format: "<b>TCP</b>:ip:port" e.g. "<b>TCP</b>:127.0.0.1:9100".

Format returned:

  • <type>:<ip>:<port>
  • IP address and specific port number in "TCP":#.#.#.#:# format. e.g. TCP:127.0.0.1:9100
  • IP and port are integer numbers
  • Note that the type of connection is "TCP".

Note that "<b>TCP</b>" prefix on returned string to denote the type of connection.

Implements com.UniPRT.Sdk.Comm.IComm.

References com.UniPRT.Sdk.Comm.TcpConnection._ipAddress, and com.UniPRT.Sdk.Comm.TcpConnection._port.

◆ DescriptorValidate()

static String com.UniPRT.Sdk.Comm.TcpConnection.DescriptorValidate ( String  descriptorHint,
DescriptorPortType  portTypeHint 
)
static

Validate/build descriptor string from descriptorHint that may or may not have a port. Returns valid descriptor or empty "" string.

descriptorHint accepted values:

  • Ip only: #.#.#.#
  • Uses default ports based on portTypeHint
  • Ip + Port: #.#.#.#:p where p = port number
  • TCP: Ip
  • Uses default ports based on portTypeHint
  • "TCP:" + Ip + Port: "TCP:#.#.#.#:p" where p = port number

References com.UniPRT.Sdk.Comm.TcpConnection.DescriptorPortType.DATA, com.UniPRT.Sdk.Comm.TcpConnection.DEFAULT_DATA_PORT, com.UniPRT.Sdk.Comm.TcpConnection.DEFAULT_MGMT_PORT, com.UniPRT.Sdk.Comm.TcpConnection.DEFAULT_STATUS_PORT, com.UniPRT.Sdk.Comm.TcpConnection.DescriptorPortType.MGMT, and com.UniPRT.Sdk.Comm.TcpConnection.DescriptorPortType.STATUS.

Referenced by com.UniPRT.Sdk.Comm.Communication.CreateComm(), and com.UniPRT.Sdk.Comm.TcpConnection.TcpConnection().

◆ GetPort()

int com.UniPRT.Sdk.Comm.TcpConnection.GetPort ( )

◆ IpAddress()

String com.UniPRT.Sdk.Comm.TcpConnection.IpAddress ( )

IP address of connection.

References com.UniPRT.Sdk.Comm.TcpConnection._ipAddress.

◆ Open()

◆ Port()

int com.UniPRT.Sdk.Comm.TcpConnection.Port ( )

Port Number of connection.

References com.UniPRT.Sdk.Comm.TcpConnection._port.

◆ Read()

byte [] com.UniPRT.Sdk.Comm.TcpConnection.Read ( )

Read all available bytes.

Returns
Array of bytes read.

Implements com.UniPRT.Sdk.Comm.IComm.

References com.UniPRT.Sdk.Comm.TcpConnection._inStream.

◆ Write()

void com.UniPRT.Sdk.Comm.TcpConnection.Write ( byte[]  dataOut)

Write all bytes from the array passed in.

Parameters
dataOutByte array to write.

Implements com.UniPRT.Sdk.Comm.IComm.

References com.UniPRT.Sdk.Comm.TcpConnection._outStream.

Member Data Documentation

◆ _client

◆ _inStream

InputStream com.UniPRT.Sdk.Comm.TcpConnection._inStream
private

◆ _ipAddress

String com.UniPRT.Sdk.Comm.TcpConnection._ipAddress = ""
private

◆ _outStream

OutputStream com.UniPRT.Sdk.Comm.TcpConnection._outStream
private

◆ _port

◆ DEFAULT_DATA_PORT

final int com.UniPRT.Sdk.Comm.TcpConnection.DEFAULT_DATA_PORT = 9100
static

◆ DEFAULT_MGMT_PORT

final int com.UniPRT.Sdk.Comm.TcpConnection.DEFAULT_MGMT_PORT = 3007
static

Port used to control and manage printers.

Referenced by com.UniPRT.Sdk.Comm.TcpConnection.DescriptorValidate().

◆ DEFAULT_STATUS_PORT

final int com.UniPRT.Sdk.Comm.TcpConnection.DEFAULT_STATUS_PORT = 3002
static

Port for listening to status messages from printer.

Printer may be configured to send status messages to different ports based on the "Ret. Status Port" setting on the printer. If the "Ret. Status Port" is set to "E-NET Stat Port", the printer may send status messages to the "Status Port Number" setting which has a default of 3002.

e.g. Emulation (printer language such as PGL) diagnostic messages from printer.

e.g. Capturing ODV barcode data and grade with the following printer menu settings:

  • "System/Printer Mgmt/Ret. Status Port" set to "E-NET Stat Port"
  • "System/Printer Mgmt/Status Port Number" set to 3002
  • "ODV2D/Control/Telemetry Data" set to "Full Report"
  • "ODV2D/Control/Return Data" set to "Data+Grade" for listening to "barcode data + grade"

Referenced by com.UniPRT.Sdk.Comm.TcpConnection.DescriptorValidate().


The documentation for this class was generated from the following file:
com.UniPRT.Sdk.Comm.TcpConnection.TcpConnection
TcpConnection(String descriptor)
Initialize new class instance based on descriptor string.
Definition: TcpConnection.java:234
com.UniPRT.Sdk.Comm
Definition: AComm.java:1
com.UniPRT
com.UniPRT.Sdk.Comm.TcpConnection
Class that implements the TCP specific communication and extends the AComm class.
Definition: TcpConnection.java:32
com.UniPRT.Sdk
com