LibSerial 1.0.0
LibSerial provides a convenient, object oriented approach to accessing serial ports on POSIX systems.
Loading...
Searching...
No Matches
LibSerial::SerialStreamBuf Class Reference

SerialStreamBuf is the streambuf subclass used by SerialStream. This subclass takes care of opening the serial port file in the required modes and providing the corresponding file descriptor to SerialStream so that various parameters associated with the serial port can be set. Several features of this streambuf class resemble those of std::filebuf, however this class it not made a subclass of filebuf because we need access to the file descriptor associated with the serial port and the standard filebuf does not provide access to it. More...

#include <SerialStreamBuf.h>

Inheritance diagram for LibSerial::SerialStreamBuf:
Collaboration diagram for LibSerial::SerialStreamBuf:

Classes

class  Implementation
 SerialStreamBuf::Implementation is the SerialStreamBuf implementation class. More...
 

Public Member Functions

 SerialStreamBuf ()
 Default Constructor.
 
 SerialStreamBuf (const std::string &fileName, const BaudRate &baudRate=BaudRate::BAUD_DEFAULT, const CharacterSize &characterSize=CharacterSize::CHAR_SIZE_DEFAULT, const FlowControl &flowControlType=FlowControl::FLOW_CONTROL_DEFAULT, const Parity &parityType=Parity::PARITY_DEFAULT, const StopBits &stopBits=StopBits::STOP_BITS_DEFAULT, bool exclusive=true)
 Constructor that allows a SerialStreamBuf instance to be created and opened, initializing the corresponding serial port with the specified parameters.
 
virtual ~SerialStreamBuf ()
 Default Destructor for a SerialStreamBuf object. Closes the serial port associated with mFileDescriptor if open.
 
 SerialStreamBuf (const SerialStreamBuf &otherSerialStreamBuf)=delete
 Copy construction is disallowed.
 
 SerialStreamBuf (const SerialStreamBuf &&otherSerialStreamBuf)=delete
 Move construction is disallowed.
 
SerialStreamBufoperator= (const SerialStreamBuf &otherSerialStreamBuf)=delete
 Copy assignment is disallowed.
 
SerialStreamBufoperator= (const SerialStreamBuf &&otherSerialStreamBuf)=delete
 Move assignment is disallowed.
 
void Open (const std::string &fileName, const std::ios_base::openmode &openMode=std::ios_base::in|std::ios_base::out, bool exclusive=true)
 Opens the serial port associated with the specified file name and the specified mode.
 
void Close ()
 Closes the serial port. All settings of the serial port will be lost and no more I/O can be performed on the serial port.
 
void DrainWriteBuffer ()
 Waits until the write buffer is drained and then returns.
 
void FlushInputBuffer ()
 Flushes the serial port input buffer.
 
void FlushOutputBuffer ()
 Flushes the serial port output buffer.
 
void FlushIOBuffers ()
 Flushes the serial port input and output buffers.
 
bool IsDataAvailable ()
 Checks if data is available at the input of the serial port.
 
bool IsOpen () const
 Determines if the serial port is open for I/O.
 
void SetDefaultSerialPortParameters ()
 Sets all serial port paramters to their default values.
 
void SetBaudRate (const BaudRate &baudRate)
 Sets the baud rate for the serial port to the specified value.
 
BaudRate GetBaudRate () const
 Gets the current baud rate for the serial port.
 
void SetCharacterSize (const CharacterSize &characterSize)
 Sets the character size for the serial port.
 
CharacterSize GetCharacterSize () const
 Gets the character size being used for serial communication.
 
void SetFlowControl (const FlowControl &flowControlType)
 Sets flow control for the serial port.
 
FlowControl GetFlowControl () const
 Gets the current flow control setting.
 
void SetParity (const Parity &parityType)
 Sets the parity type for the serial port.
 
Parity GetParity () const
 Gets the parity type for the serial port.
 
void SetStopBits (const StopBits &stopBits)
 Sets the number of stop bits to be used with the serial port.
 
StopBits GetStopBits () const
 Gets the number of stop bits currently being used by the serial.
 
void SetVMin (const short vmin)
 Sets the minimum number of characters for non-canonical reads.
 
short GetVMin () const
 Gets the VMIN value for the device, which represents the minimum number of characters for non-canonical reads.
 
void SetVTime (const short vtime)
 Sets character buffer timeout for non-canonical reads in deciseconds.
 
