Base Framework
Classes | Public Types | Public Member Functions | Static Public Member Functions | List of all members
SerialPort Class Reference

Serial port. More...

#include <base/communication/SerialPort.h>

Inherits CommunicationsPort, and AsynchronousInputStream.

Classes

class  Parity
 
class  StopBits
 

Public Types

enum  Event {
  BREAK, CD, CTS, DSR,
  ERROR, RING, RLSD, INPUT_AVAILABLE,
  OUTPUT_EMPTY
}
 

Public Member Functions

 SerialPort (const String &name)
 
String getName () const noexcept
 
unsigned int getBaudRate () const
 
unsigned int getDataBits () const
 
unsigned int getFlowControlMode () const
 
unsigned int getParity () const
 
unsigned int getStopBits () const
 
void setParameters (unsigned int baudRate, unsigned int dataBits, unsigned int parity, unsigned int stopBits)
 
void setFlowControlMode (unsigned int flowMode)
 
bool isCD () const
 
bool isCTS () const
 
bool isDSR () const
 
bool isDTR () const
 
bool isRI () const
 
bool isRTS () const
 
void close ()
 
void sendBreak (unsigned int milliseconds)
 
void setDTR (bool state)
 
void setRTS (bool state)
 
bool isReadTimeoutSupported () const
 
bool isWriteTimeoutSupported () const
 
unsigned int getReadTimeout () const
 
unsigned int getWriteTimeout () const
 
void setReadTimeout (unsigned int milliseconds)
 
void setWriteTimeout (unsigned int milliseconds)
 
void setInputBufferSize (unsigned int size)
 
unsigned int getInputBufferSize () const
 
void setOutputBufferSize (unsigned int size)
 
unsigned int getOutputBufferSize () const
 
unsigned int read (uint8 *buffer, unsigned int bytesToRead, bool nonblocking)
 
unsigned int write (const uint8 *buffer, unsigned int bytesToWrite, bool nonblocking)
 
void asyncCancel ()
 
AsynchronousReadOperation read (uint8 *buffer, unsigned int bytesToRead, AsynchronousReadEventListener *listener)
 
AsynchronousWriteOperation write (const uint8 *buffer, unsigned int bytesToWrite, AsynchronousWriteEventListener *listener)
 
virtual ~SerialPort ()
 
- Public Member Functions inherited from AsynchronousStream
virtual ~AsynchronousStream () noexcept(false)
 

Static Public Member Functions

static List< StringgetPorts () noexcept
 
- Static Public Member Functions inherited from AsynchronousStream
static bool asyncTest () noexcept
 

Detailed Description

Serial port.

An RS-232 serial communications port. SerialPort describes the low-level interface to a serial communications port made available by the underlying system. SerialPort defines the minimum required functionality for serial communications ports.

See also
CommunicationPort.
Version
1.0

Member Enumeration Documentation

◆ Event

Enumerator
BREAK 

Break event.

CD 

Carrier Detect (CD) event.

CTS 

Clear To Send (CTS) event.

DSR 

Data Set Ready (DSR) event.

ERROR 

Error event.

RING 

Ring Indicator (RI) event.

RLSD 

Received Line Signal Detector (RLSD) event.

INPUT_AVAILABLE 

Input data is available event.

OUTPUT_EMPTY 

Output buffer is empty event.

Constructor & Destructor Documentation

◆ SerialPort()

SerialPort::SerialPort ( const String name)

Initializes the serial communication port.

Parameters
nameThe name of the serial port (e.g. "com1" under Windows).

◆ ~SerialPort()

virtual SerialPort::~SerialPort ( )
virtual

Destroys the serial port object.

Member Function Documentation

◆ close()

void SerialPort::close ( )

Closes the communications port.

◆ getBaudRate()

unsigned int SerialPort::getBaudRate ( ) const

Registers a SerialPortEventListener object to listen for SerialEvents. Returns the currently configured baud rate.

◆ getDataBits()

unsigned int SerialPort::getDataBits ( ) const

Returns the currently configured number of data bits.

