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
-
PropertyChangedBaseCommunicationBase
- Implements
-
INotifyPropertyChangedEx
- Derived
- Inherited Members
-
PropertyChangedBase.Refresh()PropertyChangedBase.IsNotifyingPropertyChangedBase.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
stringThe name for the module.
Fields
InfiniteTimeout
Gets an infinite value for Timeout.
public static readonly int InfiniteTimeout
Field Value
Properties
CommunicationLogIsEnabled
Gets or sets a value indicating whether the instance logs communication.
[Configuration(true)]
public bool CommunicationLogIsEnabled { get; set; }
Property Value
Encoding
Gets or sets a encoding for sending and receiving.
public Encoding Encoding { get; set; }
Property Value
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
IsConnected
Gets a value indicating whether the module is connected.
public abstract bool IsConnected { get; }
Property Value
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
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
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()
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
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
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
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
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