UniPRT SDK  v2.0.0.3
UniPRT.Sdk.Comm.UsbConnection Class Reference

Class that implements the USB specific communication and extends the AComm class. More...

Inheritance diagram for UniPRT.Sdk.Comm.UsbConnection:
UniPRT.Sdk.Comm.AComm UniPRT.Sdk.Comm.IComm

Public Member Functions

 UsbConnection ()
 Initialize new class instance based on vendorId and productId. The target of this instance is the first printer that matches the vendorId and productId. More...
 
 UsbConnection (short vendorId)
 
 UsbConnection (short vendorId, short productId)
 
 UsbConnection (string descriptor)
 Initialize new class instance based on descriptor string. More...
 
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)
 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...
 
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)
 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 List<(short vendorID, short productID)> AvaliableDevices ()
 Return the v_id and p_id of valid devices in the format of List<(short v_id, short p_id)>. More...
 
static List< short > DescriptorValidate (string descriptorHint)
 Validate descriptor string from descriptorHint that may or may not have a v_id and p_id. Returns tuple. More...
 

Public Attributes

override string Descriptor => $"USB:V_ID:{_v_id}; P_ID:{_p_id}"
 Returns a string description of the connection. Format: "USB:V_ID:_v_id; P_ID:_p_id" e.g. "USB:V_ID:4611; P_ID:1234". More...
 

Static Public Attributes

static readonly short PTX_USB_VID = 0x14ae
 USB v_id of PRINTRONIX printer (0x14ae)
 
static readonly short TSC_USB_VID = 0x1203
 USB v_id of TSC printer (0x1203)
 

Properties

override int BytesAvailable [get]
 
override bool Connected [get]
 
short P_ID [get]
 Product ID.
 
short V_ID [get]
 Vendor ID.
 

Detailed Description

Class that implements the USB specific communication and extends the AComm class.

Examples

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)
{
SendPrintFile(); // send file over usb
SendPrintString(); // send print data over usb
}
public static void SendPrintFile() // send file over usb
{
var devices = UsbConnection.AvaliableDevices();
if (devices.Count > 0)
{
string fileName = @"C:\testFiles\Hello.pgl";
UsbConnection PtrUsbComm = new UsbConnection(devices[0].vendorID, devices[0].productID);
try
{
PtrUsbComm.Open();
if (File.Exists(fileName))
{
using (BinaryReader binReader = new BinaryReader(File.Open(fileName, FileMode.Open)))
{
Console.WriteLine($"Sending \"{fileName}\" to printer");
PtrUsbComm.Write(binReader);
}
}
else
{
Console.WriteLine($"File \"{fileName}\" not found");
}
}
catch (Exception e)
{
Console.WriteLine($"Exception Msg: {e.Message}");
}
finally
{
PtrUsbComm.Close();
}
}
}
public static void SendPrintString() // send print data over usb
{
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
";
var devices = UsbConnection.AvaliableDevices();
if (devices.Count > 0)
{
UsbConnection PtrUsbComm = new UsbConnection(devices[0].vendorID, devices[0].productID);
try
{
PtrUsbComm.Open();
if (PtrUsbComm.Connected)
{
//byte[] outBytes = Encoding.UTF8.GetBytes(dataToPrint);
byte[] outBytes = Encoding.ASCII.GetBytes(dataToPrint);
PtrUsbComm.Write(outBytes);
}
else
{
Console.WriteLine($"Not connected to printer");
}
}
catch (Exception e)
{
Console.WriteLine($"Exception Msg: {e.Message}");
}
finally
{
PtrUsbComm.Close();
}
}
}
}
}

Constructor & Destructor Documentation

◆ UsbConnection() [1/2]

UniPRT.Sdk.Comm.UsbConnection.UsbConnection ( string  descriptor)

Initialize new class instance based on descriptor string.

Parameters
[in]descriptorAcceptable Formats:
  • USB
  • USB:V_ID:<v_id>
  • USB:V_ID:<v_id>; P_ID:<p_id>
    • <v_id> and <p_id> allow decimal or hexadecimal

