DUNE: Uniform Navigational Environment  2.3.0
DUNE::Hardware::SerialPort Class Reference

Detailed Description

The SerialPort class encapsulates serial port access.

Classes

class  Error
 

Public Types

enum  Parity { SP_PARITY_NONE, SP_PARITY_EVEN, SP_PARITY_ODD }
 
enum  StopBits { SP_STOPBITS_1, SP_STOPBITS_1_5, SP_STOPBITS_2 }
 
enum  DataBits { SP_DATABITS_5, SP_DATABITS_6, SP_DATABITS_7, SP_DATABITS_8 }
 

Public Member Functions

 SerialPort (const std::string &device, int baudrate=38400, Parity parity=SP_PARITY_NONE, StopBits stopbits=SP_STOPBITS_1, DataBits databits=SP_DATABITS_8, bool block=false)
 
 ~SerialPort (void)
 
void setMinimumRead (int value)
 
int write (const char *bfr, int size)
 
int write (const uint8_t *bfr, int size)
 
int write (const char *bfr)
 
int read (char *bfr, int size)
 
int readString (char *bfr, int size)
 
int read (uint8_t *bfr, int size)
 
void flushInput (void)
 
void flushOutput (void)
 
void flush (void)
 
void setBaudRate (int baudrate)
 
void setParity (Parity parity)
 
void setDataBits (DataBits databits)
 
void setStopBits (StopBits sbits)
 
void setCanonicalInput (bool enable)
 
void setCanonicalInputTerminator (char terminator)
 
void setNonBlocking (void)
 
void sendBreak (int duration)
 
void setXONXOFF (bool enabled)
 
void setCTSRTS (bool enabled)
 
System::IOMultiplexing::Result hasNewData (double timeout=-1.0)
 
void addToPoll (System::IOMultiplexing &poller)
 
void delFromPoll (System::IOMultiplexing &p)
 
bool wasTriggered (System::IOMultiplexing &p)
 

Static Public Member Functions

static std::vector< std::string > enumerate (void)
 

Member Enumeration Documentation

Data bits.

Enumerator
SP_DATABITS_5 

Five data bits.

SP_DATABITS_6 

Six data bits.

SP_DATABITS_7 

Seven data bits.

SP_DATABITS_8 

Eight data bits.

The optional parity bit is a simple sum of the data bits indicating whether or not the data contains an even or odd number of 1 bits.

With even parity, the parity bit is 0 if there is an even number of 1's in the character. With odd parity, the parity bit is 0 if there is an odd number of 1's in the data. No parity means that no parity bit is present or transmitted.

Enumerator
SP_PARITY_NONE 

No parity.

SP_PARITY_EVEN 

Even parity.

SP_PARITY_ODD 

Odd parity.

There can be 1, 1.5, or 2 stop bits between characters and they always have a value of 1.

Stop bits traditionally were used to give the computer time to process the previous character, but now only serve to synchronize the receiving computer to the incoming characters.

Enumerator
SP_STOPBITS_1 

One stop bit.

SP_STOPBITS_1_5 

One and half stop bits.

SP_STOPBITS_2 

Two stop bits.

Constructor & Destructor Documentation

DUNE::Hardware::SerialPort::SerialPort ( const std::string &  device,
int  baudrate = 38400,
Parity  parity = SP_PARITY_NONE,
StopBits  stopbits = SP_STOPBITS_1,
DataBits  databits = SP_DATABITS_8,
bool  block = false 
)

Serial port constructor.

Parameters
deviceserial port device.
baudrateI/O baud rate.
parityparity checking type.
stopbitsnumber of stop bits.
databitsnumber of data bits.
blockfalse enable non-blocking I/O.
Exceptions
SerialPortError.

References DUNE::System::Error::getLastMessage(), setBaudRate(), setCanonicalInput(), setCTSRTS(), setDataBits(), setMinimumRead(), setNonBlocking(), setParity(), setStopBits(), and setXONXOFF().

DUNE::Hardware::SerialPort::~SerialPort ( void  )

Serial port destructor.

References flush().

Member Function Documentation

void DUNE::Hardware::SerialPort::addToPoll ( System::IOMultiplexing poller)
void DUNE::Hardware::SerialPort::delFromPoll ( System::IOMultiplexing p)
std::vector< std::string > DUNE::Hardware::SerialPort::enumerate ( void  )
static

Enumerate available serial ports.

The names returned are dependent on the operating system.

Returns
vector of serial port names (devices).

References DUNE::Utils::String::format(), and DUNE::FileSystem::Path.

void DUNE::Hardware::SerialPort::flush ( void  )

Flush both input and output buffers.

Referenced by ~SerialPort().

