SerialCommunicator Class

Threadsafe TCP communicator, it can send/receive data to/from TCP address and port

Definition

Namespace: OddPrg.Communication.Serial
Assembly: OddPrg.Communication.Serial (in OddPrg.Communication.Serial.dll) Version: 2.0.0.2 (2.0.0.2)
C#
public class SerialCommunicator : Communicator, 
	ISerialPort
Inheritance
Object    Communicator    SerialCommunicator
Implements
ISerialPort

Constructors

SerialCommunicator Initializes a new instance of the SerialCommunicator class. Attention - this method does not initialize the default settings. Please fill in the ConnectionParams before starting the communication. You can use the method fillConnectionParamsWithDefaultValues to fill in the default values.

Properties

Active true - communicator is sending or receiving data false - communicator does not perform communication at this time
(Inherited from Communicator)
Connected true - communicator is connected and ready to send and receive data false - communicator is not connected
(Inherited from Communicator)
ConnectionParams Communicator connection parameters. They must be assigned before calling Connect method.
(Inherited from Communicator)
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.
(Inherited from Communicator)
LastRequestFailed Indicates if last request/response operation failed.
(Inherited from Communicator)
Name Name of the communicator
(Inherited from Communicator)
TimeoutExpired Indicates if last request/response operation timed out.
(Inherited from Communicator)
TotalBytesTransfered Number of bytes transfered beginning since construction of communicator or since call of ClearTransferCounters.
(Inherited from Communicator)
TotalTransferTime Number of milliseconds that consumed in/out byte transfer since construction of communicator or since call of ClearTransferCounters.
(Inherited from Communicator)

Methods

