CommandMessenger.SerialPortManager Class Reference

Fas Manager for serial port data More...

Inheritance diagram for CommandMessenger.SerialPortManager:

## Public Member Functions

| SerialPortManager ()
| Default constructor. More...
| SerialPortManager (char eolSeparator, char escapeCharacter)
| Constructor. More...
void | Initialize (char eolSeparator, char escapeCharacter)
| Initializes this object. More...
bool | StartListening ()
| Connects to a serial port defined through the current settings. More...
bool | Open ()
| Opens the serial port. More...
bool | PortExists ()
| Queries if a given port exists. More...
bool | Close ()
| Closes the serial port. More...
bool | IsOpen ()
| Query ifthe serial port is open. More...
bool | StopListening ()
| Stops listening to the serial port. More...
void | WriteLine (string value)
| Writes a string to the serial port. More...
void | WriteLine< T > (T value)
| Writes a parameter to the serial port followed by a NewLine. More...
void | Write< T > (T value)
| Writes a parameter to the serial port. More...
bool | UpdateBaudRateCollection ()
| Retrieves the possible baud rates for the currently selected serial port. More...
string | ReadLine ()
| Reads a line from the string buffer. More...
void | Dispose ()
| Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. More...

## Public Attributes

readonly Encoding | StringEncoder = Encoding.GetEncoding("ISO-8859-1")

## Protected Member Functions

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

## Properties

char | EolDelimiter `[get, set]`
| Gets or sets the End-Of-Line delimiter. More...
long | LastLineTimeStamp `[get, set]`
| Gets or sets the time stamp of the last received line. More...
SerialSettings | CurrentSerialSettings `[get, set]`
| Gets or sets the current serial port settings. More...
SerialPort | SerialPort `[get]`
| Gets the serial port. More...

## Events

EventHandler | NewLineReceived

## Detailed Description

Fas Manager for serial port data

## Constructor & Destructor Documentation

CommandMessenger.SerialPortManager.SerialPortManager | ( |  | ) |

inline

Default constructor.

References CommandMessenger.SerialPortManager.Initialize().

{
Initialize(';', '/');
}
CommandMessenger.SerialPortManager.Initialize
void Initialize(char eolSeparator, char escapeCharacter)
Initializes this object.
Definition: SerialPortManager.cs:66

CommandMessenger.SerialPortManager.SerialPortManager | ( | char | eolSeparator,
|  | char | escapeCharacter
| ) |  |

inline

Constructor.

Parameters

  eolSeparator | The End-Of-Line separator.
  escapeCharacter | The escape character.

References CommandMessenger.SerialPortManager.Initialize().

{
Initialize(eolSeparator, escapeCharacter);
}
CommandMessenger.SerialPortManager.Initialize
void Initialize(char eolSeparator, char escapeCharacter)
Initializes this object.
Definition: SerialPortManager.cs:66

## Member Function Documentation

bool CommandMessenger.SerialPortManager.Close | ( |  | ) |

inline

Closes the serial port.

Returns
  true if it succeeds, false if it fails.

References CommandMessenger.SerialPortManager.PortExists(), and CommandMessenger.SerialPortManager.SerialPort.

Referenced by CommandMessenger.SerialPortManager.Dispose(), CommandMessenger.SerialPortManager.StartListening(), CommandMessenger.SerialPortManager.StopListening(), and CommandMessenger.SerialPortManager.UpdateBaudRateCollection().

{
try
{
if (SerialPort != null && PortExists())
{
_serialPort.Close();
return true;
}
}
catch
{
return false;
}
return true;
}
CommandMessenger.SerialPortManager.SerialPort
SerialPort SerialPort
Gets the serial port.
Definition: SerialPortManager.cs:110
CommandMessenger.SerialPortManager.PortExists
bool PortExists()
Queries if a given port exists.
Definition: SerialPortManager.cs:180

void CommandMessenger.SerialPortManager.Dispose | ( |  | ) |

inline

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

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

virtual void CommandMessenger.SerialPortManager.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.

References CommandMessenger.SerialPortManager.Close(), and CommandMessenger.SerialPortManager.IsOpen().

{
if (disposing)
{
_pollBuffer.StopAndWait();
// Releasing serial port (and other unmanaged objects)
if (IsOpen()) Close();
_serialPort.Dispose();
}
}
CommandMessenger.SerialPortManager.Close
bool Close()
Closes the serial port.
Definition: SerialPortManager.cs:187
CommandMessenger.SerialPortManager.IsOpen
bool IsOpen()
Query ifthe serial port is open.
Definition: SerialPortManager.cs:206

void CommandMessenger.SerialPortManager.Initialize | ( | char | eolSeparator,
|  | char | escapeCharacter
| ) |  |

inline

Initializes this object.

Parameters

  eolSeparator | The End-Of-Line separator.
  escapeCharacter | The escape character.

References CommandMessenger.SerialPortManager.EolDelimiter, and CommandMessenger.SerialSettings.PortNameCollection.

Referenced by CommandMessenger.SerialPortManager.SerialPortManager().

