UniPRT SDK  v1.0.0.0
TcpConnection 類別 參考文件

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

類別TcpConnection的繼承圖:
AComm IComm

複合項目

enum  DescriptorPortType
 用於請求具有預設埠值的描述符類型 更多...
 

公開方法(Public Methods)

 TcpConnection (String descriptor) throws IllegalArgumentException
 根據 descriptor 字串初始化新的類別實例。 更多...
 
 TcpConnection (String ipAddress, int port)
 Initialize new class instance.
 
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 ()
 返回連線的字串描述。格式: "TCP:ip:port" 例如: "TCP:127.0.0.1:9100" 更多...
 
String IpAddress ()
 IP address of connection.
 
void Open ()
 Opens a connection.
 
int Port ()
 Port Number of connection.
 
byte[] Read ()
 Read all bytes available.
 
void Read (OutputStream binDataIn) throws IOException
 Reads all bytes available into the stream passed in.
 
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 String DescriptorValidate (String descriptorHint, DescriptorPortType portTypeHint)
 從可能包含或不包含埠的 descriptorHint 驗證/建構描述符字串。返回有效的描述符或空字串 ""。 更多...
 

靜態公開屬性

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

詳細描述

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

範例

package com.test.UniPRTSdk;
import com.UniPRT.Sdk.Comm.TcpConnection;
import java.io.*;
public class commTcpSnippet {
public static void main(String[] args) {
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();
}
}
}

建構子與解構子說明文件

◆ TcpConnection()

TcpConnection ( String  descriptor) throws IllegalArgumentException

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

參數
[in]descriptor冒號 (:) 分隔的連線描述字串。 可接受的格式:
  • <ip>
    • IP 位址,格式為 #.#.#.#。例如 127.0.0.1
    • 使用預設的資料埠 (DEFAULT_DATA_PORT)
  • <ip>:<port>
    • IP 位址和指定的埠號,格式為 #.#.#.#:#。例如 127.0.0.1:9100
  • <type>:<ip>:<port>
    • IP 位址和指定的埠號,格式為 "TCP":#.#.#.#:#。例如 TCP:127.0.0.1:9100
    • 注意,不論傳入的是什麼,連線的 type 將始終是 "TCP"。

函式成員說明文件

◆ Descriptor()

String Descriptor ( )

返回連線的字串描述。格式: "TCP:ip:port" 例如: "TCP:127.0.0.1:9100"

返回的格式:

  • <type>:<ip>:<port>
    • IP 位址及指定的埠號,格式為 "TCP":#.#.#.#:#,例如 TCP:127.0.0.1:9100
    • IP 和埠號為整數
    • 注意連線的 type 為 "TCP"。

注意返回的字串前綴 "TCP" 來標示連線的類型。

實作 IComm.

◆ DescriptorValidate()

static String DescriptorValidate ( String  descriptorHint,
DescriptorPortType  portTypeHint 
)
static

從可能包含或不包含埠的 descriptorHint 驗證/建構描述符字串。返回有效的描述符或空字串 ""。

descriptorHint 可接受的值:

  • 僅 IP 位址: #.#.#.#
    • 根據 portTypeHint 使用預設埠
  • IP + 埠號: #.#.#.#:p 其中 p = 埠號
  • TCP: IP 位址
    • 根據 portTypeHint 使用預設埠
  • "TCP:" + IP + 埠號: "TCP:#.#.#.#:p" 其中 p = 埠號

◆ 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.

資料成員說明文件

◆ DEFAULT_STATUS_PORT

final int DEFAULT_STATUS_PORT = 3002
static

Port for listening to status messages from printer.

打印機可根據打印機上的「Ret. Status Port」設定將狀態讯息發送到不同的端口。 如果「Ret. Status Port」設置為「E-NET Stat Port」,則打印機可能會將狀態讯息發送到「Status Port Number」設定,預設為 3002。

例如:打印機語言(如 PGL)的模擬診斷讯息。

例如:使用以下打印機菜單設置捕獲 ODV 條碼數據和等級:

  • 將「System/Printer Mgmt/Ret. Status Port」設為「E-NET Stat Port」
  • 將「System/Printer Mgmt/Status Port Number」設為 3002
  • 將「ODV2D/Control/Telemetry Data」設為「Full Report」
  • 將「ODV2D/Control/Return Data」設為「Data+Grade」,以便監聽「條碼數據 + 等級」
com.UniPRT.Sdk.Comm.TcpConnection.TcpConnection
TcpConnection(String descriptor)
根據 descriptor 字串初始化新的類別實例。
Definition: TcpConnection.java:285