Communicator Class

A thread safe abstract communicator. Please consider to change a protocol before any modification of this class.

Definition

Namespace: OddPrg.Communication
Assembly: OddPrg.Communication (in OddPrg.Communication.dll) Version: 2.0.0.2 (2.0.0.2)
C#
public abstract class Communicator : IDisposable
Inheritance
Object    Communicator
Derived
More
Implements
IDisposable

Constructors

Communicator Allows descendants to initialize communicator properties.

Properties

Active true - communicator is sending or receiving data false - communicator does not perform communication at this time
Connected true - communicator is connected and ready to send and receive data false - communicator is not connected
ConnectionParams Communicator connection parameters. They must be assigned before calling Connect method.
LastError Returns kind of last error stored in RaiseComException(). If no error has occured, ComExceptionKind.Unknow is returned. You may test LastRequestFailed to indicate if some error has occured.
LastRequestFailed Indicates if last request/response operation failed.
Name Name of the communicator
rsNoResponse 
rsUserTermination 
TimeoutExpired Indicates if last request/response operation timed out.
TotalBytesTransfered Number of bytes transfered beginning since construction of communicator or since call of ClearTransferCounters.
TotalTransferTime Number of milliseconds that consumed in/out byte transfer since construction of communicator or since call of ClearTransferCounters.

Methods