{
// Find installed serial ports on hardware
_currentSerialSettings.PortNameCollection = SerialPort.GetPortNames();
_currentSerialSettings.PropertyChanged += CurrentSerialSettingsPropertyChanged;
// If serial ports are found, we select the first one
if (_currentSerialSettings.PortNameCollection.Length > 0)
_currentSerialSettings.PortName = _currentSerialSettings.PortNameCollection[0];
EolDelimiter = eolSeparator;
_isEscaped = new IsEscaped();
_pollBuffer = new TimedAction(SerialBufferPollFrequency, SerialPortDataReceived);
}
CommandMessenger.SerialPortManager.EolDelimiter
char EolDelimiter
Gets or sets the End-Of-Line delimiter.
Definition: SerialPortManager.cs:89
CommandMessenger.SerialSettings.PortNameCollection
string[] PortNameCollection
Available ports on the computer
Definition: SerialSettings.cs:123

bool CommandMessenger.SerialPortManager.IsOpen | ( |  | ) |

inline

Query ifthe serial port is open.

Returns
  true if open, false if not.

References CommandMessenger.SerialPortManager.PortExists().

Referenced by CommandMessenger.SerialPortManager.Dispose(), and CommandMessenger.SerialPortManager.StartListening().

{
try
{
return _serialPort != null && PortExists() && _serialPort.IsOpen;
}
catch
{
return false;
}
}
CommandMessenger.SerialPortManager.PortExists
bool PortExists()
Queries if a given port exists.
Definition: SerialPortManager.cs:180

bool CommandMessenger.SerialPortManager.Open | ( |  | ) |

inline

Opens the serial port.

Returns
  true if it succeeds, false if it fails.

References CommandMessenger.SerialPortManager.PortExists(), and CommandMessenger.SerialPortManager.SerialPort.

Referenced by CommandMessenger.SerialPortManager.StartListening(), and CommandMessenger.SerialPortManager.UpdateBaudRateCollection().

{
try
{
if (SerialPort != null && PortExists())
{
_serialPort.Open();
return _serialPort.IsOpen;
}
}
catch
{
return false;
}
return false;
}
CommandMessenger.SerialPortManager.SerialPort
SerialPort SerialPort
Gets the serial port.
Definition: SerialPortManager.cs:110
CommandMessenger.SerialPortManager.PortExists
bool PortExists()
Queries if a given port exists.
Definition: SerialPortManager.cs:180

bool CommandMessenger.SerialPortManager.PortExists | ( |  | ) |

inline

Queries if a given port exists.

Returns
  true if it succeeds, false if it fails.

Referenced by CommandMessenger.SerialPortManager.Close(), CommandMessenger.SerialPortManager.IsOpen(), and CommandMessenger.SerialPortManager.Open().

{
return SerialPort.GetPortNames().Contains(_serialPort.PortName);
}

string CommandMessenger.SerialPortManager.ReadLine | ( |  | ) |

inline

Reads a line from the string buffer.

Returns
  The read line.

References CommandMessenger.SerialPortManager.LastLineTimeStamp, and CommandMessenger.TimeUtils.Millis.

{
// Force a last update, if update has waited to long
// This helps if a code was stopped in Serial port reading
if ((TimeUtils.Millis - LastLineTimeStamp) > 2*SerialBufferPollFrequency)
{
ParseLines();
}
lock (_lineBuffer)
{
if (_lineBuffer.Count == 0) return null;
return _lineBuffer.Dequeue();
}
}
CommandMessenger.SerialPortManager.LastLineTimeStamp
long LastLineTimeStamp
Gets or sets the time stamp of the last received line.
Definition: SerialPortManager.cs:93

bool CommandMessenger.SerialPortManager.StartListening | ( |  | ) |

inline

Connects to a serial port defined through the current settings.

Returns
  true if it succeeds, false if it fails.

References CommandMessenger.SerialSettings.BaudRate, CommandMessenger.SerialPortManager.Close(), CommandMessenger.SerialSettings.DataBits, CommandMessenger.SerialPortManager.IsOpen(), CommandMessenger.SerialPortManager.Open(), CommandMessenger.SerialSettings.Parity, CommandMessenger.SerialSettings.PortName, CommandMessenger.SerialPortManager.SerialPort, and CommandMessenger.SerialSettings.StopBits.

Referenced by CommandMessenger.CmdMessenger.StartListening().

{
// Closing serial port if it is open
if (IsOpen()) Close();
// Setting serial port settings
_serialPort = new SerialPort(
_currentSerialSettings.PortName,
_currentSerialSettings.BaudRate,
_currentSerialSettings.Parity,
_currentSerialSettings.DataBits,
_currentSerialSettings.StopBits);
// Subscribe to event and open serial port for data
_pollBuffer.Start();
return Open();
}
CommandMessenger.SerialSettings.Parity
Parity Parity
One of the Parity values.
Definition: SerialSettings.cs:78
CommandMessenger.SerialSettings.BaudRate
int BaudRate
The baud rate.
Definition: SerialSettings.cs:62
CommandMessenger.SerialSettings.StopBits
StopBits StopBits
One of the StopBits values.
Definition: SerialSettings.cs:108
CommandMessenger.SerialPortManager.SerialPort
SerialPort SerialPort
Gets the serial port.
Definition: SerialPortManager.cs:110
CommandMessenger.SerialPortManager.Close
bool Close()
Closes the serial port.
Definition: SerialPortManager.cs:187
CommandMessenger.SerialPortManager.Open
bool Open()
Opens the serial port.
Definition: SerialPortManager.cs:161
CommandMessenger.SerialSettings.DataBits
int DataBits
The data bits value.
Definition: SerialSettings.cs:93
CommandMessenger.SerialPortManager.IsOpen
bool IsOpen()
Query ifthe serial port is open.
Definition: SerialPortManager.cs:206
CommandMessenger.SerialSettings.PortName
string PortName
The port to use (for example, COM1).
Definition: SerialSettings.cs:47

