Auto ID SDK v1.00 (beta) 5/15/2022
TcpConnection Class Reference

Class that implements the TCP expecific communication and extends the AComm class. More...

Inheritance diagram for TcpConnection:
AComm IComm

Public Types

enum  DescriptorPortType { DATA , MGMT , STATUS }
 Types for requesting descriptiors with default port values.
 

Public Member Functions

 TcpConnection (string descriptor)
 Initialize new class instance based on descriptor string. More...
 
 TcpConnection (string IpAddress, int port)
 Initialize new class instance.
 
override void Close ()
 Closes a connection. More...
 
override void Open ()
 Opens a connection. More...
 
override byte[] Read ()
 Read all bytes available. More...
 
override void Write (byte[] dataOut)
 Writes all bytes from the array passed in. More...
 
- Public Member Functions inherited from AComm
virtual void Read (BinaryWriter binDataIn)
 Reads all bytes available into the stream passed in. More...
 
virtual void WaitForData (int msTimeOut)
 Waits until data available BytesAvailable in current thread. Current thread sleeps until data received or timeout reached. Blocking call.
More...
 
virtual void Write (BinaryReader binReader)
 Write from input stream to output stream
More...
 
virtual void WriteAndWaitForResponse (BinaryWriter binDataIn, BinaryReader binDataOut, int responseStartTimeOut, int responseEndTimeOut, string completetionToken)
 Write binDataOut stream data to output stream and return data received in binDataIn stream. Data returned is any data received or up to completion token if received. Wait for response timeout before returning. More...
 
virtual byte[] WriteAndWaitForResponse (byte[] dataOut, int responseStartTimeOut, int responseEndTimeOut, string completetionToken)
 Write byte data to output stream and return data received. Data returned is any data received or up to completion token if received. Wait for response timeout before returning. More...
 

Static Public Member Functions

static string DescriptorValidate (string descriptorHint, DescriptorPortType portTypeHint)
 Validate/build descriptor string from descriptorHint that may or may not have a port. More...
 

Static Public Attributes

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. More...
 

Properties

override int BytesAvailable [get]
 
override bool Connected [get]
 
override string Descriptor [get]
 Returns a string description of the connection. Format: "TCP:ip:port" e.g. "TCP:127.0.0.1:9100". More...
 
string IpAddress [get]
 IP address of connection.
 
int Port [get]
 Port Number of connection.
 
- Properties inherited from AComm
abstract int BytesAvailable [get]
 
abstract bool Connected [get]
 
abstract string Descriptor [get]
 
- Properties inherited from IComm
int BytesAvailable [get]
 Number of bytes available to read. This is estimate and number can change as data is received.
 
bool Connected [get]
 true if connection is established
 
string Descriptor [get]
 string descriptor of the connection.
 

Detailed Description

Class that implements the TCP expecific communication and extends the AComm class.

Examples

using System;
using System.IO;
using System.Text;
using AutoId.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(string descriptor)
Initialize new class instance based on descriptor string.
Definition: TcpComm.cs:187
Provide support for different communication interfaces/ports.
Definition: TcpComm.cs:14
Definition: TcpComm.cs:14
Definition: TcpComm.cs:14


Constructor & Destructor Documentation

◆ TcpConnection()

TcpConnection ( string  descriptor)
inline

Initialize new class instance based on descriptor string.

Parameters
[in]descriptorColon (:) separated string description of connection.
Acceptable Formats:
  • <ip>
    • IP address in #.#.#.# format. e.g. 127.0.0.1
    • Uses default data port (DEFAULT_DATA_PORT)
  • <ip>:<port>
    • IP address and specific port number in #.#.#.#:# format. e.g. 127.0.0.1:9100
  • <type>:<ip>:<port>
    • IP address and specific port number in "TCP":#.#.#.#:# format. e.g. TCP:127.0.0.1:9100
    • Note that the type of connection will always be of "TCP" independent of what is passed in.

Member Function Documentation

◆ Close()

override void Close ( )
inlinevirtual

Closes a connection.

Implements AComm.

◆ DescriptorValidate()

static string DescriptorValidate ( string  descriptorHint,
DescriptorPortType  portTypeHint 
)
inlinestatic

Validate/build descriptor string from descriptorHint that may or may not have a port.

descriptorHint accepted values:

  • Ip only: #.#.#.#
  • Ip + Port: #.#.#.#:p where p = port number
  • "TCP:" + Ip + Port: "TCP:#.#.#.#:p" where p = port number

◆ Open()

override void Open ( )
inlinevirtual

Opens a connection.

Implements AComm.

◆ Read()

override byte[] Read ( )
inlinevirtual

Read all bytes available.

Implements AComm.

◆ Write()

override void Write ( byte[]  dataOut)
inlinevirtual

Writes all bytes from the array passed in.

Implements AComm.

Member Data Documentation

◆ DEFAULT_STATUS_PORT

readonly int 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"

Property Documentation

◆ Descriptor

override string Descriptor
get

Returns a string description of the connection. Format: "TCP:ip:port" e.g. "TCP:127.0.0.1:9100".

Format returned:

  • <type>:<ip>:<port>
    • IP address and specific port number in "TCP":#.#.#.#:# format. e.g. TCP:127.0.0.1:9100
    • IP and port are integer numbers
    • Note that the type of connection is "TCP" .

Note that "TCP" prefix on returned string to denote the type of connection.