short GetVTime () const
 Gets the current timeout value for non-canonical reads in deciseconds.
 
void SetDTR (const bool dtrState=true)
 Sets the DTR line to the specified value.
 
bool GetDTR () const
 Gets the status of the DTR line.
 
void SetRTS (const bool rtsState=true)
 Set the RTS line to the specified value.
 
bool GetRTS () const
 Get the status of the RTS line.
 
bool GetCTS ()
 Get the status of the CTS line.
 
bool GetDSR ()
 Get the status of the DSR line.
 
int GetFileDescriptor () const
 Gets the serial port file descriptor.
 
int GetNumberOfBytesAvailable ()
 Gets the number of bytes available in the read buffer.
 

Protected Member Functions

virtual std::streambuf * setbuf (char_type *character, std::streamsize numberOfBytes) override
 Performs an operation that is defined separately for each class derived from streambuf. The default behavior is to do nothing if gptr() is non-null and gptr()!=egptr(). Also, setbuf(0, 0) usually means unbuffered I/O and setbuf(p, n) means use p[0]...p[n-1] to hold the buffered characters. In general, this method implements the subclass's notion of getting memory for the buffered characters.
 
virtual std::streamsize xsputn (const char_type *character, std::streamsize numberOfBytes) override
 Writes up to n characters from the character sequence at char s to the serial port associated with the buffer.
 
virtual std::streamsize xsgetn (char_type *character, std::streamsize numberOfBytes) override
 Reads up to n characters from the serial port and returns them through the character array located at s.
 
virtual int_type overflow (const int_type character) override
 Writes the specified character to the associated serial port.
 
virtual int_type underflow () override
 Reads and returns the next character from the associated serial port if one otherwise returns traits::eof(). This method is used for buffered I/O while uflow() is called for unbuffered I/O.
 
virtual int_type uflow () override
 Reads and returns the next character from the associated serial port if one otherwise returns traits::eof(). This method is used for unbuffered I/O while underflow() is called for unbuffered I/O.
 
virtual int_type pbackfail (const int_type character) override
 This function is called when a putback of a character fails. This must be implemented for unbuffered I/O as all streambuf subclasses are required to provide putback of at least one character.
 
virtual std::streamsize showmanyc () override
 Checks whether input is available on the port. If you call SerialStream::in_avail, this method will be called to check for available input.
 

Detailed Description

SerialStreamBuf is the streambuf subclass used by SerialStream. This subclass takes care of opening the serial port file in the required modes and providing the corresponding file descriptor to SerialStream so that various parameters associated with the serial port can be set. Several features of this streambuf class resemble those of std::filebuf, however this class it not made a subclass of filebuf because we need access to the file descriptor associated with the serial port and the standard filebuf does not provide access to it.

At present, this class uses unbuffered I/O and all calls to setbuf() will be ignored.

Definition at line 58 of file SerialStreamBuf.h.

Constructor & Destructor Documentation

◆ SerialStreamBuf() [1/4]

LibSerial::SerialStreamBuf::SerialStreamBuf ( )
explicit

Default Constructor.

Definition at line 386 of file SerialStreamBuf.cpp.

◆ SerialStreamBuf() [2/4]

LibSerial::SerialStreamBuf::SerialStreamBuf ( const std::string &  fileName,
const BaudRate &  baudRate = BaudRate::BAUD_DEFAULT,
const CharacterSize &  characterSize = CharacterSize::CHAR_SIZE_DEFAULT,
const FlowControl &  flowControlType = FlowControl::FLOW_CONTROL_DEFAULT,
const Parity &  parityType = Parity::PARITY_DEFAULT,
const StopBits &  stopBits = StopBits::STOP_BITS_DEFAULT,
bool  exclusive = true 
)
explicit

Constructor that allows a SerialStreamBuf instance to be created and opened, initializing the corresponding serial port with the specified parameters.

Parameters
fileNameThe file name of the serial stream.
baudRateThe communications baud rate.
characterSizeThe size of the character buffer for storing read/write streams.
parityTypeThe parity type for the serial stream.
stopBitsThe number of stop bits for the serial stream.
flowControlTypeThe flow control type for the serial stream.
exclusiveSet exclusive access for the serial stream

Definition at line 392 of file SerialStreamBuf.cpp.

◆ ~SerialStreamBuf()

LibSerial::SerialStreamBuf::~SerialStreamBuf ( )
virtualdefault

Default Destructor for a SerialStreamBuf object. Closes the serial port associated with mFileDescriptor if open.

