CommandMessenger.CmdMessenger Class Reference

Command messenger main class More...

Inheritance diagram for CommandMessenger.CmdMessenger:

## Public Member Functions

delegate void | MessengerCallbackFunction (ReceivedCommand receivedCommand)
| Definition of the messenger callback function. More...
| CmdMessenger (SerialPortManager communications)
| Constructor. More...
| CmdMessenger (SerialPortManager communications, char fieldSeparator)
| Constructor. More...
| CmdMessenger (SerialPortManager communications, char fieldSeparator, char commandSeparator)
| Constructor. More...
| CmdMessenger (SerialPortManager communications, char fieldSeparator, char commandSeparator, char escapeCharacter)
| Constructor. More...
void | SetControlToInvokeOn (Control controlToInvokeOn)
| Sets a control to invoke on. More...
bool | StopListening ()
| Stop listening and end serial port connection. More...
bool | StartListening ()
| Starts serial port connection and start listening. More...
void | Attach (MessengerCallbackFunction newFunction)
| Attaches default callback for unsupported commands. More...
void | Attach (int messageId, MessengerCallbackFunction newFunction)
| Attaches default callback for certain Message ID. More...
void | ProcessLines ()
| Process the command lines and invokes callbacks. More...
void | HandleMessage (ReceivedCommand receivedCommand)
| Handle message. More...
ReceivedCommand | SendCommand (int cmdId)
| Sends a command. More...
ReceivedCommand | SendCommand (int cmdId, string argument)
| Sends a command. More...
ReceivedCommand | SendCommand (SendCommand sendCommand)
| Sends a command. More...
ReceivedCommand | SendCommand (int cmdId, string argument, bool reqAc, int ackCmdId, int timeout)
| Sends a command. More...
ReceivedCommand | SendCommand (int cmdId, string[] arguments, bool reqAc, int ackCmdId, int timeout)
| Sends a command. More...
void | Dispose ()
| Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. More...

## Public Attributes

EventHandler | NewLinesReceived
EventHandler | NewLineReceived
EventHandler | NewLineSent

## Protected Member Functions

virtual void | Dispose (bool disposing)
| Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. More...

## Properties

bool | PrintLfCr `[get, set]`
| Gets or sets a whether to print a line feed carriage return after each command. More...
String | CurrentReceivedLine `[get, set]`
| Gets or sets the current received command line. More...
ReceivedCommand | CurrentReceivedCommand `[get, set]`
| Gets or sets the current received command. More...
String | CurrentSentLine `[get, set]`
| Gets or sets the currently sent line. More...
long | LastLineTimeStamp `[get, set]`
| Gets or sets the time stamp of the last command line received. More...

## Detailed Description

Command messenger main class

## Constructor & Destructor Documentation

CommandMessenger.CmdMessenger.CmdMessenger | ( | SerialPortManager | communications | ) |

inline

Constructor.

Parameters

  communications | The Serial port object.

{
Init(communications, ',', ';', '/');
}

CommandMessenger.CmdMessenger.CmdMessenger | ( | SerialPortManager | communications,
|  | char | fieldSeparator
| ) |  |

inline

Constructor.

Parameters

  communications | The Serial port object.
  fieldSeparator | The field separator.

{
Init(communications, fieldSeparator, ';', '/');
}

CommandMessenger.CmdMessenger.CmdMessenger | ( | SerialPortManager | communications,
|  | char | fieldSeparator,
|  | char | commandSeparator
| ) |  |

inline

Constructor.

Parameters

  communications | The Serial port object.
  fieldSeparator | The field separator.
  commandSeparator | The command separator.

{
Init(communications, fieldSeparator, commandSeparator, commandSeparator);
}

CommandMessenger.CmdMessenger.CmdMessenger | ( | SerialPortManager | communications,
|  | char | fieldSeparator,
|  | char | commandSeparator,
|  | char | escapeCharacter
| ) |  |

inline

Constructor.

Parameters

  communications | The Serial port object.
  fieldSeparator | The field separator.
  commandSeparator | The command separator.
  escapeCharacter | The escape character.

{
Init(communications, fieldSeparator, commandSeparator, escapeCharacter);
}

## Member Function Documentation

void CommandMessenger.CmdMessenger.Attach | ( | MessengerCallbackFunction | newFunction | ) |

inline

Attaches default callback for unsupported commands.

Parameters

  newFunction | The callback function.

{
_defaultCallback = newFunction;
}

void CommandMessenger.CmdMessenger.Attach | ( | int | messageId,
|  | MessengerCallbackFunction | newFunction
| ) |  |

inline

Attaches default callback for certain Message ID.

Parameters

  messageId | Command ID.
  newFunction | The callback function.

{
_callbackList[messageId] = newFunction;
}

void CommandMessenger.CmdMessenger.Dispose | ( |  | ) |

inline

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

{
Dispose(true);
}
CommandMessenger.CmdMessenger.Dispose
void Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resourc...
Definition: CmdMessenger.cs:424

virtual void CommandMessenger.CmdMessenger.Dispose | ( | bool | disposing | ) |

inlineprotectedvirtual

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