bool CommandMessenger.SerialPortManager.StopListening | ( |  | ) |

inline

Stops listening to the serial port.

Returns
  true if it succeeds, false if it fails.

References CommandMessenger.SerialPortManager.Close().

{
_pollBuffer.StopAndWait();
return Close();
}
CommandMessenger.SerialPortManager.Close
bool Close()
Closes the serial port.
Definition: SerialPortManager.cs:187

bool CommandMessenger.SerialPortManager.UpdateBaudRateCollection | ( |  | ) |

inline

Retrieves the possible baud rates for the currently selected serial port.

Returns
  true if it succeeds, false if it fails.

References CommandMessenger.SerialPortManager.Close(), CommandMessenger.SerialPortManager.Open(), CommandMessenger.SerialSettings.PortName, and CommandMessenger.SerialPortManager.SerialPort.

{
try
{
Close();
_serialPort = new SerialPort(_currentSerialSettings.PortName);
if (Open())
{
var fieldInfo = _serialPort.BaseStream.GetType()
.GetField("commProp", BindingFlags.Instance | BindingFlags.NonPublic);
if (fieldInfo != null)
{
object p = fieldInfo.GetValue(_serialPort.BaseStream);
var fieldInfoValue = p.GetType()
.GetField("dwSettableBaud",
BindingFlags.Instance | BindingFlags.NonPublic |
BindingFlags.Public);
if (fieldInfoValue != null)
{
var dwSettableBaud = (Int32) fieldInfoValue.GetValue(p);
Close();
_currentSerialSettings.UpdateBaudRateCollection(dwSettableBaud);
}
}
}
}
catch
{
return false;
}
return true;
}
CommandMessenger.SerialPortManager.SerialPort
SerialPort SerialPort
Gets the serial port.
Definition: SerialPortManager.cs:110
CommandMessenger.SerialPortManager.Close
bool Close()
Closes the serial port.
Definition: SerialPortManager.cs:187
CommandMessenger.SerialPortManager.Open
bool Open()
Opens the serial port.
Definition: SerialPortManager.cs:161
CommandMessenger.SerialSettings.PortName
string PortName
The port to use (for example, COM1).
Definition: SerialSettings.cs:47

void CommandMessenger.SerialPortManager.Write< T > | ( | T | value | ) |

inline

Writes a parameter to the serial port.

Template Parameters

  T | Generic type parameter.

Parameters

  value | The value.

{
var writeString = value.ToString();
try
{
byte[] writeBytes = StringEncoder.GetBytes(writeString);
_serialPort.Write(writeBytes, 0, writeBytes.Length);
}
catch
{
}
}

void CommandMessenger.SerialPortManager.WriteLine | ( | string | value | ) |

inline

Writes a string to the serial port.

Parameters

  value | The string to write.

{
var writeString = value;
try
{
byte[] writeBytes = StringEncoder.GetBytes(writeString + '\n');
_serialPort.Write(writeBytes, 0, writeBytes.Length);
}
catch
{
}
}

void CommandMessenger.SerialPortManager.WriteLine< T > | ( | T | value | ) |

inline

Writes a parameter to the serial port followed by a NewLine.

Template Parameters

  T | Generic type parameter.

Parameters

  value | The value.

{
var writeString = value.ToString();
try
{
byte[] writeBytes = StringEncoder.GetBytes(writeString + '\n');
_serialPort.Write(writeBytes, 0, writeBytes.Length);
}
catch
{
}
}

## Property Documentation

SerialSettings CommandMessenger.SerialPortManager.CurrentSerialSettings

getset

Gets or sets the current serial port settings.

The current serial settings.

char CommandMessenger.SerialPortManager.EolDelimiter

getset

Gets or sets the End-Of-Line delimiter.

The End-Of-Line delimiter.

Referenced by CommandMessenger.SerialPortManager.Initialize().

long CommandMessenger.SerialPortManager.LastLineTimeStamp

getset

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

time stamp of the last received line.

Referenced by CommandMessenger.SerialPortManager.ReadLine().

SerialPort CommandMessenger.SerialPortManager.SerialPort

get

Gets the serial port.

The serial port.

Referenced by CommandMessenger.SerialPortManager.Close(), CommandMessenger.SerialPortManager.Open(), CommandMessenger.SerialPortManager.StartListening(), and CommandMessenger.SerialPortManager.UpdateBaudRateCollection().

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

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