◆ SerialStreamBuf() [3/4]

LibSerial::SerialStreamBuf::SerialStreamBuf ( const SerialStreamBuf otherSerialStreamBuf)
delete

Copy construction is disallowed.

◆ SerialStreamBuf() [4/4]

LibSerial::SerialStreamBuf::SerialStreamBuf ( const SerialStreamBuf &&  otherSerialStreamBuf)
delete

Move construction is disallowed.

Member Function Documentation

◆ Close()

void LibSerial::SerialStreamBuf::Close ( )

Closes the serial port. All settings of the serial port will be lost and no more I/O can be performed on the serial port.

Definition at line 423 of file SerialStreamBuf.cpp.

◆ DrainWriteBuffer()

void LibSerial::SerialStreamBuf::DrainWriteBuffer ( )

Waits until the write buffer is drained and then returns.

Definition at line 429 of file SerialStreamBuf.cpp.

◆ FlushInputBuffer()

void LibSerial::SerialStreamBuf::FlushInputBuffer ( )

Flushes the serial port input buffer.

Definition at line 435 of file SerialStreamBuf.cpp.

◆ FlushIOBuffers()

void LibSerial::SerialStreamBuf::FlushIOBuffers ( )

Flushes the serial port input and output buffers.

Definition at line 447 of file SerialStreamBuf.cpp.

◆ FlushOutputBuffer()

void LibSerial::SerialStreamBuf::FlushOutputBuffer ( )

Flushes the serial port output buffer.

Definition at line 441 of file SerialStreamBuf.cpp.

◆ GetBaudRate()

BaudRate LibSerial::SerialStreamBuf::GetBaudRate ( ) const

Gets the current baud rate for the serial port.

Returns
Returns the baud rate.

Definition at line 477 of file SerialStreamBuf.cpp.

◆ GetCharacterSize()

CharacterSize LibSerial::SerialStreamBuf::GetCharacterSize ( ) const

Gets the character size being used for serial communication.

Returns
Returns the current character size.

Definition at line 489 of file SerialStreamBuf.cpp.

◆ GetCTS()

bool LibSerial::SerialStreamBuf::GetCTS ( )

Get the status of the CTS line.

Returns
Returns true iff the status of the CTS line is high.

Definition at line 579 of file SerialStreamBuf.cpp.

◆ GetDSR()

bool LibSerial::SerialStreamBuf::GetDSR ( )

Get the status of the DSR line.

Returns
Returns true iff the status of the DSR line is high.

Definition at line 585 of file SerialStreamBuf.cpp.

◆ GetDTR()

bool LibSerial::SerialStreamBuf::GetDTR ( ) const

Gets the status of the DTR line.

Returns
Returns true iff the status of the DTR line is high.

Definition at line 561 of file SerialStreamBuf.cpp.

◆ GetFileDescriptor()

int LibSerial::SerialStreamBuf::GetFileDescriptor ( ) const

Gets the serial port file descriptor.

Returns
Returns the serial port file descriptor.

Definition at line 591 of file SerialStreamBuf.cpp.

◆ GetFlowControl()

FlowControl LibSerial::SerialStreamBuf::GetFlowControl ( ) const

Gets the current flow control setting.

Returns
Returns the flow control type of the serial port.

Definition at line 501 of file SerialStreamBuf.cpp.

◆ GetNumberOfBytesAvailable()

int LibSerial::SerialStreamBuf::GetNumberOfBytesAvailable ( )

Gets the number of bytes available in the read buffer.

Returns
Returns the number of bytes avilable in the read buffer.

Definition at line 597 of file SerialStreamBuf.cpp.

◆ GetParity()

Parity LibSerial::SerialStreamBuf::GetParity ( ) const

Gets the parity type for the serial port.

Returns
Returns the parity type.

Definition at line 513 of file SerialStreamBuf.cpp.

◆ GetRTS()

bool LibSerial::SerialStreamBuf::GetRTS ( ) const

Get the status of the RTS line.

Returns
Returns true iff the status of the RTS line is high.

Definition at line 573 of file SerialStreamBuf.cpp.

◆ GetStopBits()

StopBits LibSerial::SerialStreamBuf::GetStopBits ( ) const

Gets the number of stop bits currently being used by the serial.

Returns
Returns the number of stop bits.

Definition at line 525 of file SerialStreamBuf.cpp.

◆ GetVMin()

short LibSerial::SerialStreamBuf::GetVMin ( ) const

