UniPRT SDK  v1.0.0.0
UniPRT.Sdk.Comm.TcpConnection类 参考

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

类 UniPRT.Sdk.Comm.TcpConnection 继承关系图:
UniPRT.Sdk.Comm.AComm UniPRT.Sdk.Comm.IComm

Public 类型

enum  DescriptorPortType { DATA, MGMT, STATUS }
 用于请求带有默认端口值的描述符的类型
 

Public 成员函数

 TcpConnection (string descriptor)
 根据 descriptor 字符串初始化新类实例。 更多...
 
 TcpConnection (string IpAddress, int port)
 Initialize new class instance
 
override void Close ()
 Closes a connection.
 
override void Open ()
 Opens a connection.
 
override byte[] Read ()
 Read all bytes available.
 
virtual void Read (BinaryWriter binDataIn)
 Reads all bytes available into the stream passed in.
 
virtual void WaitForData (int msTimeOut)
 等待数据可用 BytesAvailable 在当前线程。当前线程休眠直到接收到数据或超时。阻塞调用。 更多...
 
virtual void Write (BinaryReader binReader)
 从输入流写入输出流 更多...
 
override void Write (byte[] dataOut)
 Writes all bytes from the array passed in.
 
virtual void WriteAndWaitForResponse (BinaryWriter binDataIn, BinaryReader binDataOut, int responseStartTimeOut, int responseEndTimeOut, string completetionToken)
 将 binDataOut 流数据写入输出流,并将接收到的数据返回到 binDataIn 流中。返回的数据是接收到的任何数据或达到接收的完成标记。如果没有响应则在返回前等待超时。 更多...
 
virtual byte[] WriteAndWaitForResponse (byte[] dataOut, int responseStartTimeOut, int responseEndTimeOut, string completetionToken)
 将字节数据写入输出流并返回接收到的数据。返回的数据是接收到的任何数据或达到接收的完成标记。在返回前等待响应超时。 更多...
 

静态 Public 成员函数

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

Public 属性

override string Descriptor => $"TCP:{_ipAddress}:{_port.ToString()}"
 返回连接的字符串描述。格式:"TCP:ip:port" 例如 "TCP:127.0.0.1:9100" 更多...
 

静态 Public 属性

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

属性

override int BytesAvailable [get]
 
override bool Connected [get]
 
string IpAddress [get]
 IP address of connection
 
int Port [get]
 Port Number of connection
 

详细描述

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

示例

using System;
using System.IO;
using System.Text;
using UniPRT.Sdk.Comm; // imports SDK namespace
namespace Snippets
{
class MyComm
{
public static void MainComm(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) // send file over default printer data port
{
string fileName = @"C:\testFiles\Hello.pgl";
TcpConnection PtrTcpComm = new TcpConnection(ipAddress, TcpConnection.DEFAULT_DATA_PORT); // sending through default data port
try
{
PtrTcpComm.Open();
if (File.Exists(fileName))
{
using (BinaryReader binReader = new BinaryReader(File.Open(fileName, FileMode.Open)))
{
Console.WriteLine($"Sending \"{fileName}\" to printer");
PtrTcpComm.Write(binReader);
}
}
else
{
Console.WriteLine($"File \"{fileName}\" not found");
}
}
catch (Exception e)
{
Console.WriteLine($"Exception Msg: {e.Message}");
}
finally
{
PtrTcpComm.Close();
}
}
public static void SendPrintString(string ipAddress) // send print data over default printer data port
{
string dataToPrint =
@"~CREATE;C39;72
SCALE;DOT
PAGE;30;40
ALPHA
C10;1;33;0;0;@HELLO@
C16;54;37;0;0;@*World*@
STOP
BARCODE
C128C;XRD3:3:6:6:9:9:12:12;H6;10;32
@World@
STOP
END
~EXECUTE;C39
~NORMAL
";
TcpConnection PtrTcpComm = new TcpConnection(ipAddress, TcpConnection.DEFAULT_DATA_PORT); // sending through default data port 9100
try
{
PtrTcpComm.Open();
if (PtrTcpComm.Connected)
{
//byte[] outBytes = Encoding.UTF8.GetBytes(dataToPrint);
byte[] outBytes = Encoding.ASCII.GetBytes(dataToPrint);
PtrTcpComm.Write(outBytes);
}
else
{
Console.WriteLine($"Not connected to printer");
}
}
catch (Exception e)
{
Console.WriteLine($"Exception Msg: {e.Message}");
}
finally
{
PtrTcpComm.Close();
}
}
}
}

构造及析构函数说明

◆ TcpConnection()

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",无论传入的值为何。

成员函数说明

◆ DescriptorValidate()

static string 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 = 端口号

