UniPRT SDK  v1.0.0.0
TcpConnection类 参考

实现 TCP 特定通信并扩展 AComm 类的类。 更多...

类 TcpConnection 继承关系图:
AComm IComm

enum  DescriptorPortType
 用于请求具有默认端口值的描述符类型 更多...
 

Public 成员函数

 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 流中。返回的数据是收到的数据或收到的完成标记。等待响应超时后返回。 更多...
 

静态 Public 成员函数

static String DescriptorValidate (String descriptorHint, DescriptorPortType portTypeHint)
 从可能包含或不包含端口的 descriptorHint 验证/构建描述符字符串。返回有效的描述符或空字符串 ""。 更多...
 

静态 Public 属性

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