Gets the VMIN value for the device, which represents the minimum number of characters for non-canonical reads.

Returns
Returns the minimum number of characters for non-canonical reads.

Definition at line 537 of file SerialStreamBuf.cpp.

◆ GetVTime()

short LibSerial::SerialStreamBuf::GetVTime ( ) const

Gets the current timeout value for non-canonical reads in deciseconds.

Returns
Returns the character buffer timeout for non-canonical reads in deciseconds.

Definition at line 549 of file SerialStreamBuf.cpp.

◆ IsDataAvailable()

bool LibSerial::SerialStreamBuf::IsDataAvailable ( )

Checks if data is available at the input of the serial port.

Returns
Returns true iff data is available to read.

Definition at line 453 of file SerialStreamBuf.cpp.

◆ IsOpen()

bool LibSerial::SerialStreamBuf::IsOpen ( ) const

Determines if the serial port is open for I/O.

Returns
Returns true iff the serial port is open.

Definition at line 459 of file SerialStreamBuf.cpp.

◆ Open()

void LibSerial::SerialStreamBuf::Open ( const std::string &  fileName,
const std::ios_base::openmode &  openMode = std::ios_base::in | std::ios_base::out,
bool  exclusive = true 
)

Opens the serial port associated with the specified file name and the specified mode.

Parameters
fileNameThe file name of the serial port.
openModeThe communication mode status when the serial communication port is opened.
exclusiveSet exclusive access for this process to the serial port.

Definition at line 413 of file SerialStreamBuf.cpp.

◆ operator=() [1/2]

SerialStreamBuf & LibSerial::SerialStreamBuf::operator= ( const SerialStreamBuf &&  otherSerialStreamBuf)
delete

Move assignment is disallowed.

◆ operator=() [2/2]

SerialStreamBuf & LibSerial::SerialStreamBuf::operator= ( const SerialStreamBuf otherSerialStreamBuf)
delete

Copy assignment is disallowed.

◆ overflow()

std::streambuf::int_type LibSerial::SerialStreamBuf::overflow ( const int_type  character)
overrideprotectedvirtual

Writes the specified character to the associated serial port.

Parameters
characterThe character to be written to the serial port.
Returns
Returns the character.

Definition at line 629 of file SerialStreamBuf.cpp.

◆ pbackfail()

std::streambuf::int_type LibSerial::SerialStreamBuf::pbackfail ( const int_type  character)
overrideprotectedvirtual

This function is called when a putback of a character fails. This must be implemented for unbuffered I/O as all streambuf subclasses are required to provide putback of at least one character.

Parameters
characterThe character to putback.
Returns
Returns The character iff successful, otherwise eof to signal an error.

Definition at line 647 of file SerialStreamBuf.cpp.

◆ SetBaudRate()

void LibSerial::SerialStreamBuf::SetBaudRate ( const BaudRate &  baudRate)

Sets the baud rate for the serial port to the specified value.

Parameters
baudRateThe baud rate to be set for the serial port.

Definition at line 471 of file SerialStreamBuf.cpp.

◆ setbuf()

std::streambuf * LibSerial::SerialStreamBuf::setbuf ( char_type *  character,
std::streamsize  numberOfBytes 
)
overrideprotectedvirtual

Performs an operation that is defined separately for each class derived from streambuf. The default behavior is to do nothing if gptr() is non-null and gptr()!=egptr(). Also, setbuf(0, 0) usually means unbuffered I/O and setbuf(p, n) means use p[0]...p[n-1] to hold the buffered characters. In general, this method implements the subclass's notion of getting memory for the buffered characters.

In the case of SerialStreamBuffer, we want to keep using unbuffered I/O. Hence, using this method has no effect at present.

Parameters
characterPointer to the character buffer to write to the serial port.
numberOfBytesThe number of characters to write to the serial port.
Returns
Returns a pointer to this streambuf object.

Definition at line 611 of file SerialStreamBuf.cpp.

◆ SetCharacterSize()

void LibSerial::SerialStreamBuf::SetCharacterSize ( const CharacterSize &  characterSize)

Sets the character size for the serial port.

Parameters
characterSizeThe character size to be set.

Definition at line 483 of file SerialStreamBuf.cpp.

◆ SetDefaultSerialPortParameters()

void LibSerial::SerialStreamBuf::SetDefaultSerialPortParameters ( )

Sets all serial port paramters to their default values.

Definition at line 465 of file SerialStreamBuf.cpp.

