CmdMessenger 3.0 CmdMessenger is a serial port messaging library for the Arduino

- Main Page
- Related Pages
- Namespaces
- Classes
- Files

- Class List
- Class Index
- Class Hierarchy
- Class Members

Public Member Functions | Public Attributes | Protected Member Functions | Properties | List of all members
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.

73 {
74 Init(communications, ',', ';', '/');
75 }

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

Constructor.

Parameters

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

81 {
82 Init(communications, fieldSeparator, ';', '/');
83 }

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.

90 {
91 Init(communications, fieldSeparator, commandSeparator, commandSeparator);
92 }

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.

101 {
102 Init(communications, fieldSeparator, commandSeparator, escapeCharacter);
103 }

## Member Function Documentation

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

Attaches default callback for unsupported commands.

Parameters

  newFunction | The callback function.

157 {
158 _defaultCallback = newFunction;
159 }

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

Attaches default callback for certain Message ID.

Parameters

  messageId | Command ID.
  newFunction | The callback function.

165 {
166 _callbackList[messageId] = newFunction;
167 }

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

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

425 {
426 Dispose(true);
427 }
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 | ) |
| inline protected virtual

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

Parameters

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

434 {
435 if (disposing)
436 {
437 _controlToInvokeOn = null;
438 _communications.NewLineReceived -= NewSerialDataReceived;
439 }
440 }

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().

232 {
233 MessengerCallbackFunction callback = null;
234 //var commandId = -1;
235 //ReceivedCommand receivedCommand;
236 if (receivedCommand.Ok)
237 {
238 //receivedCommand = new ReceivedCommand(commandString);
239 if (_callbackList.ContainsKey(receivedCommand.CommandId))
240 {
241 callback = _callbackList[receivedCommand.CommandId];
242 }
243 else
244 {
245 if (_defaultCallback != null) callback = _defaultCallback;
246 }
247 }
248 else
249 {
250 // Empty command
251 receivedCommand = new ReceivedCommand();
252 }
253 InvokeCallBack(callback, receivedCommand);
254 }
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.

183 {
184 bool continueParsing = true;
185 while (continueParsing)
186 {
187 var line = _communications.ReadLine();
188 if (line != null)
189 {
190 CurrentReceivedLine = CleanLine(line);
191 CurrentReceivedCommand = ParseMessage(CurrentReceivedLine);
192 LastLineTimeStamp = _communications.LastLineTimeStamp;
193 InvokeEvent(NewLineReceived);
194 HandleMessage(CurrentReceivedCommand);
195 }
196 else
197 continueParsing = false;
198 }
199 }
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().

262 {
263 return SendCommand(cmdId, "");
264 }
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().

271 {
272 return SendCommand(cmdId, argument, false, 0, 0);
273 }
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.

279 {
280 return SendCommand(sendCommand.CmdId, sendCommand.Arguments, sendCommand.ReqAc, sendCommand.AckCmdId,
281 sendCommand.Timeout);
282 }
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().

292 {
293 return SendCommand(cmdId, new[] {argument}, reqAc, ackCmdId, timeout);
294 }
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.

304 {
305 // Disable listening, all callbacks are disabled until after command was sent
306
307 lock (_processSerialDataLock)
308 {
309 _communications.NewLineReceived -= NewSerialDataReceived;
310
311 CurrentSentLine = cmdId.ToString(CultureInfo.InvariantCulture);
312
313 foreach (var argument in arguments)
314 {
315 CurrentSentLine += _fieldSeparator + argument;
316 }
317
318 if (PrintLfCr)
319 _communications.WriteLine(CurrentSentLine + _commandSeparator);
320 else
321 {
322 _communications.Write(CurrentSentLine + _commandSeparator);
323 }
324
325 InvokeEvent(NewLineSent);
326
327 ReceivedCommand ackCommand = reqAc ? BlockedTillReply(ackCmdId, timeout) : new ReceivedCommand();
328 _communications.NewLineReceived += NewSerialDataReceived;
329
330 return ackCommand;
331 }
332 }
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.

130 {
131 _controlToInvokeOn = controlToInvokeOn;
132 }

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().

144 {
145 if (_communications.StartListening())
146 {
147 // Timestamp of this command is same as time stamp of serial line
148 LastLineTimeStamp = _communications.LastLineTimeStamp;
149 return true;
150 }
151 return false;
152 }
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.

137 {
138 return _communications.StopListening();
139 }

## Property Documentation

ReceivedCommand CommandMessenger.CmdMessenger.CurrentReceivedCommand
| get set

Gets or sets the current received command.

The current received command.

Referenced by CommandMessenger.CmdMessenger.ProcessLines().

String CommandMessenger.CmdMessenger.CurrentReceivedLine
| get set

Gets or sets the current received command line.

The current received line.

Referenced by CommandMessenger.CmdMessenger.ProcessLines().

String CommandMessenger.CmdMessenger.CurrentSentLine
| get set

Gets or sets the currently sent line.

The currently sent line.

Referenced by CommandMessenger.CmdMessenger.SendCommand().

long CommandMessenger.CmdMessenger.LastLineTimeStamp
| get set

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
| get set

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