BeginWaiting Resets waiting events. This method is used to initialize usage of WaitForWithoutSend which performs continuous waiting for incoming data stream.
(Inherited from Communicator)
ClearTransferCounters Clears total bytes transfered and total transfer time.
(Inherited from Communicator)
Connect(CancellationToken) Call this method to connect to a remote device with standard "connecting" message displayed in a log.
(Inherited from Communicator)
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.
(Inherited from Communicator)
Disconnect Call this method to disconnect from a remote device with standard "disconnecting" message displayed in a log.
(Inherited from Communicator)
Disconnect(String) Call this method to disconnect from a remote device.
(Inherited from Communicator)
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.
(Inherited from Communicator)
DisplayReceivedBytes Displays all received bytes that were processed by Dispatch method in a log.
(Inherited from Communicator)
Dispose IDisposable.Dispose implementation - Resets instance members
(Inherited from Communicator)
EndWaiting Sets waiting event, escapes the waiting loop and displays possible error message.
(Inherited from Communicator)
GetComParams 
GetCurrentTime Function that returns current date/time. It should be used insted of DateTime.Now in all communicator methods.
(Inherited from Communicator)
GetDefaultStartReceiveTimeout Calculates the default receive timeout in msec depending on the baud rate. Returns the time in which the reception should start.
(Overrides CommunicatorGetDefaultStartReceiveTimeout(Int32, Int32))
GetDefaultTimeout Calculates the default timeout in msec depending on the baud rate. Returns the time in which the reception should be finished.
(Overrides CommunicatorGetDefaultTimeout(Int32, Int32))
GetDetailedInfo Returns a string with detailed communicator settings including information about timeouts.
(Inherited from Communicator)
GetHandshakeFromParam 
GetInfo Returns a string with brief description of current communicator settings.
(Inherited from Communicator)
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.
(Inherited from Communicator)
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.
(Inherited from Communicator)
GetParityFromParam Vrati paritu znamou komponentě MegaPipeNetPC.PocketPC z parametrů komunikátoru.
GetStopBitsFromParam Vrati počet stop bitů srozumitelný komponentě MegaPipeNetPC.PocketPC.
ChangeBaudrate Změna rychlosti komunikace. Vrati true, pokud rychlost byla zmenena.
ChangeComParams 
ChangeDataBits Change the parameter DataBits of the COM port.
ChangeParity Meni paritu. Vraci true, pokud byla parita zmenena.
ChangeStopBits Meni pocet datovych bitu v seriove komunikaci
InitReadingProbe Special serial COM port init procedure for strange HW
PrepareReceived Clears receive buffers.
(Inherited from Communicator)
PrepareSent Stores copy of data to sent for further echo check. Displays bytes to send in a log.
(Inherited from Communicator)
RaiseComException(String) Displays error message and raises the unknown communication exception.
(Inherited from Communicator)
RaiseComException(String, ComExceptionComExceptionKind) Displays error message and raises the communication exception.
(Inherited from Communicator)
RaiseComException(String, Exception) Displays error message and raises the communication exception.
(Inherited from Communicator)
RaiseComException(String, ComExceptionComExceptionKind, Exception) Displays error message without description and raises the communication exception.
(Inherited from Communicator)
RaiseComException(String, Boolean, ComExceptionComExceptionKind) Displays error message and raises the communication exception.
(Inherited from Communicator)
RaiseComException(String, Boolean, ComExceptionComExceptionKind, Exception) Displays error message and raises the communication exception.
(Inherited from Communicator)
RefreshState Refreshes a displaying of communicator status.
(Inherited from Communicator)
Send(ByteBuffer, CancellationToken) Sends data to a remote device and returns. This method does not wait for a response.
(Inherited from Communicator)
Send(String, CancellationToken) Sends data to a remote device and returns. This method does not wait for a response.
(Inherited from Communicator)
Send(ByteBuffer, ByteBuffer, CancellationToken) Sends data to a remote device and returns. This method does not wait for a response.
(Inherited from Communicator)
SetDtr Nastaví nebo smaže DTR. Funguje pouze, pokud je otevřený COM.
SetRts Nastaví nebo smaže RTS. Funguje pouze, pokud je otevřený COM.
ShowProgress Shows communication progress message usualy used to write a message into a log.
(Inherited from Communicator)
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.
(Inherited from Communicator)
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.
(Inherited from Communicator)
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).
(Inherited from Communicator)
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.
(Inherited from Communicator)
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).
(Inherited from Communicator)
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.
(Inherited from Communicator)
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.
(Inherited from Communicator)
WaitForBytes Sends the content of data argument and waits until specified byte sequence is received. Incomming data are returned in the data argument.
(Inherited from Communicator)
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.
(Inherited from Communicator)
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.
(Inherited from Communicator)
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.
(Inherited from Communicator)
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.
(Inherited from Communicator)
WaitForLength Sends the content of data argument and waits until specified count of bytes is received. Incomming data are returned in the data argument.
(Inherited from Communicator)
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.
(Inherited from Communicator)
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.
(Inherited from Communicator)
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.
(Inherited from Communicator)
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.
(Inherited from Communicator)
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.
(Inherited from Communicator)
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.
(Inherited from Communicator)
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.
(Inherited from Communicator)
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.
(Inherited from Communicator)
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 - ???
(Inherited from Communicator)

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.
(Inherited from Communicator)
AutoConnect If it is true, the communicator calls Connect method before any send/receive operation. After finishing this operation it calls Disconnect method.
(Inherited from Communicator)
AutoConnectRepeatCount The maximal connection retries per one communication. The number of all connection tries is AutoConnectRepeatCount + 1. Applies only if AutoConnect is true.
(Inherited from Communicator)
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.
(Inherited from Communicator)
ByteDisplayMode Gets/Sets How to display sent and received bytes in a communication log. Default value is hexadecimal separated by |.
(Inherited from Communicator)
ClearEcho If it is true, echoed data from the beginning of receive buffer are cleared (but all data are displayed in a log)
(Inherited from Communicator)
ComBaudRate Index of the parameter "BaudRate" in the ConnectionParams
ComDataBits Index of the parameter "DataBits" in the ConnectionParams
ComDoneDelay Index of the parameter "DoneDelay" in the ConnectionParams
ComDTREnable Index of the parameter "DTREnable" in the ConnectionParams
ComHandshake Index of parameter "Handshake".
ComIgnorePortErrors If this parameter is true, prot errors are displayed in the log but do not stop communication.
ComInitDelay Index of the parameter "InitDelay" in the ConnectionParams
ComNumber Index of the parameter "ComNumber" in the ConnectionParams
ComParity Index of the parameter "Parity" in the ConnectionParams
ComRTSEnable Index of the parameter "RTSEnable" in the ConnectionParams
ComStopBits Index of the parameter "StopBits" in the ConnectionParams
DefaultParamValues Default values of the parameters in the ConnectionParams
DeviceName The name of a device to communicate with. Used in error messages.
(Inherited from Communicator)
DisplayBytesLength Count of bytes to display on a single line in a log. It is applicable only if DefaultByteDisplayMode is set to ByteDisplayModes.Hex.
(Inherited from Communicator)
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.
(Inherited from Communicator)
DisplaySystemChars Applies to ByteDisplayMode is Chars or Lines. Whether to display characters lower than 32. These characters are displayed as names surroundex in < and >.
(Inherited from Communicator)
DisplayTicks Whether to display time from creating a communicator for each line in communication log.
(Inherited from Communicator)
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.
(Inherited from Communicator)
CheckDataReceive Whether "Device is not responding" or "Reply is invalid or incomplete" errors will be raised.
(Inherited from Communicator)
CheckTimeout Whether "Timeout expired" errors will be raised.
(Inherited from Communicator)
IgnoreNoResponse Whether not to display "No data received" message.
(Inherited from Communicator)
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.
(Inherited from Communicator)
InputLineSeparator Line separators for input to control displaying bytes if ByteDisplayMode is equal to ByteDisplayModes.Lines
(Inherited from Communicator)
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.
(Inherited from Communicator)
NoTerminateTest Whether to check a user termination. It could be useful for some disconnect operations when user termination is not desirable.
(Inherited from Communicator)
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.
(Inherited from Communicator)
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.
(Inherited from Communicator)
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.
(Inherited from Communicator)
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.
(Inherited from Communicator)
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.
(Inherited from Communicator)
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.
(Inherited from Communicator)
OutputLineSeparator Line separators for output to control displaying bytes if ByteDisplayMode is equal to ByteDisplayModes.Lines
(Inherited from Communicator)
ReplyDelay Device reaction time in miliseconds. Used in GetDefaultTimeout to compute a communication timeout.
(Inherited from Communicator)
SerialParamNames Names of the parameters in the ConnectionParams
SleepTime Sleep time in every repeat of main waiting loop in miliseconds.
(Inherited from Communicator)
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.
(Inherited from Communicator)
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.
(Inherited from Communicator)
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).
(Inherited from Communicator)
WaitAfterSend If this parameter is true, communicator waits (getCommunicationTime(data.Count) + 50) ms after send each data.
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.
(Inherited from Communicator)

See Also