◆ SetDTR()

void LibSerial::SerialStreamBuf::SetDTR ( const bool  dtrState = true)

Sets the DTR line to the specified value.

Parameters
dtrStateThe line voltage state to be set, (true = high, false = low).

Definition at line 555 of file SerialStreamBuf.cpp.

◆ SetFlowControl()

void LibSerial::SerialStreamBuf::SetFlowControl ( const FlowControl &  flowControlType)

Sets flow control for the serial port.

Parameters
flowControlTypeThe flow control type to be set.

Definition at line 495 of file SerialStreamBuf.cpp.

◆ SetParity()

void LibSerial::SerialStreamBuf::SetParity ( const Parity &  parityType)

Sets the parity type for the serial port.

Parameters
parityTypeThe parity type to be set.

Definition at line 507 of file SerialStreamBuf.cpp.

◆ SetRTS()

void LibSerial::SerialStreamBuf::SetRTS ( const bool  rtsState = true)

Set the RTS line to the specified value.

Parameters
rtsStateThe line voltage state to be set, (true = high, false = low).

Definition at line 567 of file SerialStreamBuf.cpp.

◆ SetStopBits()

void LibSerial::SerialStreamBuf::SetStopBits ( const StopBits &  stopBits)

Sets the number of stop bits to be used with the serial port.

Parameters
stopBitsThe number of stop bits to set.

Definition at line 519 of file SerialStreamBuf.cpp.

◆ SetVMin()

void LibSerial::SerialStreamBuf::SetVMin ( const short  vmin)

Sets the minimum number of characters for non-canonical reads.

Note
See VMIN in man termios(3).
Parameters
vminthe number of minimum characters to be set.

Definition at line 531 of file SerialStreamBuf.cpp.

◆ SetVTime()

void LibSerial::SerialStreamBuf::SetVTime ( const short  vtime)

Sets character buffer timeout for non-canonical reads in deciseconds.

Parameters
vtimeThe timeout value in deciseconds to be set.
Returns
Returns the character buffer timeout for non-canonical reads in deciseconds.

Definition at line 543 of file SerialStreamBuf.cpp.

◆ showmanyc()

std::streamsize LibSerial::SerialStreamBuf::showmanyc ( )
overrideprotectedvirtual

Checks whether input is available on the port. If you call SerialStream::in_avail, this method will be called to check for available input.

while(serial_port.rdbuf()->in_avail() > 0)
{
serial_port.get(ch) ;
...
}
Returns
Returns 1 if characters are available at the serial port, 0 if no characters are available, and -1 if unsuccessful.

Definition at line 653 of file SerialStreamBuf.cpp.

◆ uflow()

std::streambuf::int_type LibSerial::SerialStreamBuf::uflow ( )
overrideprotectedvirtual

Reads and returns the next character from the associated serial port if one otherwise returns traits::eof(). This method is used for unbuffered I/O while underflow() is called for unbuffered I/O.

Returns
Returns the next character from the serial port.

Definition at line 641 of file SerialStreamBuf.cpp.

◆ underflow()

std::streambuf::int_type LibSerial::SerialStreamBuf::underflow ( )
overrideprotectedvirtual

Reads and returns the next character from the associated serial port if one otherwise returns traits::eof(). This method is used for buffered I/O while uflow() is called for unbuffered I/O.

Returns
Returns the next character from the serial port.

Definition at line 635 of file SerialStreamBuf.cpp.

◆ xsgetn()

std::streamsize LibSerial::SerialStreamBuf::xsgetn ( char_type *  character,
std::streamsize  numberOfBytes 
)
overrideprotectedvirtual

Reads up to n characters from the serial port and returns them through the character array located at s.

Parameters
characterPointer to the character buffer to write to the serial port.
numberOfBytesThe number of characters to write to the serial port.
Returns
Returns the number of characters actually read from the serial port.

Definition at line 623 of file SerialStreamBuf.cpp.

◆ xsputn()

std::streamsize LibSerial::SerialStreamBuf::xsputn ( const char_type *  character,
std::streamsize  numberOfBytes 
)
overrideprotectedvirtual

Writes up to n characters from the character sequence at char s to the serial port associated with the buffer.

Parameters
characterPointer to the character buffer to write to the serial port.
numberOfBytesThe number of characters to write to the serial port.
Returns
Returns the number of characters that were successfully written to the serial port.

Definition at line 617 of file SerialStreamBuf.cpp.


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