Parameters

  disposing | true if resources should be disposed, false if not.

{
if (disposing)
{
_controlToInvokeOn = null;
_communications.NewLineReceived -= NewSerialDataReceived;
}
}

void CommandMessenger.CmdMessenger.HandleMessage | ( | ReceivedCommand | receivedCommand | ) |

inline

Handle message.

Parameters

  receivedCommand | The received command.

References CommandMessenger.ReceivedCommand.CommandId, and CommandMessenger.ReceivedCommand.Ok.

Referenced by CommandMessenger.CmdMessenger.ProcessLines().

{
MessengerCallbackFunction callback = null;
//var commandId = -1;
//ReceivedCommand receivedCommand;
if (receivedCommand.Ok)
{
//receivedCommand = new ReceivedCommand(commandString);
if (_callbackList.ContainsKey(receivedCommand.CommandId))
{
callback = _callbackList[receivedCommand.CommandId];
}
else
{
if (_defaultCallback != null) callback = _defaultCallback;
}
}
else
{
// Empty command
receivedCommand = new ReceivedCommand();
}
InvokeCallBack(callback, receivedCommand);
}
CommandMessenger.CmdMessenger.MessengerCallbackFunction
delegate void MessengerCallbackFunction(ReceivedCommand receivedCommand)
Definition of the messenger callback function.

delegate void CommandMessenger.CmdMessenger.MessengerCallbackFunction | ( | ReceivedCommand | receivedCommand | ) |

Definition of the messenger callback function.

Parameters

  receivedCommand | The received command.

void CommandMessenger.CmdMessenger.ProcessLines | ( |  | ) |

inline

Process the command lines and invokes callbacks.

References CommandMessenger.CmdMessenger.CurrentReceivedCommand, CommandMessenger.CmdMessenger.CurrentReceivedLine, CommandMessenger.CmdMessenger.HandleMessage(), and CommandMessenger.CmdMessenger.LastLineTimeStamp.

{
bool continueParsing = true;
while (continueParsing)
{
var line = _communications.ReadLine();
if (line != null)
{
CurrentReceivedLine = CleanLine(line);
CurrentReceivedCommand = ParseMessage(CurrentReceivedLine);
LastLineTimeStamp = _communications.LastLineTimeStamp;
InvokeEvent(NewLineReceived);
HandleMessage(CurrentReceivedCommand);
}
else
continueParsing = false;
}
}
CommandMessenger.CmdMessenger.CurrentReceivedCommand
ReceivedCommand CurrentReceivedCommand
Gets or sets the current received command.
Definition: CmdMessenger.cs:62
CommandMessenger.CmdMessenger.LastLineTimeStamp
long LastLineTimeStamp
Gets or sets the time stamp of the last command line received.
Definition: CmdMessenger.cs:203
CommandMessenger.CmdMessenger.CurrentReceivedLine
String CurrentReceivedLine
Gets or sets the current received command line.
Definition: CmdMessenger.cs:58
CommandMessenger.CmdMessenger.HandleMessage
void HandleMessage(ReceivedCommand receivedCommand)
Handle message.
Definition: CmdMessenger.cs:231

ReceivedCommand CommandMessenger.CmdMessenger.SendCommand | ( | int | cmdId | ) |

inline

Sends a command.

Parameters

  cmdId | Command ID.

Returns
  .

Referenced by CommandMessenger.CmdMessenger.SendCommand().

{
return SendCommand(cmdId, "");
}
CommandMessenger.CmdMessenger.SendCommand
ReceivedCommand SendCommand(int cmdId)
Sends a command.
Definition: CmdMessenger.cs:261

ReceivedCommand CommandMessenger.CmdMessenger.SendCommand | ( | int | cmdId,
|  | string | argument
| ) |  |

inline

Sends a command.

Parameters

  cmdId | Command ID.
  argument | The command argument.

Returns
  .

References CommandMessenger.CmdMessenger.SendCommand().

{
return SendCommand(cmdId, argument, false, 0, 0);
}
CommandMessenger.CmdMessenger.SendCommand
ReceivedCommand SendCommand(int cmdId)
Sends a command.
Definition: CmdMessenger.cs:261

ReceivedCommand CommandMessenger.CmdMessenger.SendCommand | ( | SendCommand | sendCommand | ) |

inline

Sends a command.

Parameters

  sendCommand | The command to sent.

Returns
  .

References CommandMessenger.SendCommand.AckCmdId, CommandMessenger.SendCommand.Arguments, CommandMessenger.SendCommand.CmdId, CommandMessenger.SendCommand.ReqAc, CommandMessenger.CmdMessenger.SendCommand(), and CommandMessenger.SendCommand.Timeout.

{
return SendCommand(sendCommand.CmdId, sendCommand.Arguments, sendCommand.ReqAc, sendCommand.AckCmdId,
sendCommand.Timeout);
}
CommandMessenger.CmdMessenger.SendCommand
ReceivedCommand SendCommand(int cmdId)
Sends a command.
Definition: CmdMessenger.cs:261

ReceivedCommand CommandMessenger.CmdMessenger.SendCommand | ( | int | cmdId,
|  | string | argument,
|  | bool | reqAc,
|  | int | ackCmdId,
|  | int | timeout
| ) |  |