BeginWaiting Resets waiting events. This method is used to initialize usage of WaitForWithoutSend which performs continuous waiting for incoming data stream.
ClearTransferCounters Clears total bytes transfered and total transfer time.
Connect(CancellationToken) Call this method to connect to a remote device with standard "connecting" message displayed in a log.
Connect(String, CancellationToken) Call this method to connect to a remote device. It must be called before any sending operation unless AutoConnect is set to true.
Disconnect Call this method to disconnect from a remote device with standard "disconnecting" message displayed in a log.
Disconnect(String) Call this method to disconnect from a remote device.
Dispatch Call this method from a data receive handler to accept new incoming data and to add them to the receive buffer and test if all data are received.
DisplayReceivedBytes Displays all received bytes that were processed by Dispatch method in a log.
Dispose IDisposable.Dispose implementation - Resets instance members
EndWaiting Sets waiting event, escapes the waiting loop and displays possible error message.
GetCurrentTime Function that returns current date/time. It should be used insted of DateTime.Now in all communicator methods.
GetDefaultStartReceiveTimeout Returns maximal time to wait for the first response counted after a request is sent. This method should be used to compute StartReceiveTimeout and NextReceiveTimeout before every communication id CheckDataReceive is set to true.
GetDefaultTimeout Returns the maximal time for communication with a device. TimeoutScale and ReplyDelay properties will be used for computation. This method should be used to compute Rimeout before every communication id CheckTimeout is set to true.
GetDetailedInfo Returns a string with detailed communicator settings including information about timeouts.
GetInfo Returns a string with brief description of current communicator settings.
GetParamsDetailedInfo Returns a detailed description of communicator configuration. If it is not overriden, it returns list of names and values of all connection params and it includes also timeouts.
GetParamsInfo Returns a brief description of communicator configuration. If it is not overriden, it returns a comma delimited string containing all values of connection params.
PrepareReceived Clears receive buffers.
PrepareSent Stores copy of data to sent for further echo check. Displays bytes to send in a log.
RaiseComException(String) Displays error message and raises the unknown communication exception.
RaiseComException(String, ComExceptionComExceptionKind) Displays error message and raises the communication exception.
RaiseComException(String, Exception) Displays error message and raises the communication exception.
RaiseComException(String, ComExceptionComExceptionKind, Exception) Displays error message without description and raises the communication exception.
RaiseComException(String, Boolean, ComExceptionComExceptionKind) Displays error message and raises the communication exception.
RaiseComException(String, Boolean, ComExceptionComExceptionKind, Exception) Displays error message and raises the communication exception.
RefreshState Refreshes a displaying of communicator status.
Send(ByteBuffer, CancellationToken) Sends data to a remote device and returns. This method does not wait for a response.
Send(String, CancellationToken) Sends data to a remote device and returns. This method does not wait for a response.
Send(ByteBuffer, ByteBuffer, CancellationToken) Sends data to a remote device and returns. This method does not wait for a response.
ShowProgress Shows communication progress message usualy used to write a message into a log.
Wait Waits required count of milliseconds. Waiting loop can be breaked via OnTestTerminate callback only. All incomming data are ignored and not displayed in a log.
WaitFor(ByteBuffer, WaitingTerminateProc, CancellationToken) Sends specified data and waits for the response. The waiting loop will be terminated if waitingTerminateProc returns true. If ByteBuffer data is empty, nothing is sent. Incomming bytes are returned using parametr data (so data parameter cannot be null). The procedure clears ByteBuffer data before start of receiving. If multiple incomming streams should be received without sending output bytes, WaitForWithoutSend method should be used.
WaitFor(String, WaitingTerminateProc, CancellationToken) Sends specified data and waits for the response. The waiting loop will be terminated if waitingTerminateProc returns true. If string data is empty, nothing is sent. Incomming chars are returned using parametr data (so data parameter cannot be null).
WaitFor(ByteBuffer, WaitingTerminateProc, ByteBuffer, CancellationToken) Sends specified data and waits for the response. The waiting loop will be terminated if waitingTerminateProc returns true. If ByteBuffer data is empty, nothing is sent. Incomming bytes are returned using parametr data (so data parameter cannot be null). The procedure clears ByteBuffer data before start of receiving. If multiple incomming streams should be received without sending output bytes, WaitForWithoutSend method should be used.
WaitForAnyData Sends content of data argument and waits until some data are received. Incomming data are returned in the data argument. This procedure is useful when data are sent/received in whole packets (e.g. HTTP).
WaitForByte(ByteBuffer, Byte, CancellationToken) Sends the content of data argument and waits until specified byte is received. Incomming data are returned in the data argument.
WaitForByte(ByteBuffer, Byte, CancellationToken) Sends the content of data argument and waits until any byte from byteSet array is received. Incomming data are returned in the data argument.
WaitForBytes Sends the content of data argument and waits until specified byte sequence is received. Incomming data are returned in the data argument.
WaitForChar(ByteBuffer, Char, CancellationToken) Sends the content of data argument and waits until specified char is received. Incomming data are returned in the data argument.
WaitForChar(ByteBuffer, Char, CancellationToken) Sends the content of data argument and waits until any char from charSet array is received. Incomming data are returned in the data argument.
WaitForChar(String, Char, CancellationToken) Sends the content of data argument and waits until specified char is received. Incomming data are returned in the data argument.
WaitForChar(String, Char, CancellationToken) Sends the content of data argument and waits until any char from charSet array is received. Incomming data are returned in the data argument.
WaitForLength Sends the content of data argument and waits until specified count of bytes is received. Incomming data are returned in the data argument.
WaitForStr(ByteBuffer, String, CancellationToken) Sends the content of data argument and waits until specified string is received. Incomming data are returned in the data argument. Incoming bytes are transformed using InputEncoding.
WaitForStr(String, String, CancellationToken) Sends the content of data argument and waits until specified string is received. Incomming data are returned in the data argument. Incoming bytes are transformed using InputEncoding.
WaitForStrings(ByteBuffer, String, CancellationToken) Sends the content of data argument and waits until any string from specified list is received. Incomming data are returned in the data argument. Incoming bytes are transformed using InputEncoding.
WaitForStrings(ByteBuffer, String, CancellationToken) Sends the content of data argument and waits until any string from specified list is received. Incomming data are returned in the data argument. Incoming bytes are transformed using InputEncoding.
WaitForStrings(String, String, CancellationToken) Sends the content of data argument and waits until any string from specified list is received. Incomming data are returned in the data argument. Incoming bytes are transformed using InputEncoding.
WaitForStrings(String, String, CancellationToken) Sends the content of data argument and waits until any string from specified list is received. Incomming data are returned in the data argument. Incoming bytes are transformed using InputEncoding.
WaitForStrings(ByteBuffer, String, ByteBuffer, CancellationToken) Sends the content of data argument and waits until any string from specified list is received. Incomming data are returned in the data argument. Incoming bytes are transformed using InputEncoding.
WaitForTime Sends the content of data argument and waits required count of milliseconds. Waiting loop can be breaked via OnTestTerminate callback only. Incomming data are returned in the data argument.
WaitForWithoutSend Receives incomming data until waitingTerminate returns true. Input buffer is not cleared, incomming bytes are appended to the input buffer content, so the method can be called multiple times. If some data were received in the meantime and waitingTerminate returns true for that data, method returns immediately to process returned data. Method waitingTerminate checks always whole receive buffer. Before call of WaitForwithoutSend, waiting must be initialized by calling BeginWaiting wich sets up timeout-check events. Todo: BeginWaiting ands WaitForWithoutSend can be unified in one method - ???

Fields

