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

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

類別com.UniPRT.Sdk.Comm.TcpConnection的繼承圖:
com.UniPRT.Sdk.Comm.AComm com.UniPRT.Sdk.Comm.IComm

複合項目

enum  DescriptorPortType
 請求帶有預設端口值的描述符的類型。 更多...
 

公開方法(Public Methods)

int GetPort ()
 
 TcpConnection (String descriptor)
 根據 descriptor 字串初始化新類別實例。 更多...
 
 TcpConnection (String ipAddress, int port)
 
String IpAddress ()
 IP address of connection. 更多...
 
int Port ()
 Port Number of connection. 更多...
 
int BytesAvailable ()
 
boolean Connected ()
 
String Descriptor ()
 返回連線的字串描述。格式:"<b>TCP</b>:ip:port" 例如:"<b>TCP</b>:127.0.0.1:9100" 更多...
 
void Close ()
 
void Open ()
 
byte[] Read ()
 
void Write (byte[] dataOut)
 
- 公開方法(Public Methods) 繼承自 com.UniPRT.Sdk.Comm.AComm
void Read (OutputStream binDataIn) throws IOException
 
void WaitForData (int msTimeOut)
 等待當前執行緒有可用的資料 BytesAvailable。當前執行緒會休眠直到收到資料或超時為止。此為阻塞呼叫。 更多...
 
void Write (InputStream binReader) throws IOException
 從輸入流寫入輸出流。 更多...
 
void WriteAndWaitForResponse (OutputStream binDataIn, InputStream binDataOut, int responseStartTimeOut, int responseEndTimeOut, String completetionToken) throws IOException
 將 binDataOut 流資料寫入輸出流,並將接收到的資料返回到 binDataIn 流中。返回的資料是收到的資料或收到的完成標記。等待回應超時後返回。 更多...
 
byte[] WriteAndWaitForResponse (byte[] dataOut, int responseStartTimeOut, int responseEndTimeOut, String completetionToken) throws IOException
 將位元組資料寫入輸出流並返回接收到的資料。返回的資料是接收到的任何資料或收到的完成標記。等待回應超時後返回。 更多...
 

靜態公開方法(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_STATUS_PORT = 3002
 Port for listening to status messages from printer. 更多...
 
static final int DEFAULT_MGMT_PORT = 3007
 Port used to control and manage printers. 更多...
 

私有方法(Private Methods)

void ConnectionSettings (String ipAddress, int port)
 

私有屬性

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

詳細描述

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

範例

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

建構子與解構子說明文件

◆ TcpConnection() [1/2]

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

根據 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"。

參考 com.UniPRT.Sdk.Comm.TcpConnection.DescriptorPortType.DATA, 及 com.UniPRT.Sdk.Comm.TcpConnection.DescriptorValidate().

◆ TcpConnection() [2/2]

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

函式成員說明文件

◆ BytesAvailable()

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

Get the number of bytes available to read.

傳回值
Number of bytes available to read.

實作 com.UniPRT.Sdk.Comm.IComm.

參考 com.UniPRT.Sdk.Comm.TcpConnection._inStream.

◆ Close()

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

◆ Connected()

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

Check if connection is established.

傳回值
true if connection is established, otherwise false.

實作 com.UniPRT.Sdk.Comm.IComm.

參考 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 ( )

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

返回的格式:

  • <type>:<ip>:<port>
  • IP 地址和特定端口號格式為 "TCP":#.#.#.#:#。例如:TCP:127.0.0.1:9100
  • IP 和端口為整數
  • 注意連線的 type 是 "TCP"。

注意返回的字串中以 "<b>TCP</b>" 前綴來標示連線類型。

實作 com.UniPRT.Sdk.Comm.IComm.

參考 com.UniPRT.Sdk.Comm.TcpConnection._ipAddress, 及 com.UniPRT.Sdk.Comm.TcpConnection._port.

◆ DescriptorValidate()

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

從可能有或沒有端口的 descriptorHint 驗證/構建描述符字串。返回有效的描述符或空的 "" 字串。

descriptorHint 接受的值:

  • 僅 IP:#.#.#.#
  • 根據 portTypeHint 使用預設端口
  • IP + 端口:#.#.#.#:p,其中 p = 端口號
  • TCP:IP
  • 根據 portTypeHint 使用預設端口
  • "TCP:" + IP + 端口:"TCP:#.#.#.#:p",其中 p = 端口號

參考 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, 及 com.UniPRT.Sdk.Comm.TcpConnection.DescriptorPortType.STATUS.

被參考於 com.UniPRT.Sdk.Comm.Communication.CreateComm(), 及 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.

參考 com.UniPRT.Sdk.Comm.TcpConnection._ipAddress.

◆ Open()

◆ Port()

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

Port Number of connection.

參考 com.UniPRT.Sdk.Comm.TcpConnection._port.

◆ Read()

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

Read all available bytes.

傳回值
Array of bytes read.

實作 com.UniPRT.Sdk.Comm.IComm.

參考 com.UniPRT.Sdk.Comm.TcpConnection._inStream.

◆ Write()

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

Write all bytes from the array passed in.

參數
dataOutByte array to write.

實作 com.UniPRT.Sdk.Comm.IComm.

參考 com.UniPRT.Sdk.Comm.TcpConnection._outStream.

資料成員說明文件

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

被參考於 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.

打印機可以根據 "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.DescriptorValidate().


此類別(class) 文件是由下列檔案中產生:
com.UniPRT.Sdk.Comm.TcpConnection.TcpConnection
TcpConnection(String descriptor)
根據 descriptor 字串初始化新類別實例。
Definition: TcpConnection.java:234
com.UniPRT.Sdk.Comm
Definition: AComm.java:1
com.UniPRT
com.UniPRT.Sdk.Comm.TcpConnection
實作 TCP 特定通訊並擴展 AComm 類別的類別。
Definition: TcpConnection.java:32
com.UniPRT.Sdk
com