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 成员函数

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 成员函数 继承自 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
 将字节数据写入输出流并返回接收到的数据。返回的数据是接收到的任何数据或收到的完成标记。等待响应超时后返回。 更多...
 

静态 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_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 成员函数

void ConnectionSettings (String ipAddress, int port)
 

Private 属性

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

Close the connection.

实现了 com.UniPRT.Sdk.Comm.IComm.

引用了 com.UniPRT.Sdk.Comm.TcpConnection._client.

◆ 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

Socket com.UniPRT.Sdk.Comm.TcpConnection._client
private

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


该类的文档由以下文件生成:
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