Table of Contents

Class CommunicationBase

Namespace
Loehnert.Lisrt.Communication
Assembly
Loehnert.Lisrt.Communication.dll

Represents a base class for message based communication connections using a Stream.

public abstract class CommunicationBase : LisrtModule, ICommunication, ILisrtModule, IInitializable, INotifyPropertyChangedEx, INotifyPropertyChanged, IHasServiceView, IDisposable
Inheritance
PropertyChangedBase
CommunicationBase
Implements
INotifyPropertyChangedEx
Derived
Inherited Members
PropertyChangedBase.Refresh()
PropertyChangedBase.IsNotifying
PropertyChangedBase.PropertyChanged

Remarks

Use this class for message-based communication using a Stream instance. A message starts with an optional StartDelimiter and ends with the required EndDelimiter. To use this class for a non-message-based communication, set the EndDelimiter to an empty array, then the DataReceived event is fired immediately when any bytes are received.

To convert the sent and received bytes to strings, it is necessary to set the Encoding property.

Constructors

CommunicationBase(string)

Initializes a new instance of the CommunicationBase class.

protected CommunicationBase(string moduleName)

Parameters

moduleName string

The name for the module.

Fields

InfiniteTimeout

Gets an infinite value for Timeout.

public static readonly int InfiniteTimeout

Field Value

int

Properties

CommunicationLogIsEnabled

Gets or sets a value indicating whether the instance logs communication.

[Configuration(true)]
public bool CommunicationLogIsEnabled { get; set; }

Property Value

bool

Encoding

Gets or sets a encoding for sending and receiving.

public Encoding Encoding { get; set; }

Property Value

Encoding

EndDelimiter

Gets or sets a string for ending a transmission.

public virtual byte[] EndDelimiter { get; set; }

Property Value

byte[]

Remarks

This is added at the end of a message send with the Send(byte[], string) or SendString(string, string) method.

InitializationState

Gets or sets a value indicating whether the object is initialized.

[DoNotNotify]
public InitializationState InitializationState { get; protected set; }

Property Value

InitializationState

IsConnected

Gets a value indicating whether the module is connected.

public abstract bool IsConnected { get; }

Property Value

bool

StartDelimiter

Gets or sets a string for starting a transmission.

public virtual byte[] StartDelimiter { get; set; }

Property Value

byte[]

Remarks

This is added at the beginning of a message send with the Send(byte[], string) or SendString(string, string) method.

Exceptions

InvalidOperationException

Cannot be set when IsConnected is true.

Stream

Gets the stream for receive and write operations.

protected abstract Stream Stream { get; }

Property Value

Stream

Timeout

Gets or sets the number of milliseconds before a timeout occurs when a send or receive operation does not finish. Changes only will affect after initialization.

[Configuration(1000, Unit = "ms", Description = "-1 = Infinite Timeout")]
public int Timeout { get; set; }

Property Value

int

Remarks

Value InfiniteTimeout sets it to infinite.

Methods

ClearBuffer()

Clears the internal buffer.

public virtual void ClearBuffer()

Close()

Closes the session.

public abstract void Close()

Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

public void Dispose()

Exit()

De initializes the module.

public virtual void Exit()

Initialize()

Initializes the module, if InitializationState is not Initialized.

public virtual void Initialize()

Exceptions

InvalidOperationException

Throw when EndDelimiter is null or has a length less than 1.

InitializeStream()

Initializes the Stream while setting the Timeout and starting to read.

protected void InitializeStream()

Open()

Opens the session.

public abstract void Open()

Remarks

A implementation of this method, must handle the Enabled property.

Query(byte[], string)

Clears the received buffer, sends data and returns received data as bytes.

public byte[] Query(byte[] data, string logComment = "")

Parameters

data byte[]

The data bytes to send.

logComment string

A comment for the communication log.

Returns

byte[]

The result data bytes.

Exceptions

ArgumentNullException

Thrown when data is null.

PortNotOpenException

Thrown when the port is not open.

TimeoutException

Thrown when no data are received before timeout.

InvalidDataException

Thrown when data contain StartDelimiter or EndDelimiter.

QueryString(string, string)

Clears the received buffer, sends data and returns received data.

public string QueryString(string data, string logComment = "")

Parameters

data string

The data to send.

logComment string

A comment for the communication log.

Returns

string

The result data.

Exceptions

ArgumentNullException

Thrown when data is null.

PortNotOpenException

Thrown when the port is not open.

TimeoutException

Thrown when no data are received before timeout.

InvalidDataException

Thrown when data contain StartDelimiter or EndDelimiter.

Receive()

Receives the data bytes.

public virtual byte[] Receive()

Returns

byte[]

The received data bytes or null if buffer is empty.

Exceptions

PortNotOpenException

Thrown when the port is not open.

TimeoutException

Thrown when no data are received before timeout.

ReceiveString()

Receives the data.

public virtual string ReceiveString()

Returns

string

The received data.

Exceptions

PortNotOpenException

Thrown when the port is not open.

InvalidOperationException

Thrown when Encoding is null.

TimeoutException

Thrown when no data are received before timeout.

Send(byte[], string)

Sends the data.

public virtual void Send(byte[] data, string logComment = "")

Parameters

data byte[]

The data to send.

logComment string

A comment for the communication log.

Exceptions

ArgumentNullException

Thrown when data is null.

PortNotOpenException

Thrown when the port is not open.

InvalidDataException

Thrown when data contain StartDelimiter or EndDelimiter.

SendString(string, string)

Sends a string, converted to bytes using the Encoding property.

public virtual void SendString(string data, string logComment = "")

Parameters

data string

The string to send.

logComment string

A comment for the communication log.

Exceptions

ArgumentNullException

Thrown when data is null.

PortNotOpenException

Thrown when the port is not open.

InvalidOperationException

Thrown when Encoding is null.

ShowServiceView()

Shows the service view.

public abstract void ShowServiceView()

Events

DataReceived

Occurs when data have been received.

public event EventHandler<DataReceivedEventArgs> DataReceived

Event Type

EventHandler<DataReceivedEventArgs>

See Also