AfterDataReceived Performs some action after the communicator has recieved the data. If this procedure throws an exception, it has no effect. The parameter contains the received data.
AutoConnect If it is true, the communicator calls Connect method before any send/receive operation. After finishing this operation it calls Disconnect method.
AutoConnectRepeatCount The maximal connection retries per one communication. The number of all connection tries is AutoConnectRepeatCount + 1. Applies only if AutoConnect is true.
BeforeDataSend Performs some action before sending the data throught the communicator. If this procedure throws an exception, it has no effect. The parametr contains data that should be send.
ByteDisplayMode Gets/Sets How to display sent and received bytes in a communication log. Default value is hexadecimal separated by |.
ClearEcho If it is true, echoed data from the beginning of receive buffer are cleared (but all data are displayed in a log)
DefaultByteDisplayMode Default value of ByteDisplayMode - each byte hexadecimal value, separated by '|'.
DeviceName The name of a device to communicate with. Used in error messages.
DisplayBytesLength Count of bytes to display on a single line in a log. It is applicable only if DefaultByteDisplayMode is set to ByteDisplayModes.Hex.
DisplayCycleCount If this value is equal to displayCycleCountWholeLine communicator waits for whole line (DisplayCycleCount bytes) to display. If this value is equal to e.g. 3, communicator displays all received bytes every 3 cycles of waiting loop.
DisplaySystemChars Applies to ByteDisplayMode is Chars or Lines. Whether to display characters lower than 32. These characters are displayed as names surroundex in < and >.
DisplayTicks Whether to display time from creating a communicator for each line in communication log.
DoEvents If the communicator runs in main application thread, use this event to call Application.DoEvents. The reason is that if it runs as a part of some service, the namespace System.Windows.Forms.Application could not be accessible to call Application.DoEvents.
CheckDataReceive Whether "Device is not responding" or "Reply is invalid or incomplete" errors will be raised.
CheckTimeout Whether "Timeout expired" errors will be raised.
IgnoreNoResponse Whether not to display "No data received" message.
InputEncoding Encoding used to convert incomming bytes to string. It is used to display correct characters in a log if ByteDisplayMode is set to Chars or Lines.
InputLineSeparator Line separators for input to control displaying bytes if ByteDisplayMode is equal to ByteDisplayModes.Lines
NextReceiveTimeout The count of miliseconds to display error "Reply is invalid or incomplete" if no data comes in time counted from a previos data receive. It is applied only if CheckTimeout is set to true.
NoTerminateTest Whether to check a user termination. It could be useful for some disconnect operations when user termination is not desirable.
OnActivityChange Event raised when communicator activity is changed. It can be uset to enable/disable user controls. Remember to use BeginInvoke in this procedure if it changes UI and communication runs in a separate thread.
OnAfterConnect Performs some special action (e.g. authentication) after the communicator is successfully connected. This procedure should throw ComException if the current opening operation was not successfull, which aborts the communicator connection.
OnGetCurrentTime Procedure that returns current date/time. This procedure can be set if other time than DateTime.Now needs to be returned as current date/time. If this property is null, DateTime.Now is used as current date/time.
OnShowProgress Event to display communication progress in a log. Remember to use BeginInvoke in this procedure if it changes UI and communication runs in a separate thread.
OnShowState Event tot display a communicator status. It can be used e.g. to display in status bar. Remember to use BeginInvoke in this procedure if it changes UI and communication runs in a separate thread.
OutputEncoding Applies to output/input of string data. It is used to display correct characters in a log if ByteDisplayMode is set to Chars or Lines.
OutputLineSeparator Line separators for output to control displaying bytes if ByteDisplayMode is equal to ByteDisplayModes.Lines
ReplyDelay Device reaction time in miliseconds. Used in GetDefaultTimeout to compute a communication timeout.
SleepTime Sleep time in every repeat of main waiting loop in miliseconds.
StartReceiveTimeout The count of miliseconds to display error "Device is not responding" if no data comes after sending a request. It can be computed using GetDefaultStartReceiveTimeout method. It is applied only if CheckTimeout is set to true.
Timeout The count of miliseconds to display error "Timeout expired" counted from sending a request. It must be initialized before every communication. It can be computed using GetDefaultTimeout method. It is applied only if CheckTimeout is set to true.
TimeoutScale Multiplier for time to send and receive. Used in GetDefaultTimeout to compute a communication timeout. This value is expected to be greater or equal to 1 (1 = exact expected time).
WaitBeforeSend Wait miliseconds in before sending any data. It can be used for protocols that require it by definition, e.g. M-bus needs 67 ms.

See Also