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

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

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

公開型態

enum  DescriptorPortType { DATA, MGMT, STATUS }
 用於請求帶有默認端口值的描述符的類型
 

公開方法(Public Methods)

 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)
 將字節數據寫入輸出流並返回接收到的數據。返回的數據是接收到的任何數據或達到接收的完成令牌。在返回前等待回應超時。 更多...
 

靜態公開方法(Static Public Methods)

static string DescriptorValidate (string descriptorHint, DescriptorPortType portTypeHint)
 驗證/建立從 descriptorHint 中可能有或沒有端口的描述符字符串。返回有效的描述符或空字符串 ""。 更多...
 

公開屬性

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

靜態公開屬性

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. 更多...
 

屬性(properties)

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