UniPRT SDK  v2.0.0.0
UsbConnection 類別 參考文件

實作 USB 特定通訊並擴展 AComm 類別的類別。 更多...

類別UsbConnection的繼承圖:
AComm IComm

公開方法(Public Methods)

 UsbConnection ()
 根據 vendorId 與 productId 初始化新類別實例。 更多...
 
 UsbConnection (short vendorId)
 
 UsbConnection (short vendorId, short productId)
 
 UsbConnection (String descriptor)
 根據 descriptor 字串初始化新的類別實例。 更多...
 
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 ()
 返回連線的字串描述。 更多...
 
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)
 等待當前執行緒有可用的資料 BytesAvailable。當前執行緒會休眠直到收到資料或超時為止。此為阻塞呼叫。 更多...
 
void Write (byte[] dataOut)
 Writes all bytes from the array passed in.
 
void Write (InputStream binReader) throws IOException
 從輸入流寫入輸出流。 更多...
 
byte[] WriteAndWaitForResponse (byte[] dataOut, int responseStartTimeOut, int responseEndTimeOut, String completetionToken) throws IOException
 將位元組資料寫入輸出流並返回接收到的資料。返回的資料是接收到的任何資料或收到的完成標記。等待回應超時後返回。 更多...
 
void WriteAndWaitForResponse (OutputStream binDataIn, InputStream binDataOut, int responseStartTimeOut, int responseEndTimeOut, String completetionToken) throws IOException
 將 binDataOut 流資料寫入輸出流,並將接收到的資料返回到 binDataIn 流中。返回的資料是收到的資料或收到的完成標記。等待回應超時後返回。 更多...
 

靜態公開方法(Static Public Methods)

static List< Tuple< Short, Short > > AvaliableDevices ()
 返回可用裝置的列表,格式為 List<Tuple<Short, Short>>。 更多...
 
static List< Short > DescriptorValidate (String descriptorHint)
 根據提示字串驗證描述符格式。 更多...
 

靜態公開屬性

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

詳細描述

實作 USB 特定通訊並擴展 AComm 類別的類別。

範例

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();
}
}
}

建構子與解構子說明文件

◆ UsbConnection() [1/2]

根據 vendorId 與 productId 初始化新類別實例。

參數
[in]vendorId廠商 ID(例如 TSC 或 Printronix)。可選。
[in]productId產品 ID。可選。

◆ UsbConnection() [2/2]

UsbConnection ( String  descriptor)

根據 descriptor 字串初始化新的類別實例。

參數
descriptor可接受的格式:
  • USB
  • USB:V_ID:<v_id>
  • USB:V_ID:<v_id>; P_ID:<p_id>
    • <v_id> 與 <p_id> 可使用十進位或十六進位

函式成員說明文件

◆ AvaliableDevices()

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

返回可用裝置的列表,格式為 List<Tuple<Short, Short>>。

v_id 表示廠商 ID;p_id 表示產品 ID。

◆ Descriptor()

String Descriptor ( )

返回連線的字串描述。

格式如下:

  • USB:V_ID:<v_id>; P_ID:<p_id>
    • v_id 和 p_id 為 short 型別,例如 V_ID:4611; P_ID:1234

實作 IComm.

◆ DescriptorValidate()

static List<Short> DescriptorValidate ( String  descriptorHint)
static

根據提示字串驗證描述符格式。

可接受的格式:

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

◆ WaitForData()

void WaitForData ( int  msTimeOut)
inherited

等待當前執行緒有可用的資料 BytesAvailable。當前執行緒會休眠直到收到資料或超時為止。此為阻塞呼叫。

參數
[in]msTimeOut在沒有收到資料前返回的毫秒超時。

實作 IComm.

◆ Write()

void Write ( InputStream  binReader) throws IOException
inherited

從輸入流寫入輸出流。

參數
[in]binReader要傳送的來源資料。

實作 IComm.

◆ WriteAndWaitForResponse() [1/2]

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

將位元組資料寫入輸出流並返回接收到的資料。返回的資料是接收到的任何資料或收到的完成標記。等待回應超時後返回。

傳回值
byte[]從連接接收到的位元組陣列。
返回的資料是接收到的任何資料或收到的完成標記。
參數
[in]dataOut要寫入連接的來源位元組陣列。
[in]responseStartTimeOut等待接收到第一個資料的超時。從第一個字節開始計時。
[in]responseEndTimeOut沒有新資料接收的超時。從最後一個字節後開始計時。
[in]completetionToken標示預期回應結束的字串標記。如果只是想在寫入資料到連接後等待回應,可以是空字串。

實作 IComm.

◆ WriteAndWaitForResponse() [2/2]

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

將 binDataOut 流資料寫入輸出流,並將接收到的資料返回到 binDataIn 流中。返回的資料是收到的資料或收到的完成標記。等待回應超時後返回。

參數
[out]binDataIn用於存儲從連接接收到的回應的目標流。
[in]binDataOut寫入連接的來源流資料。
[in]responseStartTimeOut等待接收到第一個資料的超時。從第一個字節開始計時。
[in]responseEndTimeOut沒有新資料接收的超時。從最後一個字節後開始計時。
[in]completetionToken標示預期回應結束的字串標記。如果只是想在寫入資料到連接後等待回應,可以是空字串。

實作 IComm.

com.UniPRT.Sdk.Comm.UsbConnection.UsbConnection
UsbConnection()
根據 vendorId 與 productId 初始化新類別實例。
Definition: UsbConnection.java:140