UniPRT SDK  v2.0.0.0
UsbConnection Class Reference

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

Inheritance diagram for UsbConnection:
AComm IComm

Public Member Functions

 UsbConnection ()
 Initialize new class instance based on vendorId and productId. More...
 
 UsbConnection (short vendorId)
 
 UsbConnection (short vendorId, short productId)
 
 UsbConnection (String descriptor)
 Initialize new class instance based on descriptor string. More...
 
int BytesAvailable ()
 Number of bytes available to read. This is estimate and number can change as data is received.
 
void Close ()
 Closes a connection.
 
boolean Connected ()
 true if connection is established
 
String Descriptor ()
 Returns a string description of the connection. More...
 
void Open ()
 Opens a connection.
 
short P_ID ()
 Product ID.
 
byte[] Read ()
 Read all bytes available.
 
void Read (OutputStream binDataIn) throws IOException
 Reads all bytes available into the stream passed in.
 
short V_ID ()
 Vendor ID.
 
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 (byte[] dataOut)
 Writes all bytes from the array passed in.
 
void Write (InputStream binReader) throws IOException
 Write from input stream to output stream
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...
 
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...
 

Static Public Member Functions

static List< Tuple< Short, Short > > AvaliableDevices ()
 Return the list of available devices in the format of List<Tuple<Short, Short>>. More...
 
static List< Short > DescriptorValidate (String descriptorHint)
 Validate descriptor string from hint. More...
 

Static Public Attributes

static final short PTX_USB_VID = 0x14ae
 
static final short TSC_USB_VID = 0x1203
 

Detailed Description

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

Examples

package com.test.UniPRTSdk;
import com.UniPRT.Sdk.Comm.UsbConnection;
import com.UniPRT.Sdk.Utilities.Tuple;
import java.io.*;
import java.util.List;
public class commUsbSnippet {
public static void main(String[] args) {
sendPrintFile(); // send file over default printer data port
sendPrintString(); // send print data over default printer data port
}
public static void sendPrintFile() {
String fileName = "C:\\testFiles\\Hello.pgl";
List<Tuple<Short, Short>> devices = UsbConnection.AvaliableDevices();
UsbConnection ptrUsbComm = new UsbConnection(devices.get(0).GetX(), devices.get(0).GetY()); // sending through default data port
try {
ptrUsbComm.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) {
ptrUsbComm.Write(buffer);
}
}
} else {
System.out.println("File \"" + fileName + "\" not found");
}
} catch (Exception e) {
System.out.println("Exception Msg: " + e.getMessage());
} finally {
ptrUsbComm.Close();
}
}
public static void sendPrintString() {
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";
List<Tuple<Short, Short>> devices = UsbConnection.AvaliableDevices();
UsbConnection ptrUsbComm = new UsbConnection(devices.get(0).GetX(), devices.get(0).GetY()); // sending through default data port
try {
ptrUsbComm.Open();
if (ptrUsbComm.Connected()) {
byte[] outBytes = dataToPrint.getBytes("US-ASCII");
ptrUsbComm.Write(outBytes);
} else {
System.out.println("Not connected to printer");
}
} catch (Exception e) {
System.out.println("Exception Msg: " + e.getMessage());
} finally {
ptrUsbComm.Close();
}
}
}

Constructor & Destructor Documentation

◆ UsbConnection() [1/2]

Initialize new class instance based on vendorId and productId.

Parameters
[in]vendorIdVendor ID (e.g., TSC or Printronix). Optional.
[in]productIdProduct ID. Optional.

◆ UsbConnection() [2/2]

UsbConnection ( String  descriptor)

Initialize new class instance based on descriptor string.

Parameters
descriptorAcceptable formats:
  • USB
  • USB:V_ID:<v_id>
  • USB:V_ID:<v_id>; P_ID:<p_id>
    • <v_id> and <p_id> allow decimal or hexadecimal values

Member Function Documentation

◆ AvaliableDevices()

static List<Tuple<Short, Short> > AvaliableDevices ( )
static

Return the list of available devices in the format of List<Tuple<Short, Short>>.

v_id means Vendor ID; p_id means Product ID.

◆ Descriptor()

String Descriptor ( )

Returns a string description of the connection.

Format returned:

  • USB:V_ID:<v_id>; P_ID:<p_id>
    • v_id and p_id are short numbers. e.g., V_ID:4611; P_ID:1234

Implements IComm.

◆ DescriptorValidate()

static List<Short> DescriptorValidate ( String  descriptorHint)
static

Validate descriptor string from hint.

Acceptable formats:

  • USB
  • USB:V_ID:4611
  • USB:V_ID:4611; P_ID:1234

◆ WaitForData()

void WaitForData ( int  msTimeOut)
inherited

Waits until data available BytesAvailable in current thread. Current thread sleeps until data received or timeout reached. Blocking call.

Parameters
[in]msTimeOutMillisecond timeout before return if no data received.

Implements IComm.

◆ Write()

void Write ( InputStream  binReader) throws IOException
inherited

Write from input stream to output stream

Parameters
[in]binReaderSource data to send

Implements IComm.

◆ WriteAndWaitForResponse() [1/2]

byte [] WriteAndWaitForResponse ( byte[]  dataOut,
int  responseStartTimeOut,
int  responseEndTimeOut,
String  completionToken 
) throws IOException
inherited

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.

Return values
byte[]Array of bytes received from connection.
Data returned is any data received or up to completetionToken if token received
Parameters
[in]dataOutSource byte array to write to connection.
[in]responseStartTimeOutTimeout to wait for first data to be received. Time to first byte.
[in]responseEndTimeOutTimeout of no new data received. Time after last byte.
[in]completetionTokenString token denoting the end of the response expected. This can be empty string if simply want to wait for response after writing data to connection.

Implements IComm.

◆ WriteAndWaitForResponse() [2/2]

void WriteAndWaitForResponse ( OutputStream  binDataIn,
InputStream  binDataOut,
int  responseStartTimeOut,
int  responseEndTimeOut,
String  completionToken 
) throws IOException
inherited

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.

Parameters
[out]binDataInDestination stream to store response received from connection.
[in]binDataOutSource stream data to write to connection.
[in]responseStartTimeOutTimeout to wait for first data to be received. Time to first byte.
[in]responseEndTimeOutTimeout of no new data received. Time after last byte.
[in]completetionTokenString token denoting the end of the response expected. This can be empty string if simply want to wait for response after writing data to connection.

Implements IComm.

com.UniPRT.Sdk.Comm.UsbConnection.UsbConnection
UsbConnection()
Initialize new class instance based on vendorId and productId.
Definition: UsbConnection.java:140