◆ UsbConnection() [2/2]

UniPRT.Sdk.Comm.UsbConnection.UsbConnection ( )

Initialize new class instance based on vendorId and productId. The target of this instance is the first printer that matches the vendorId and productId.

Parameters
[in]vendorIdSpecifies whether a TSC printer or a PRINTRONIX printer is to be connected. When this parameter is omitted, either a TSC printer or a PRINTRONIX printer is the target
[in]productIdSpecify the productId of the printer. When this parameter is omitted, the first printer whose vendorId matches is the target

Member Function Documentation

◆ AvaliableDevices()

static List<(short vendorID, short productID)> UniPRT.Sdk.Comm.UsbConnection.AvaliableDevices ( )
static

Return the v_id and p_id of valid devices in the format of List<(short v_id, short p_id)>.

The "v_id" refers to the Vendor ID, and "p_id" refers to the Product ID.

◆ DescriptorValidate()

static List<short> UniPRT.Sdk.Comm.UsbConnection.DescriptorValidate ( string  descriptorHint)
static

Validate descriptor string from descriptorHint that may or may not have a v_id and p_id. Returns tuple.

descriptorHint accepted values:

  • Not specified: "USB"
  • Specified V_ID: "USB:V_ID:4611" or "USB:V_ID:0x1203"
  • Specified V_ID and P_ID: "USB:V_ID:4611; P_ID:1234" or "USB:V_ID:0x1203; P_ID:0x04D2"

◆ WaitForData()

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

Waits until data available BytesAvailable in current thread. Current thread sleeps until data received or timeout reached. Blocking call.

Parameters
[in]msTimeOutMillisecond timeout before return if no data received.

Implements UniPRT.Sdk.Comm.IComm.

◆ Write()

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

Write from input stream to output stream

Parameters
[in]binReaderSource data to send

Implements 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

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.

Parameters
[out]binDataInDestination stream to store response received from connection.
[in]binDataOut
Source stream data to write to connection.
[in]responseStartTimeOutTimeout, milliseconds, to wait for first data to be received. Time to first byte.
[in]responseEndTimeOutTimeout, milliseconds, of no new data received. Time after last byte.
[in]completetionTokenString token denoting the end of the response expected. This can be empty string if simply want to wait for response after writing data to connection.

Implements UniPRT.Sdk.Comm.IComm.

◆ WriteAndWaitForResponse() [2/2]

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

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.

Return values
byte[]Array of bytes received from connection.
Data returned is any data received or up to completetionToken if token received
Parameters
[in]dataOut
Source byte array to write to connection.
[in]responseStartTimeOutTimeout, milliseconds, to wait for first data to be received. Time to first byte.
[in]responseEndTimeOutTimeout, milliseconds, of no new data received. Time after last byte.
[in]completetionTokenString token denoting the end of the response expected. This can be empty string if simply want to wait for response after writing data to connection.

Implements UniPRT.Sdk.Comm.IComm.

Member Data Documentation

◆ Descriptor

override string UniPRT.Sdk.Comm.UsbConnection.Descriptor => $"USB:V_ID:{_v_id}; P_ID:{_p_id}"

Returns a string description of the connection. Format: "USB:V_ID:_v_id; P_ID:_p_id" e.g. "USB:V_ID:4611; P_ID:1234".

Format returned:

  • USB:V_ID:<v_id>; P_ID:<p_id>
    • p_id and v_id are short numbers. e.g. V_ID:4611; P_ID:1234
UniPRT
Provide support for different communication interfaces/ports.
Definition: Communication.cs:9
UniPRT.Sdk.Comm
Definition: Communication.cs:9
UniPRT.Sdk
Definition: Communication.cs:9
UniPRT.Sdk.Comm.UsbConnection.UsbConnection
UsbConnection()
Initialize new class instance based on vendorId and productId. The target of this instance is the fir...
Definition: UsbComm.cs:406