inline

Sends a command.

Parameters

  cmdId | Command ID.
  argument | The command argument.
  reqAc | true to request acknowledge command.
  ackCmdId | acknowledgement command ID
  timeout | Timeout on acknowlegde command.

Returns
  .

References CommandMessenger.CmdMessenger.SendCommand().

{
return SendCommand(cmdId, new[] {argument}, reqAc, ackCmdId, timeout);
}
CommandMessenger.CmdMessenger.SendCommand
ReceivedCommand SendCommand(int cmdId)
Sends a command.
Definition: CmdMessenger.cs:261

ReceivedCommand CommandMessenger.CmdMessenger.SendCommand | ( | int | cmdId,
|  | string[] | arguments,
|  | bool | reqAc,
|  | int | ackCmdId,
|  | int | timeout
| ) |  |

inline

Sends a command.

Parameters

  cmdId | Command ID.
  arguments | The arguments.
  reqAc | true to request acknowledge command.
  ackCmdId | acknowledgement command ID
  timeout | Timeout on acknowlegde command.

Returns
  .

References CommandMessenger.CmdMessenger.CurrentSentLine, and CommandMessenger.CmdMessenger.PrintLfCr.

{
// Disable listening, all callbacks are disabled until after command was sent
lock (_processSerialDataLock)
{
_communications.NewLineReceived -= NewSerialDataReceived;
CurrentSentLine = cmdId.ToString(CultureInfo.InvariantCulture);
foreach (var argument in arguments)
{
CurrentSentLine += _fieldSeparator + argument;
}
if (PrintLfCr)
_communications.WriteLine(CurrentSentLine + _commandSeparator);
else
{
_communications.Write(CurrentSentLine + _commandSeparator);
}
InvokeEvent(NewLineSent);
ReceivedCommand ackCommand = reqAc ? BlockedTillReply(ackCmdId, timeout) : new ReceivedCommand();
_communications.NewLineReceived += NewSerialDataReceived;
return ackCommand;
}
}
CommandMessenger.CmdMessenger.PrintLfCr
bool PrintLfCr
Gets or sets a whether to print a line feed carriage return after each command.
Definition: CmdMessenger.cs:54
CommandMessenger.CmdMessenger.CurrentSentLine
String CurrentSentLine
Gets or sets the currently sent line.
Definition: CmdMessenger.cs:66

void CommandMessenger.CmdMessenger.SetControlToInvokeOn | ( | Control | controlToInvokeOn | ) |

inline

Sets a control to invoke on.

Parameters

  controlToInvokeOn | The control to invoke on.

{
_controlToInvokeOn = controlToInvokeOn;
}

bool CommandMessenger.CmdMessenger.StartListening | ( |  | ) |

inline

Starts serial port connection and start listening.

Returns
  true if it succeeds, false if it fails.

References CommandMessenger.CmdMessenger.LastLineTimeStamp, and CommandMessenger.SerialPortManager.StartListening().

{
if (_communications.StartListening())
{
// Timestamp of this command is same as time stamp of serial line
LastLineTimeStamp = _communications.LastLineTimeStamp;
return true;
}
return false;
}
CommandMessenger.CmdMessenger.LastLineTimeStamp
long LastLineTimeStamp
Gets or sets the time stamp of the last command line received.
Definition: CmdMessenger.cs:203
CommandMessenger.SerialPortManager.StartListening
bool StartListening()
Connects to a serial port defined through the current settings.
Definition: SerialPortManager.cs:140

bool CommandMessenger.CmdMessenger.StopListening | ( |  | ) |

inline

Stop listening and end serial port connection.

Returns
  true if it succeeds, false if it fails.

{
return _communications.StopListening();
}

## Property Documentation

ReceivedCommand CommandMessenger.CmdMessenger.CurrentReceivedCommand

getset

Gets or sets the current received command.

The current received command.

Referenced by CommandMessenger.CmdMessenger.ProcessLines().

String CommandMessenger.CmdMessenger.CurrentReceivedLine

getset

Gets or sets the current received command line.

The current received line.

Referenced by CommandMessenger.CmdMessenger.ProcessLines().

String CommandMessenger.CmdMessenger.CurrentSentLine

getset

Gets or sets the currently sent line.

The currently sent line.

Referenced by CommandMessenger.CmdMessenger.SendCommand().

long CommandMessenger.CmdMessenger.LastLineTimeStamp

getset

Gets or sets the time stamp of the last command line received.

The last line time stamp.

Referenced by CommandMessenger.CmdMessenger.ProcessLines(), and CommandMessenger.CmdMessenger.StartListening().

bool CommandMessenger.CmdMessenger.PrintLfCr

getset

Gets or sets a whether to print a line feed carriage return after each command.

true if print line feed carriage return, false if not.

Referenced by CommandMessenger.CmdMessenger.SendCommand().

The documentation for this class was generated from the following file:

- D:/My Documents/Github/Arduino-Code-and-Libraries/Libraries/CmdMessenger/CSharp/CommandMessenger/CmdMessenger.cs