# VectorNav C++ Library: vn::xplat::SerialPort Class Reference

vn::xplat::SerialPort Class Reference

Represents a cross-platform serial port. More...

`#include <serialport.h>`

Inheritance diagram for vn::xplat::SerialPort:

## Public Types

enum | StopBits { ONE_STOP_BIT, TWO_STOP_BITS }
Public Types inherited from vn::xplat::IPort
typedef void(* | DataReceivedHandler) (void *userData)
| Callback handler signature that can receive notification when new data has been received on the port. More...

## Public Member Functions

| SerialPort (const std::string &portName, uint32_t baudrate)
| Creates a new SerialPort with the provided connection parameters. More...
virtual void | open ()
| Opens the simple port.
virtual void | close ()
| Closes the simple port.
virtual bool | isOpen ()
| Indicates if the simple port is open. More...
virtual void | write (const char data[], size_t length)
| Writes out data to the simple port. More...
virtual void | read (char dataBuffer[], size_t numOfBytesToRead, size_t &numOfBytesActuallyRead)
| Allows reading data from the simple port. More...
virtual void | registerDataReceivedHandler (void *userData, DataReceivedHandler handler)
| Registers a callback method for notification when new data is received on the port. More...
virtual void | unregisterDataReceivedHandler ()
| Unregisters the registered callback method.
uint32_t | baudrate ()
| Returns the baudrate connected at. More...
std::string | port ()
| Returns the port connected to. More...
void | changeBaudrate (uint32_t br)
| Changes the connected baudrate of the port. More...
StopBits | stopBits ()
| Returns the stop bit configuration. More...
void | setStopBits (StopBits stopBits)
| Sets the stop bit configuration. More...
size_t | NumberOfReceiveDataDroppedSections ()
| Indicates if the platforms supports event notifications. More...

## Static Public Member Functions

static std::vector< std::string > | getPortNames ()
| Returns a list of the names of all the available serial ports on the system. More...
static bool | determineIfPortIsOptimized (std::string portName)
| With regard to optimizing COM ports provided by FTDI drivers, this method will check if the COM port has been optimized. More...
static void | optimizePort (std::string portName)
| This will perform optimization of FTDI USB serial ports. More...

## Detailed Description

Represents a cross-platform serial port.

When the SerialPort if first created and the connection opened, the user will normally have to poll the method read to see if any new data is available on the serial port. However, if the user code registers a handler with the method registerDataReceivedHandler, the SerialPort object will start an internal thread that monitors the serial port for new data, and when new data is available, it will alert the user code through the callback handler. Then the user can call read to retrieve the data.

## Constructor & Destructor Documentation

vn::xplat::SerialPort::SerialPort | ( | const std::string & | portName,
|  | uint32_t | baudrate
| ) |  |

Creates a new SerialPort with the provided connection parameters.

Parameters

  [in] | portName | The name of the serial port.
  [in] | baudrate | The baudrate to open the serial port at.

## Member Function Documentation

uint32_t vn::xplat::SerialPort::baudrate | ( |  | ) |

Returns the baudrate connected at.

Returns
  The connected baudrate.

void vn::xplat::SerialPort::changeBaudrate | ( | uint32_t | br | ) |

Changes the connected baudrate of the port.

Parameters

  [in] | br | The baudrate to change the port to.

static bool vn::xplat::SerialPort::determineIfPortIsOptimized | ( | std::string | portName | ) |

static

With regard to optimizing COM ports provided by FTDI drivers, this method will check if the COM port has been optimized.

Parameters

  [in] | portName | The COM port name to check.

Returns
  `true` if the COM port is optimized; otherwise `false`.

static std::vector<std::string> vn::xplat::SerialPort::getPortNames | ( |  | ) |

static

Returns a list of the names of all the available serial ports on the system.

Returns
  The list of available serial port names.

virtual bool vn::xplat::SerialPort::isOpen | ( |  | ) |

virtual

Indicates if the simple port is open.

Returns
  `true` if the serial port is open; otherwise `false`.

Implements vn::xplat::IPort.

size_t vn::xplat::SerialPort::NumberOfReceiveDataDroppedSections | ( |  | ) |

Indicates if the platforms supports event notifications.

Returns the number of dropped sections of received data.

Returns
  The number of sections of dropped data sections. Note this is not indicative of the total number of dropped bytes.

static void vn::xplat::SerialPort::optimizePort | ( | std::string | portName | ) |

static

This will perform optimization of FTDI USB serial ports.

If calling this method on Windows, the process must have administrator privileges to write settings to the registry. Otherwise an

Parameters

  [in] | portName | The FTDI USB Serial Port to optimize.

std::string vn::xplat::SerialPort::port | ( |  | ) |

Returns the port connected to.

Returns
  The port name.

virtual void vn::xplat::SerialPort::read | ( | char | dataBuffer[],
|  | size_t | numOfBytesToRead,
|  | size_t & | numOfBytesActuallyRead
| ) |  |

virtual

Allows reading data from the simple port.

Parameters

  [out] | dataBuffer | The data buffer to write the read data bytes to.
  [in] | numOfBytesToRead | The number of bytes to attempt reading from the simple port.
  [out] | numOfBytesActuallyRead | The number of bytes actually read from the simple port.

Implements vn::xplat::IPort.

virtual void vn::xplat::SerialPort::registerDataReceivedHandler | ( | void * | userData,
|  | DataReceivedHandler | handler
| ) |  |

virtual

Registers a callback method for notification when new data is received on the port.

Parameters

  [in] | userData | Pointer to user data, which will be provided to the callback method.
  [in] | handler | The callback method.

Implements vn::xplat::IPort.

void vn::xplat::SerialPort::setStopBits | ( | StopBits | stopBits | ) |

Sets the stop bit configuration.

Parameters

  [in] | stopBits | The stop bit configuration.

StopBits vn::xplat::SerialPort::stopBits | ( |  | ) |

Returns the stop bit configuration.

Returns
  The current stop bit configuration.

virtual void vn::xplat::SerialPort::write | ( | const char | data[],
|  | size_t | length
| ) |  |

virtual

Writes out data to the simple port.

Parameters

  [in] | data | The data array to write out.
  [in] | length | The length of the data array to write out.

Implements vn::xplat::IPort.

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

- include/vn/serialport.h