◆ WaitForData()

virtual void UniPRT.Sdk.Comm.AComm.WaitForData ( int  msTimeOut)
virtualinherited

等待数据可用 BytesAvailable 在当前线程。当前线程休眠直到接收到数据或超时。阻塞调用。

参数
[in]msTimeOut如果未接收到数据则在返回前的毫秒超时时间。

实现了 UniPRT.Sdk.Comm.IComm.

◆ Write()

virtual void UniPRT.Sdk.Comm.AComm.Write ( BinaryReader  binReader)
virtualinherited

从输入流写入输出流

参数
[in]binReader要发送的源数据

实现了 UniPRT.Sdk.Comm.IComm.

◆ WriteAndWaitForResponse() [1/2]

virtual void UniPRT.Sdk.Comm.AComm.WriteAndWaitForResponse ( BinaryWriter  binDataIn,
BinaryReader  binDataOut,
int  responseStartTimeOut,
int  responseEndTimeOut,
string  completetionToken 
)
virtualinherited

将 binDataOut 流数据写入输出流,并将接收到的数据返回到 binDataIn 流中。返回的数据是接收到的任何数据或达到接收的完成标记。如果没有响应则在返回前等待超时。

参数
[out]binDataIn目标流以存储从连接接收到的响应。
[in]binDataOut
要写入连接的源流数据。
[in]responseStartTimeOut等待接收到第一个数据的超时时间(毫秒)。第一个字节的时间。
[in]responseEndTimeOut没有接收到新数据的超时时间(毫秒)。最后一个字节后的时间。
[in]completetionToken指示预期响应结束的字符串标记。如果只是想等待写入数据后的响应,可以是空字符串。

实现了 UniPRT.Sdk.Comm.IComm.

◆ WriteAndWaitForResponse() [2/2]

virtual byte [] UniPRT.Sdk.Comm.AComm.WriteAndWaitForResponse ( byte[]  dataOut,
int  responseStartTimeOut,
int  responseEndTimeOut,
string  completetionToken 
)
virtualinherited

将字节数据写入输出流并返回接收到的数据。返回的数据是接收到的任何数据或达到接收的完成标记。在返回前等待响应超时。

返回值
byte[]从连接接收到的字节数组。 返回的数据是接收到的任何数据或达到完成标记时的数据。
参数
[in]dataOut
要写入连接的源字节数组。
[in]responseStartTimeOut等待接收到第一个数据的超时时间(毫秒)。第一个字节的时间。
[in]responseEndTimeOut没有接收到新数据的超时时间(毫秒)。最后一个字节后的时间。
[in]completetionToken指示预期响应结束的字符串标记。如果只是想等待写入数据后的响应,可以是空字符串。

实现了 UniPRT.Sdk.Comm.IComm.

类成员变量说明

◆ DEFAULT_STATUS_PORT

readonly int UniPRT.Sdk.Comm.TcpConnection.DEFAULT_STATUS_PORT = 3002
static

Port for listening to status messages from printer.

Printer may be configured to send status messages to different ports based on the "Ret. Status Port" setting on the printer. If the "Ret. Status Port" is set to "E-NET Stat Port", the printer may send status messages to the "Status Port Number" setting which has a default of 3002.

e.g. Emulation (printer language such as PGL) diagnostic messages from printer.

e.g. Capturing ODV barcode data and grade with the following printer menu settings:

  • "System/Printer Mgmt/Ret. Status Port" set to "E-NET Stat Port"
  • "System/Printer Mgmt/Status Port Number" set to 3002
  • "ODV2D/Control/Telemetry Data" set to "Full Report"
  • "ODV2D/Control/Return Data" set to "Data+Grade" for listening to "barcode data + grade"

◆ Descriptor

override string UniPRT.Sdk.Comm.TcpConnection.Descriptor => $"TCP:{_ipAddress}:{_port.ToString()}"

返回连接的字符串描述。格式:"TCP:ip:port" 例如 "TCP:127.0.0.1:9100"

返回格式:

  • <type>:<ip>:<port>
    • IP地址和特定端口号的格式为 "TCP":#.#.#.#:# 例如 TCP:127.0.0.1:9100
    • IP和端口为整数
    • 注意,连接的 type 为 "TCP"。

注意返回字符串中的 "TCP" 前缀表示连接的类型。

UniPRT
提供对不同通信接口/端口的支持
Definition: Communication.cs:9
UniPRT.Sdk.Comm
Definition: Communication.cs:9
UniPRT.Sdk
Definition: Communication.cs:9
UniPRT.Sdk.Comm.TcpConnection.TcpConnection
TcpConnection(string descriptor)
根据 descriptor 字符串初始化新类实例。
Definition: TcpComm.cs:301