◆ getFlowControlMode()

unsigned int SerialPort::getFlowControlMode ( ) const

Returns the currently configured flow control mode.

◆ getInputBufferSize()

unsigned int SerialPort::getInputBufferSize ( ) const

Returns the input buffer size.

◆ getName()

String SerialPort::getName ( ) const
noexcept

Returns the name of the communications port.

◆ getOutputBufferSize()

unsigned int SerialPort::getOutputBufferSize ( ) const

Returns the output buffer size.

◆ getParity()

unsigned int SerialPort::getParity ( ) const

Returns the currently configured parity setting.

◆ getPorts()

static List<String> SerialPort::getPorts ( )
staticnoexcept

Returns the available ports.

◆ getReadTimeout()

unsigned int SerialPort::getReadTimeout ( ) const

Returns the maximum read timeout period.

◆ getStopBits()

unsigned int SerialPort::getStopBits ( ) const

Returns the currently configured stop bits.

◆ getWriteTimeout()

unsigned int SerialPort::getWriteTimeout ( ) const

Returns the maximum write timeout period.

◆ isCD()

bool SerialPort::isCD ( ) const

Returns the state of the CD (Carrier Detect) bit in the UART, if supported by the underlying implementation.

◆ isCTS()

bool SerialPort::isCTS ( ) const

Returns the state of the CTS (Clear To Send) bit in the UART, if supported by the underlying implementation.

◆ isDSR()

bool SerialPort::isDSR ( ) const

Returns the state of the DSR (Data Set Ready) bit in the UART, if supported by the underlying implementation.

◆ isDTR()

bool SerialPort::isDTR ( ) const

Returns the state of the DTR (Data Terminal Ready) bit in the UART, if supported by the underlying implementation.

◆ isReadTimeoutSupported()

bool SerialPort::isReadTimeoutSupported ( ) const

Returns true if read timeout is supported.

◆ isRI()

bool SerialPort::isRI ( ) const

Returns the state of the RI (Ring Indicator) bit in the UART, if supported by the underlying implementation.

◆ isRTS()

bool SerialPort::isRTS ( ) const

Returns the state of the RTS (Request To Send) bit in the UART, if supported by the underlying implementation.

◆ isWriteTimeoutSupported()

bool SerialPort::isWriteTimeoutSupported ( ) const

Returns true if write timeout is supported.

◆ read()

AsynchronousReadOperation SerialPort::read ( uint8 *  buffer,
unsigned int  bytesToRead,
AsynchronousReadEventListener listener 
)
virtual

Requests and asynchronous read operation.

Parameters
bufferThe bytes to be read.
bytesToReadThe number of bytes to be read.
listenerThe listener to be notified on completion.

Implements AsynchronousInputStream.

◆ sendBreak()

void SerialPort::sendBreak ( unsigned int  milliseconds)

Sends a break of the specified duration.

Parameters
millisecondsThe duration of the break in milliseconds.

◆ setDTR()

void SerialPort::setDTR ( bool  state)

Sets or clears the Data Terminal Ready (DTR) signal.

◆ setFlowControlMode()

void SerialPort::setFlowControlMode ( unsigned int  flowMode)

Sets the flow control mode.

◆ setInputBufferSize()

void SerialPort::setInputBufferSize ( unsigned int  size)

Sets the input buffer size.

◆ setOutputBufferSize()

void SerialPort::setOutputBufferSize ( unsigned int  size)

Sets the output buffer size.

◆ setParameters()

void SerialPort::setParameters ( unsigned int  baudRate,
unsigned int  dataBits,
unsigned int  parity,
unsigned int  stopBits 
)

Sets the serial port parameters.

◆ setReadTimeout()

void SerialPort::setReadTimeout ( unsigned int  milliseconds)

Sets the maximum read timeout period.

◆ setRTS()

void SerialPort::setRTS ( bool  state)

Sets or clears the Request To Send (RTS) signal.

◆ setWriteTimeout()

void SerialPort::setWriteTimeout ( unsigned int  milliseconds)

Sets the maximum write timeout period.