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 | Events | List of all members
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().

45 {
46 Initialize(';', '/');
47 }
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().

53 {
54 Initialize(eolSeparator, escapeCharacter);
55 }
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().

188 {
189 try
190 {
191 if (SerialPort != null && PortExists())
192 {
193 _serialPort.Close();
194 return true;
195 }
196 }
197 catch
198 {
199 return false;
200 }
201 return true;
202 }
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.

416 {
417 Dispose(true);
418 }
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 | ) |
| 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.

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

425 {
426 if (disposing)
427 {
428 _pollBuffer.StopAndWait();
429 // Releasing serial port (and other unmanaged objects)
430
431 if (IsOpen()) Close();
432 _serialPort.Dispose();
433 }
434 }
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().

67 {
68 // Find installed serial ports on hardware
69 _currentSerialSettings.PortNameCollection = SerialPort.GetPortNames();
70 _currentSerialSettings.PropertyChanged += CurrentSerialSettingsPropertyChanged;
71
72 // If serial ports are found, we select the first one
73 if (_currentSerialSettings.PortNameCollection.Length > 0)
74 _currentSerialSettings.PortName = _currentSerialSettings.PortNameCollection[0];
75 EolDelimiter = eolSeparator;
76 _isEscaped = new IsEscaped();
77 _pollBuffer = new TimedAction(SerialBufferPollFrequency, SerialPortDataReceived);
78 }
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().

207 {
208 try
209 {
210 return _serialPort != null && PortExists() && _serialPort.IsOpen;
211 }
212 catch
213 {
214 return false;
215 }
216 }
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().

162 {
163 try
164 {
165 if (SerialPort != null && PortExists())
166 {
167 _serialPort.Open();
168 return _serialPort.IsOpen;
169 }
170 }
171 catch
172 {
173 return false;
174 }
175 return false;
176 }
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().

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

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

Reads a line from the string buffer.

Returns
  The read line.

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

398 {
399 // Force a last update, if update has waited to long
400 // This helps if a code was stopped in Serial port reading
401 if ((TimeUtils.Millis - LastLineTimeStamp) > 2*SerialBufferPollFrequency)
402 {
403 ParseLines();
404 }
405 lock (_lineBuffer)
406 {
407 if (_lineBuffer.Count == 0) return null;
408 return _lineBuffer.Dequeue();
409 }
410 }
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().

141 {
142 // Closing serial port if it is open
143
144 if (IsOpen()) Close();
145
146 // Setting serial port settings
147 _serialPort = new SerialPort(
148 _currentSerialSettings.PortName,
149 _currentSerialSettings.BaudRate,
150 _currentSerialSettings.Parity,
151 _currentSerialSettings.DataBits,
152 _currentSerialSettings.StopBits);
153
154 // Subscribe to event and open serial port for data
155 _pollBuffer.Start();
156 return Open();
157 }
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().

221 {
222 _pollBuffer.StopAndWait();
223 return Close();
224 }
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.

276 {
277 try
278 {
279 Close();
280 _serialPort = new SerialPort(_currentSerialSettings.PortName);
281 if (Open())
282 {
283 var fieldInfo = _serialPort.BaseStream.GetType()
284 .GetField("commProp", BindingFlags.Instance | BindingFlags.NonPublic);
285 if (fieldInfo != null)
286 {
287 object p = fieldInfo.GetValue(_serialPort.BaseStream);
288 var fieldInfoValue = p.GetType()
289 .GetField("dwSettableBaud",
290 BindingFlags.Instance | BindingFlags.NonPublic |
291 BindingFlags.Public);
292 if (fieldInfoValue != null)
293 {
294 var dwSettableBaud = (Int32) fieldInfoValue.GetValue(p);
295 Close();
296 _currentSerialSettings.UpdateBaudRateCollection(dwSettableBaud);
297 }
298 }
299 }
300 }
301 catch
302 {
303 return false;
304 }
305 return true;
306 }
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.

261 {
262 var writeString = value.ToString();
263 try
264 {
265 byte[] writeBytes = StringEncoder.GetBytes(writeString);
266 _serialPort.Write(writeBytes, 0, writeBytes.Length);
267 }
268 catch
269 {
270 }
271 }

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

Writes a string to the serial port.

Parameters

  value | The string to write.

229 {
230 var writeString = value;
231 try
232 {
233 byte[] writeBytes = StringEncoder.GetBytes(writeString + '\n');
234 _serialPort.Write(writeBytes, 0, writeBytes.Length);
235 }
236 catch
237 {
238 }
239 }

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.

245 {
246 var writeString = value.ToString();
247 try
248 {
249 byte[] writeBytes = StringEncoder.GetBytes(writeString + '\n');
250 _serialPort.Write(writeBytes, 0, writeBytes.Length);
251 }
252 catch
253 {
254 }
255 }

## Property Documentation

SerialSettings CommandMessenger.SerialPortManager.CurrentSerialSettings
| get set

Gets or sets the current serial port settings.

The current serial settings.

char CommandMessenger.SerialPortManager.EolDelimiter
| get set

Gets or sets the End-Of-Line delimiter.

The End-Of-Line delimiter.

Referenced by CommandMessenger.SerialPortManager.Initialize().

long CommandMessenger.SerialPortManager.LastLineTimeStamp
| get set

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