void DUNE::Hardware::SerialPort::flushInput ( void  )
void DUNE::Hardware::SerialPort::flushOutput ( void  )

Flush output buffer, aborting output.

System::IOMultiplexing::Result DUNE::Hardware::SerialPort::hasNewData ( double  timeout = -1.0)
int DUNE::Hardware::SerialPort::read ( char *  bfr,
int  size 
)

Read an amount of bytes from the serial port.

Parameters
bfrdestination buffer.
sizeamount of bytes to read.
Returns
amount of bytes actually read.

Referenced by Sensors::SW100::Driver::read(), DUNE::Hardware::LUCL::Protocol::read(), read(), and readString().

int DUNE::Hardware::SerialPort::read ( uint8_t *  bfr,
int  size 
)

Read an amount of bytes from the serial port.

Parameters
bfrdestination buffer.
sizeamount of bytes to read.
Returns
amount of bytes actually read.

References read().

int DUNE::Hardware::SerialPort::readString ( char *  bfr,
int  size 
)

Read an amount of bytes from the serial port and define last character as null.

Parameters
bfrdestination buffer.
sizeamount of bytes to read.
Returns
amount of bytes actually read.

References read().

void DUNE::Hardware::SerialPort::sendBreak ( int  duration)
void DUNE::Hardware::SerialPort::setBaudRate ( int  baudrate)

Set the baud rate of the serial port.

Parameters
baudratebaud rate value.

References DUNE::System::Error::getLastMessage().

Referenced by SerialPort().

void DUNE::Hardware::SerialPort::setCanonicalInput ( bool  enable)

Enable canonical input.

Canonical input is line-oriented. Input characters are put into a buffer which can be edited interactively by the user until a CR (carriage return) or LF (line feed) character is received.

Parameters
enableenable / disable canonical input.

References DUNE::System::Error::getLastMessage().

Referenced by SerialPort().

void DUNE::Hardware::SerialPort::setCanonicalInputTerminator ( char  terminator)
void DUNE::Hardware::SerialPort::setCTSRTS ( bool  enabled)

Enable hardware flow control (CTSRTS).

Parameters
enabledenable / disable hardware flow control

References DUNE::System::Error::getLastMessage().

Referenced by SerialPort().

void DUNE::Hardware::SerialPort::setDataBits ( DataBits  databits)

Set the number of data bits of the serial port.

Parameters
databitsnumber of data bits.

References DUNE::System::Error::getLastMessage(), SP_DATABITS_5, SP_DATABITS_6, SP_DATABITS_7, and SP_DATABITS_8.

Referenced by SerialPort().

void DUNE::Hardware::SerialPort::setMinimumRead ( int  value)
void DUNE::Hardware::SerialPort::setNonBlocking ( void  )
void DUNE::Hardware::SerialPort::setParity ( Parity  parity)

Set the parity of the serial port.

Parameters
parityparity value.

References DUNE::System::Error::getLastMessage(), SP_PARITY_EVEN, SP_PARITY_NONE, and SP_PARITY_ODD.

Referenced by SerialPort().

void DUNE::Hardware::SerialPort::setStopBits ( StopBits  sbits)

Set the number of stop bits of the serial port.

Parameters
sbitsnumber of stop bits.

References DUNE::System::Error::getLastMessage(), SP_STOPBITS_1, SP_STOPBITS_1_5, and SP_STOPBITS_2.

Referenced by SerialPort().

void DUNE::Hardware::SerialPort::setXONXOFF ( bool  enabled)

Enable software flow control (XON/XOFF/XANY).

Parameters
enabledenable / disable software flow control

References DUNE::System::Error::getLastMessage().

Referenced by SerialPort().

bool DUNE::Hardware::SerialPort::wasTriggered ( System::IOMultiplexing p)
int DUNE::Hardware::SerialPort::write ( const char *  bfr,
int  size 
)

Write an amount of bytes to the serial port.

Parameters
bfrbuffer of bytes.
sizeamount of bytes to write.
Returns
amount of bytes actually written.

Referenced by DUNE::Hardware::BasicModem::sendRaw(), DUNE::Hardware::HayesModem::sendRaw(), DUNE::Hardware::LUCL::Protocol::write(), and write().

int DUNE::Hardware::SerialPort::write ( const uint8_t *  bfr,
int  size 
)

Write an amount of bytes to the serial port.

Parameters
bfrbuffer of bytes.
sizeamount of bytes to write.
Returns
amount of bytes actually written.

References write().

int DUNE::Hardware::SerialPort::write ( const char *  bfr)

Write a null terminated string to the serial port.

Parameters
bfrC string.
Returns
amount of bytes written.

References write().

Collaboration diagram for DUNE::Hardware::SerialPort:
Collaboration graph