Class CommunicationBase
Represents a base class for message based communication connections using a System.IO.Stream.
Inheritance
Implements
Inherited Members
Namespace: Loehnert.Lisrt.Communication
Assembly: Loehnert.Lisrt.Communication.dll
Syntax
public abstract class CommunicationBase : LisrtModule, ICommunication, ILisrtModule, IInitializable, INotifyPropertyChangedEx, INotifyPropertyChanged, IHasServiceView, IDisposable
Remarks
Use this class for message-based communication using a 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.
Declaration
protected CommunicationBase(string moduleName)
Parameters
Type | Name | Description |
---|---|---|
String | moduleName | The name for the module. |
Fields
InfiniteTimeout
Gets an infinite value for Timeout.
Declaration
public static readonly int InfiniteTimeout
Field Value
Type | Description |
---|---|
System.Int32 |
Properties
CommunicationLogIsEnabled
Gets or sets a value indicating whether the instance logs communication.
Declaration
[Configuration(true)]
public bool CommunicationLogIsEnabled { get; set; }
Property Value
Type | Description |
---|---|
Boolean |
Encoding
Gets or sets a encoding for sending and receiving.
Declaration
public Encoding Encoding { get; set; }
Property Value
Type | Description |
---|---|
System.Text.Encoding |
EndDelimiter
Gets or sets a string for ending a transmission.
Declaration
public virtual byte[] EndDelimiter { get; set; }
Property Value
Type | Description |
---|---|
System.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.
Declaration
[DoNotNotify]
public InitializationState InitializationState { get; protected set; }
Property Value
Type | Description |
---|---|
InitializationState |
IsConnected
Gets a value indicating whether the module is connected.
Declaration
public abstract bool IsConnected { get; }
Property Value
Type | Description |
---|---|
Boolean |
StartDelimiter
Gets or sets a string for starting a transmission.
Declaration
public virtual byte[] StartDelimiter { get; set; }
Property Value
Type | Description |
---|---|
System.Byte[] |
Remarks
This is added at the beginning of a message send with the Send(Byte[], String) or SendString(String, String) method.
Exceptions
Type | Condition |
---|---|
System.InvalidOperationException | Cannot be set when IsConnected is true. |
Stream
Gets the stream for receive and write operations.
Declaration
protected abstract Stream Stream { get; }
Property Value
Type | Description |
---|---|
System.IO.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.
Declaration
[Configuration(1000, Unit = "ms", Description = "-1 = Infinite Timeout")]
public int Timeout { get; set; }
Property Value
Type | Description |
---|---|
System.Int32 |
Remarks
Value InfiniteTimeout sets it to infinite.
Methods
ClearBuffer()
Clears the internal buffer.
Declaration
public virtual void ClearBuffer()
Close()
Closes the session.
Declaration
public abstract void Close()
Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
Declaration
public void Dispose()
Exit()
De initializes the module.
Declaration
public virtual void Exit()
Initialize()
Initializes the module, if InitializationState is not Initialized.
Declaration
public virtual void Initialize()
Exceptions
Type | Condition |
---|---|
System.InvalidOperationException | Throw when EndDelimiter is null or has a length less than 1. |
InitializeStream()
Declaration
protected void InitializeStream()
Open()
Opens the session.
Declaration
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.
Declaration
public byte[] Query(byte[] data, string logComment = "")
Parameters
Type | Name | Description |
---|---|---|
System.Byte[] | data | The data bytes to send. |
String | logComment | A comment for the communication log. |
Returns
Type | Description |
---|---|
System.Byte[] | The result data bytes. |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | Thrown when |
PortNotOpenException | Thrown when the port is not open. |
System.TimeoutException | Thrown when no data are received before timeout. |
System.IO.InvalidDataException | Thrown when |
QueryString(String, String)
Clears the received buffer, sends data and returns received data.
Declaration
public string QueryString(string data, string logComment = "")
Parameters
Type | Name | Description |
---|---|---|
String | data | The data to send. |
String | logComment | A comment for the communication log. |
Returns
Type | Description |
---|---|
String | The result data. |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | Thrown when |
PortNotOpenException | Thrown when the port is not open. |
System.TimeoutException | Thrown when no data are received before timeout. |
System.IO.InvalidDataException | Thrown when |
Receive()
Receives the data bytes.
Declaration
public virtual byte[] Receive()
Returns
Type | Description |
---|---|
System.Byte[] | The received data bytes or null if buffer is empty. |
Exceptions
Type | Condition |
---|---|
PortNotOpenException | Thrown when the port is not open. |
System.TimeoutException | Thrown when no data are received before timeout. |
ReceiveString()
Receives the data.
Declaration
public virtual string ReceiveString()
Returns
Type | Description |
---|---|
String | The received data. |
Exceptions
Type | Condition |
---|---|
PortNotOpenException | Thrown when the port is not open. |
System.InvalidOperationException | Thrown when Encoding is null. |
System.TimeoutException | Thrown when no data are received before timeout. |
Send(Byte[], String)
Sends the data.
Declaration
public virtual void Send(byte[] data, string logComment = "")
Parameters
Type | Name | Description |
---|---|---|
System.Byte[] | data | The data to send. |
String | logComment | A comment for the communication log. |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | Thrown when |
PortNotOpenException | Thrown when the port is not open. |
System.IO.InvalidDataException | Thrown when |
SendString(String, String)
Sends a string, converted to bytes using the Encoding property.
Declaration
public virtual void SendString(string data, string logComment = "")
Parameters
Type | Name | Description |
---|---|---|
String | data | The string to send. |
String | logComment | A comment for the communication log. |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | Thrown when |
PortNotOpenException | Thrown when the port is not open. |
System.InvalidOperationException | Thrown when Encoding is null. |
ShowServiceView()
Shows the service view.
Declaration
public abstract void ShowServiceView()
Events
DataReceived
Occurs when data have been received.
Declaration
public event EventHandler<DataReceivedEventArgs> DataReceived
Event Type
Type | Description |
---|---|
System.EventHandler<DataReceivedEventArgs> |