Loading...
Searching...
No Matches
SerialStream.cpp
/******************************************************************************
* @file SerialStream.cpp *
* @copyright (C) 2004-2018 LibSerial Development Team. All rights reserved. *
* crayzeewulf@gmail.com *
* *
* Redistribution and use in source and binary forms, with or without *
* modification, are permitted provided that the following conditions *
* are met: *
* *
* 1. Redistributions of source code must retain the above copyright *
* notice, this list of conditions and the following disclaimer. *
* 2. Redistributions in binary form must reproduce the above copyright *
* notice, this list of conditions and the following disclaimer in *
* the documentation and/or other materials provided with the *
* distribution. *
* 3. Neither the name PX4 nor the names of its contributors may be *
* used to endorse or promote products derived from this software *
* without specific prior written permission. *
* *
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT *
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS *
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE *
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, *
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, *
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS *
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED *
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT *
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN *
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE *
* POSSIBILITY OF SUCH DAMAGE. *
*****************************************************************************/
#include "libserial/SerialStream.h"
#include <cassert>
namespace LibSerial
{
SerialStream::SerialStream() : std::iostream(nullptr)
{
this->flush() ;
}
SerialStream::SerialStream(const std::string& fileName,
const BaudRate& baudRate,
const CharacterSize& characterSize,
const FlowControl& flowControlType,
const Parity& parityType,
const StopBits& stopBits) :
std::iostream(nullptr)
{
this->Open(fileName) ; // NOLINT (fuchsia-default-arguments)
this->SetBaudRate(baudRate) ;
this->SetCharacterSize(characterSize) ;
this->SetFlowControl(flowControlType) ;
this->SetParity(parityType) ;
this->SetStopBits(stopBits) ;
this->FlushIOBuffers() ;
}
SerialStream::~SerialStream()
try
{
// Close the serial stream if it is open.
if (this->IsOpen())
{
this->FlushIOBuffers() ;
this->Close() ;
}
}
catch(...)
{
//
// :IMPORTANT: We do not let any exceptions escape the destructor.
// (see https://isocpp.org/wiki/faq/exceptions#dtors-shouldnt-throw)
//
// :TODO: Once we add logging to LibSerial, we should issue a warning
// if we reach here.
//
}
void
SerialStream::Open(const std::string& fileName,
const std::ios_base::openmode& openMode)
try
{
// Create a new SerialStreamBuf if one does not exist.
if (mIOBuffer == nullptr)
{
mIOBuffer = std::make_unique<SerialStreamBuf>() ;
assert(mIOBuffer != nullptr) ; // NOLINT (cppcoreguidelines-pro-bounds-array-to-pointer-decay)
this->rdbuf(mIOBuffer.get()) ;
}
// Open the serial port.
mIOBuffer->Open(fileName, openMode) ;
}
catch (const std::exception&)
{
setstate(std::ios_base::failbit) ;
throw ;
}
void
SerialStream::Close()
{
// If a SerialStreamBuf is associated with this SerialStream
// we need to destroy it.
mIOBuffer = nullptr ;
}
void
SerialStream::DrainWriteBuffer()
try
{
auto my_buffer = dynamic_cast<SerialStreamBuf *>(this->rdbuf()) ;
// Make sure that we are dealing with a SerialStreamBuf before
// proceeding. This check also makes sure that we have a non-NULL
// buffer associated with this stream.
if (my_buffer != nullptr)
{
// Try to flush the input buffers the serial port with the correspoding
// function of the SerialStreamBuf class.
my_buffer->DrainWriteBuffer() ;
}
else
{
// If the dynamic_cast above failed then we either have a NULL
// streambuf associated with this stream or we have a buffer
// of class other than SerialStreamBuf. In either case, we
// have a problem and we should stop all I/O using this stream.
setstate(badbit) ;
}
}
catch (const std::exception&)
{
setstate(std::ios_base::failbit) ;
throw ;
}
void
SerialStream::FlushInputBuffer()
try
{
auto my_buffer = dynamic_cast<SerialStreamBuf *>(this->rdbuf()) ;
// Make sure that we are dealing with a SerialStreamBuf before
// proceeding. This check also makes sure that we have a non-NULL
// buffer associated with this stream.
if (my_buffer != nullptr)
{
// Try to flush the input buffers the serial port with the correspoding
// function of the SerialStreamBuf class.
my_buffer->FlushInputBuffer() ;
}
else
{
// If the dynamic_cast above failed then we either have a NULL
// streambuf associated with this stream or we have a buffer
// of class other than SerialStreamBuf. In either case, we
// have a problem and we should stop all I/O using this stream.
setstate(badbit) ;
}
}
catch (const std::exception&)
{
setstate(std::ios_base::failbit) ;
throw ;
}
void
SerialStream::FlushOutputBuffer()
try
{
auto my_buffer = dynamic_cast<SerialStreamBuf *>(this->rdbuf()) ;
// Make sure that we are dealing with a SerialStreamBuf before
// proceeding. This check also makes sure that we have a non-NULL
// buffer associated with this stream.
if (my_buffer != nullptr)
{
// Try to flush the output buffers the serial port with the correspoding
// function of the SerialStreamBuf class.
my_buffer->FlushOutputBuffer() ;
}
else
{
// If the dynamic_cast above failed then we either have a NULL
// streambuf associated with this stream or we have a buffer
// of class other than SerialStreamBuf. In either case, we
// have a problem and we should stop all I/O using this stream.
setstate(badbit) ;
}
}
catch (const std::exception&)
{
setstate(std::ios_base::failbit) ;
throw ;
}
void
SerialStream::FlushIOBuffers()
try
{
auto my_buffer = dynamic_cast<SerialStreamBuf *>(this->rdbuf()) ;
// Make sure that we are dealing with a SerialStreamBuf before
// proceeding. This check also makes sure that we have a non-NULL
// buffer associated with this stream.
if (my_buffer != nullptr)
{
// Try to flush the I/O buffers the serial port with the correspoding
// function of the SerialStreamBuf class.
my_buffer->FlushIOBuffers() ;
}
else
{
// If the dynamic_cast above failed then we either have a NULL
// streambuf associated with this stream or we have a buffer
// of class other than SerialStreamBuf. In either case, we
// have a problem and we should stop all I/O using this stream.
setstate(badbit) ;
}
}
catch (const std::exception&)
{
setstate(std::ios_base::failbit) ;
throw ;
}
bool
SerialStream::IsDataAvailable()
try
{
auto my_buffer = dynamic_cast<SerialStreamBuf *>(this->rdbuf()) ;
// Make sure that we are dealing with a SerialStreamBuf before
// proceeding. This check also makes sure that we have a non-NULL
// buffer associated with this stream.
if (my_buffer != nullptr)
{
// Try to determine if data is available with the correspoding
// function of the SerialStreamBuf class.
return my_buffer->IsDataAvailable() ;
}
// If the dynamic_cast above failed then we either have a NULL
// streambuf associated with this stream or we have a buffer
// of class other than SerialStreamBuf. In either case, we
// have a problem and we should stop all I/O using this stream.
setstate(badbit) ;
return false ;
}
catch (const std::exception&)
{
setstate(std::ios_base::failbit) ;
throw ;
}
bool
SerialStream::IsOpen()
try
{
// Checks to see if mIOBuffer is a null buffer, if not, calls
// the IsOpen() function on this stream's SerialStreamBuf mIOBuffer
if (mIOBuffer == nullptr)
{
return false;
}
return mIOBuffer->IsOpen() ;
}
catch (const std::exception&)
{
setstate(std::ios_base::failbit) ;
throw ;
}
void
SerialStream::SetBaudRate(const BaudRate& baudRate)
try
{
auto my_buffer = dynamic_cast<SerialStreamBuf *>(this->rdbuf()) ;
// Make sure that we are dealing with a SerialStreamBuf before
// proceeding. This check also makes sure that we have a non-NULL
// buffer associated with this stream.
if (my_buffer != nullptr)
{
// Try to set the baud rate with the corresponding function of
// the SerialStreamBuf class.
my_buffer->SetBaudRate(baudRate) ;
}
else
{
// If the dynamic_cast above failed then we either have a NULL
// streambuf associated with this stream or we have a buffer
// of class other than SerialStreamBuf. In either case, we
// have a problem and we should stop all I/O using this stream.
setstate(badbit) ;
}
}
catch (const std::exception&)
{
setstate(std::ios_base::failbit) ;
throw ;
}
BaudRate
SerialStream::GetBaudRate()
try
{
auto my_buffer = dynamic_cast<SerialStreamBuf *>(this->rdbuf()) ;
// Make sure that we are dealing with a SerialStreamBuf before
// proceeding. This check also makes sure that we have a non-NULL
// buffer associated with this stream.
if (my_buffer != nullptr)
{
// Try to get the baud rate. If the corresponding function of the
// SerialStreamBuf class returns BAUD_INVALID, then we have a
// problem and the stream is no longer valid for I/O.
return my_buffer->GetBaudRate() ;
}
// If the dynamic_cast above failed then we either have a NULL
// streambuf associated with this stream or we have a buffer of
// class other than SerialStreamBuf. In either case, we have a
// problem and we should stop all I/O using this stream.
setstate(badbit) ;
return BaudRate::BAUD_INVALID;
}
catch (const std::exception&)
{
setstate(std::ios_base::failbit) ;
throw ;
}
void
SerialStream::SetCharacterSize(const CharacterSize& characterSize)
try
{
auto my_buffer = dynamic_cast<SerialStreamBuf *>(this->rdbuf()) ;
// Make sure that we are dealing with a SerialStreamBuf before
// proceeding. This check also makes sure that we have a non-NULL
// buffer associated with this stream.
if (my_buffer != nullptr)
{
// Try to set the character size with the corresponding function of
// the SerialStreamBuf class.
my_buffer->SetCharacterSize(characterSize) ;
}
else
{
// If the dynamic_cast above failed then we either have a NULL
// streambuf associated with this stream or we have a buffer of
// class other than SerialStreamBuf. In either case, we have a
// problem and we should stop all I/O using this stream.
setstate(badbit) ;
}
}
catch (const std::exception&)
{
setstate(std::ios_base::failbit) ;
throw ;
}
CharacterSize
SerialStream::GetCharacterSize()
try
{
auto my_buffer = dynamic_cast<SerialStreamBuf *>(this->rdbuf()) ;
// Make sure that we are dealing with a SerialStreamBuf before
// proceeding. This check also makes sure that we have a non-NULL
// buffer associated with this stream.
if (my_buffer != nullptr)
{
// Try to get the character size. If the corresponding function of the
// SerialStreamBuf class returns CHAR_SIZE_INVALID, then we have a
// problem and the stream is no longer valid for I/O.
return my_buffer->GetCharacterSize() ;
}
// If the dynamic_cast above failed then we either have a NULL
// streambuf associated with this stream or we have a buffer of
// class other than SerialStreamBuf. In either case, we have a
// problem and we should stop all I/O using this stream.
setstate(badbit) ;
return CharacterSize::CHAR_SIZE_INVALID;
}
catch (const std::exception&)
{
setstate(std::ios_base::failbit) ;
throw ;
}
void
SerialStream::SetFlowControl(const FlowControl& flowControlType)
try
{
auto my_buffer = dynamic_cast<SerialStreamBuf *>(this->rdbuf()) ;
// Make sure that we are dealing with a SerialStreamBuf before
// proceeding. This check also makes sure that we have a non-NULL
// buffer associated with this stream.
if (my_buffer != nullptr)
{
// Try to set the flow control. If the corresponding function of the
// SerialStreamBuf class returns FLOW_CONTROL_INVALID, then we have a
// problem and the stream is no longer valid for I/O.
my_buffer->SetFlowControl(flowControlType) ;
}
else
{
// If the dynamic_cast above failed then we either have a NULL
// streambuf associated with this stream or we have a buffer of
// class other than SerialStreamBuf. In either case, we have a
// problem and we should stop all I/O using this stream.
setstate(badbit) ;
}
}
catch (const std::exception&)
{
setstate(std::ios_base::failbit) ;
throw ;
}
FlowControl
SerialStream::GetFlowControl()
try
{
auto my_buffer = dynamic_cast<SerialStreamBuf *>(this->rdbuf()) ;
// Make sure that we are dealing with a SerialStreamBuf before
// proceeding. This check also makes sure that we have a non-NULL
// buffer associated with this stream.
if (my_buffer != nullptr)
{
// Try to get the flow control. If the corresponding function of the
// SerialStreamBuf class returns FLOW_CONTROL_INVALID, then we have a
// problem and the stream is no longer valid for I/O.
return my_buffer->GetFlowControl() ;
}
// If the dynamic_cast above failed then we either have a NULL
// streambuf associated with this stream or we have a buffer of
// class other than SerialStreamBuf. In either case, we have a
// problem and we should stop all I/O using this stream.
setstate(badbit) ;
return FlowControl::FLOW_CONTROL_INVALID;
}
catch (const std::exception&)
{
setstate(std::ios_base::failbit) ;
throw ;
}
void
SerialStream::SetParity(const Parity& parityType)
try
{
auto my_buffer = dynamic_cast<SerialStreamBuf *>(this->rdbuf()) ;
// Make sure that we are dealing with a SerialStreamBuf before
// proceeding. This check also makes sure that we have a non-NULL
// buffer associated with this stream.
if (my_buffer != nullptr)
{
// Try to set the parity type. If the corresponding function of the
// SerialStreamBuf class returns PARITY_INVALID, then we have a
// problem and the stream is no longer valid for I/O.
my_buffer->SetParity(parityType) ;
}
else
{
// If the dynamic_cast above failed then we either have a NULL
// streambuf associated with this stream or we have a buffer of
// class other than SerialStreamBuf. In either case, we have a
// problem and we should stop all I/O using this stream.
setstate(badbit) ;
}
}
catch (const std::exception&)
{
setstate(std::ios_base::failbit) ;
throw ;
}
Parity
SerialStream::GetParity()
try
{
auto my_buffer = dynamic_cast<SerialStreamBuf *>(this->rdbuf()) ;
// Make sure that we are dealing with a SerialStreamBuf before
// proceeding. This check also makes sure that we have a non-NULL
// buffer associated with this stream.
if (my_buffer != nullptr)
{
// Try to get the parity type. If the corresponding function of the
// SerialStreamBuf class returns PARITY_INVALID, then we have a
// problem and the stream is no longer valid for I/O.
return my_buffer->GetParity() ;
}
// If the dynamic_cast above failed then we either have a NULL
// streambuf associated with this stream or we have a buffer of
// class other than SerialStreamBuf. In either case, we have a
// problem and we should stop all I/O using this stream.
setstate(badbit) ;
return Parity::PARITY_INVALID;
}
catch (const std::exception&)
{
setstate(std::ios_base::failbit) ;
throw ;
}
void
SerialStream::SetStopBits(const StopBits& stopBits)
try
{
auto my_buffer = dynamic_cast<SerialStreamBuf *>(this->rdbuf()) ;
// Make sure that we are dealing with a SerialStreamBuf before
// proceeding. This check also makes sure that we have a non-NULL
// buffer associated with this stream.
if (my_buffer != nullptr)
{
// Try to set the number of stop bits. If the corresponding function of the
// SerialStreamBuf class returns STOP_BITS_INVALID, then we have a
// problem and the stream is no longer valid for I/O.
my_buffer->SetStopBits(stopBits) ;
}
else
{
// If the dynamic_cast above failed then we either have a NULL
// streambuf associated with this stream or we have a buffer of
// class other than SerialStreamBuf. In either case, we have a
// problem and we should stop all I/O using this stream.
setstate(badbit) ;
}
}
catch (const std::exception&)
{
setstate(std::ios_base::failbit) ;
throw ;
}
StopBits
SerialStream::GetStopBits()
try
{
auto my_buffer = dynamic_cast<SerialStreamBuf *>(this->rdbuf()) ;
// Make sure that we are dealing with a SerialStreamBuf before
// proceeding. This check also makes sure that we have a non-NULL
// buffer associated with this stream.
if (my_buffer != nullptr)
{
// Try to get the number of stop bits. If the corresponding function of the
// SerialStreamBuf class returns STOP_BITS_INVALID, then we have a
// problem and the stream is no longer valid for I/O.
return my_buffer->GetStopBits() ;
}
// If the dynamic_cast above failed then we either have a NULL
// streambuf associated with this stream or we have a buffer of
// class other than SerialStreamBuf. In either case, we have a
// problem and we should stop all I/O using this stream.
setstate(badbit) ;
return StopBits::STOP_BITS_INVALID;
}
catch (const std::exception&)
{
setstate(std::ios_base::failbit) ;
throw ;
}
void
SerialStream::SetVMin(const short vmin)
try
{
auto my_buffer = dynamic_cast<SerialStreamBuf *>(this->rdbuf()) ;
// Make sure that we are dealing with a SerialStreamBuf before
// proceeding. This check also makes sure that we have a non-NULL
// buffer associated with this stream.
if (my_buffer != nullptr)
{
// Try to set the vMin number of characters.
my_buffer->SetVMin(vmin) ;
}
else
{
// If the dynamic_cast above failed then we either have a NULL
// streambuf associated with this stream or we have a buffer of
// class other than SerialStreamBuf. In either case, we have a
// problem and we should stop all I/O using this stream.
setstate(badbit) ;
}
}
catch (const std::exception&)
{
setstate(std::ios_base::failbit) ;
throw ;
}
short
SerialStream::GetVMin()
try
{
auto my_buffer = dynamic_cast<SerialStreamBuf *>(this->rdbuf()) ;
// Make sure that we are dealing with a SerialStreamBuf before
// proceeding. This check also makes sure that we have a non-NULL
// buffer associated with this stream.
if (my_buffer != nullptr)
{
// Try to get the vMin number of characters.
return my_buffer->GetVMin() ;
}
// If the dynamic_cast above failed then we either have a NULL
// streambuf associated with this stream or we have a buffer of
// class other than SerialStreamBuf. In either case, we have a
// problem and we should stop all I/O using this stream.
setstate(badbit) ;
return -1;
}
catch (const std::exception&)
{
setstate(std::ios_base::failbit) ;
throw ;
}
void
SerialStream::SetVTime(const short vtime)
try
{
auto my_buffer = dynamic_cast<SerialStreamBuf *>(this->rdbuf()) ;
// Make sure that we are dealing with a SerialStreamBuf before
// proceeding. This check also makes sure that we have a non-NULL
// buffer associated with this stream.
if (my_buffer != nullptr)
{
// Try to set the vTime duration in deciseconds.
my_buffer->SetVTime(vtime) ;
}
else
{
// If the dynamic_cast above failed then we either have a NULL
// streambuf associated with this stream or we have a buffer of
// class other than SerialStreamBuf. In either case, we have a
// problem and we should stop all I/O using this stream.
setstate(badbit) ;
}
}
catch (const std::exception&)
{
setstate(std::ios_base::failbit) ;
throw ;
}
short
SerialStream::GetVTime()
try
{
auto my_buffer = dynamic_cast<SerialStreamBuf *>(this->rdbuf()) ;
// Make sure that we are dealing with a SerialStreamBuf before
// proceeding. This check also makes sure that we have a non-NULL
// buffer associated with this stream.
if (my_buffer != nullptr)
{
// Try to get the vTime duration in deciseconds.
return my_buffer->GetVTime() ;
}
// If the dynamic_cast above failed then we either have a NULL
// streambuf associated with this stream or we have a buffer of
// class other than SerialStreamBuf. In either case, we have a
// problem and we should stop all I/O using this stream.
setstate(badbit) ;
return -1;
}
catch (const std::exception&)
{
setstate(std::ios_base::failbit) ;
throw ;
}
void
SerialStream::SetDTR(const bool dtrState)
try
{
auto my_buffer = dynamic_cast<SerialStreamBuf *>(this->rdbuf()) ;
// Make sure that we are dealing with a SerialStreamBuf before
// proceeding. This check also makes sure that we have a non-NULL
// buffer associated with this stream.
if (my_buffer != nullptr)
{
// Try to set the dtr line state.
my_buffer->SetDTR(dtrState) ;
}
else
{
// If the dynamic_cast above failed then we either have a NULL
// streambuf associated with this stream or we have a buffer of
// class other than SerialStreamBuf. In either case, we have a
// problem and we should stop all I/O using this stream.
setstate(badbit) ;
}
}
catch (const std::exception&)
{
setstate(std::ios_base::failbit) ;
throw ;
}
bool
SerialStream::GetDTR()
try
{
auto my_buffer = dynamic_cast<SerialStreamBuf *>(this->rdbuf()) ;
// Make sure that we are dealing with a SerialStreamBuf before
// proceeding. This check also makes sure that we have a non-NULL
// buffer associated with this stream.
if (my_buffer != nullptr)
{
// Try to get the vTime duration in deciseconds.
return my_buffer->GetDTR() ;
}
// If the dynamic_cast above failed then we either have a NULL
// streambuf associated with this stream or we have a buffer of
// class other than SerialStreamBuf. In either case, we have a
// problem and we should stop all I/O using this stream.
setstate(badbit) ;
return false ;
}
catch (const std::exception&)
{
setstate(std::ios_base::failbit) ;
throw ;
}
void
SerialStream::SetRTS(const bool rtsState)
try
{
auto my_buffer = dynamic_cast<SerialStreamBuf *>(this->rdbuf()) ;
// Make sure that we are dealing with a SerialStreamBuf before
// proceeding. This check also makes sure that we have a non-NULL
// buffer associated with this stream.
if (my_buffer != nullptr)
{
// Try to set the RTS line state.
my_buffer->SetRTS(rtsState) ;
}
else
{
// If the dynamic_cast above failed then we either have a NULL
// streambuf associated with this stream or we have a buffer of
// class other than SerialStreamBuf. In either case, we have a
// problem and we should stop all I/O using this stream.
setstate(badbit) ;
}
}
catch (const std::exception&)
{
setstate(std::ios_base::failbit) ;
throw ;
}
bool
SerialStream::GetRTS()
try
{
auto my_buffer = dynamic_cast<SerialStreamBuf *>(this->rdbuf()) ;
// Make sure that we are dealing with a SerialStreamBuf before
// proceeding. This check also makes sure that we have a non-NULL
// buffer associated with this stream.
if (my_buffer != nullptr)
{
// Try to get the RTS line state.
return my_buffer->GetRTS() ;
}
// If the dynamic_cast above failed then we either have a NULL
// streambuf associated with this stream or we have a buffer of
// class other than SerialStreamBuf. In either case, we have a
// problem and we should stop all I/O using this stream.
setstate(badbit) ;
return false ;
}
catch (const std::exception&)
{
setstate(std::ios_base::failbit) ;
throw ;
}
bool
SerialStream::GetCTS()
try
{
auto my_buffer = dynamic_cast<SerialStreamBuf *>(this->rdbuf()) ;
// Make sure that we are dealing with a SerialStreamBuf before
// proceeding. This check also makes sure that we have a non-NULL
// buffer associated with this stream.
if (my_buffer != nullptr)
{
// Try to get the CTS line state.
return my_buffer->GetCTS() ;
}
// If the dynamic_cast above failed then we either have a NULL
// streambuf associated with this stream or we have a buffer of
// class other than SerialStreamBuf. In either case, we have a
// problem and we should stop all I/O using this stream.
setstate(badbit) ;
return false ;
}
catch (const std::exception&)
{
setstate(std::ios_base::failbit) ;
throw ;
}
bool
SerialStream::GetDSR()
try
{
auto my_buffer = dynamic_cast<SerialStreamBuf *>(this->rdbuf()) ;
// Make sure that we are dealing with a SerialStreamBuf before
// proceeding. This check also makes sure that we have a non-NULL
// buffer associated with this stream.
if (my_buffer != nullptr)
{
// Try to get the DSR line state.
return my_buffer->GetDSR() ;
}
// If the dynamic_cast above failed then we either have a NULL
// streambuf associated with this stream or we have a buffer of
// class other than SerialStreamBuf. In either case, we have a
// problem and we should stop all I/O using this stream.
setstate(badbit) ;
return false ;
}
catch (const std::exception&)
{
setstate(std::ios_base::failbit) ;
throw ;
}
int
SerialStream::GetFileDescriptor()
try
{
auto my_buffer = dynamic_cast<SerialStreamBuf *>(this->rdbuf()) ;
// Make sure that we are dealing with a SerialStreamBuf before
// proceeding. This check also makes sure that we have a non-NULL
// buffer associated with this stream.
if (my_buffer != nullptr)
{
// Try to get the file descriptor.
return my_buffer->GetFileDescriptor() ;
}
// If the dynamic_cast above failed then we either have a NULL
// streambuf associated with this stream or we have a buffer of
// class other than SerialStreamBuf. In either case, we have a
// problem and we should stop all I/O using this stream.
setstate(badbit) ;
return -1;
}
catch (const std::exception&)
{
setstate(std::ios_base::failbit) ;
throw ;
}
int
SerialStream::GetNumberOfBytesAvailable()
try
{
auto my_buffer = dynamic_cast<SerialStreamBuf *>(this->rdbuf()) ;
// Make sure that we are dealing with a SerialStreamBuf before
// proceeding. This check also makes sure that we have a non-NULL
// buffer associated with this stream.
if (my_buffer != nullptr)
{
// Try to determine if data is available with the correspoding
// function of the SerialStreamBuf class.
return my_buffer->GetNumberOfBytesAvailable() ;
}
// If the dynamic_cast above failed then we either have a NULL
// streambuf associated with this stream or we have a buffer
// of class other than SerialStreamBuf. In either case, we
// have a problem and we should stop all I/O using this stream.
setstate(badbit) ;
return -1 ;
}
catch (const std::exception&)
{
setstate(std::ios_base::failbit) ;
throw ;
}
#ifdef __linux__
std::vector<std::string>
SerialStream::GetAvailableSerialPorts()
try
{
auto my_buffer = dynamic_cast<SerialStreamBuf *>(this->rdbuf()) ;
// Make sure that we are dealing with a SerialStreamBuf before
// proceeding. This check also makes sure that we have a non-NULL
// buffer associated with this stream.
if (my_buffer != nullptr)
{
// Try to get the file descriptor.
return my_buffer->GetAvailableSerialPorts() ;
}
// If the dynamic_cast above failed then we either have a NULL
// streambuf associated with this stream or we have a buffer of
// class other than SerialStreamBuf. In either case, we have a
// problem and we should stop all I/O using this stream.
setstate(badbit) ;
return {} ;
}
catch (const std::exception&)
{
setstate(std::ios_base::failbit) ;
throw ;
}
#endif
} // namespace LibSerial
LibSerial::SerialStreamBuf
SerialStreamBuf is the streambuf subclass used by SerialStream. This subclass takes care of opening t...
Definition SerialStreamBuf.h:59
LibSerial::SerialStream::FlushIOBuffers
void FlushIOBuffers()
Flushes the serial port input and output buffers.
Definition SerialStream.cpp:203
LibSerial::SerialStream::DrainWriteBuffer
void DrainWriteBuffer()
Waits until the write buffer is drained and then returns.
Definition SerialStream.cpp:113
LibSerial::SerialStream::GetVMin
short GetVMin()
Gets the VMIN value for the device, which represents the minimum number of characters for non-canonic...
Definition SerialStream.cpp:606
LibSerial::SerialStream::GetBaudRate
BaudRate GetBaudRate()
Gets the current baud rate for the serial port.
Definition SerialStream.cpp:309
LibSerial::SerialStream::GetDSR
bool GetDSR()
Get the status of the DSR line.
Definition SerialStream.cpp:828
LibSerial::SerialStream::SetCharacterSize
void SetCharacterSize(const CharacterSize &characterSize)
Sets the character size for the serial port.
Definition SerialStream.cpp:338
LibSerial::SerialStream::GetCTS
bool GetCTS()
Get the status of the CTS line.
Definition SerialStream.cpp:801
LibSerial::SerialStream::SetBaudRate
void SetBaudRate(const BaudRate &baudRate)
Sets the baud rate for the serial port to the specified value.
Definition SerialStream.cpp:279
LibSerial::SerialStream::GetNumberOfBytesAvailable
int GetNumberOfBytesAvailable()
Gets the number of bytes available in the read buffer.
Definition SerialStream.cpp:882
LibSerial::SerialStream::IsDataAvailable
bool IsDataAvailable()
Checks if data is available at the input of the serial port.
Definition SerialStream.cpp:233
LibSerial::SerialStream::FlushInputBuffer
void FlushInputBuffer()
Flushes the serial port input buffer.
Definition SerialStream.cpp:143
LibSerial::SerialStream::GetVTime
short GetVTime()
Gets the current timeout value for non-canonical reads in deciseconds.
Definition SerialStream.cpp:662
LibSerial::SerialStream::Close
void Close()
Closes the serial port. All settings of the serial port will be lost and no more I/O can be performed...
Definition SerialStream.cpp:105
LibSerial::SerialStream::~SerialStream
virtual ~SerialStream()
Default Destructor for a SerialStream object Closes the stream associated with mFileDescriptor,...
Definition SerialStream.cpp:61
LibSerial::SerialStream::GetStopBits
StopBits GetStopBits()
Gets the number of stop bits currently being used by the serial.
Definition SerialStream.cpp:548
LibSerial::SerialStream::SetDTR
void SetDTR(const bool dtrState=true)
Sets the DTR line to the specified value.
Definition SerialStream.cpp:689
LibSerial::SerialStream::SetFlowControl
void SetFlowControl(const FlowControl &flowControlType)
Sets flow control for the serial port.
Definition SerialStream.cpp:397
LibSerial::SerialStream::GetFileDescriptor
int GetFileDescriptor()
Gets the serial port file descriptor.
Definition SerialStream.cpp:855
LibSerial::SerialStream::SerialStream
SerialStream()
Default Contructor. Creates a new SerialStream object but does not open it. The Open() method will ne...
Definition SerialStream.cpp:39
LibSerial::SerialStream::GetFlowControl
FlowControl GetFlowControl()
Gets the current flow control setting.
Definition SerialStream.cpp:428
LibSerial::SerialStream::SetVMin
void SetVMin(const short vmin)
Sets the minimum number of characters for non-canonical reads.
Definition SerialStream.cpp:577
LibSerial::SerialStream::SetStopBits
void SetStopBits(const StopBits &stopBits)
Sets the number of stop bits to be used with the serial port.
Definition SerialStream.cpp:517
LibSerial::SerialStream::SetVTime
void SetVTime(const short vtime)
Sets character buffer timeout for non-canonical reads in deciseconds.
Definition SerialStream.cpp:633
LibSerial::SerialStream::IsOpen
bool IsOpen()
Determines if the serial port is open for I/O.
Definition SerialStream.cpp:261
LibSerial::SerialStream::GetDTR
bool GetDTR()
Gets the status of the DTR line.
Definition SerialStream.cpp:718
LibSerial::SerialStream::GetCharacterSize
CharacterSize GetCharacterSize()
Gets the character size being used for serial communication.
Definition SerialStream.cpp:368
LibSerial::SerialStream::SetRTS
void SetRTS(const bool rtsState=true)
Set the RTS line to the specified value.
Definition SerialStream.cpp:745
LibSerial::SerialStream::Open
void Open(const std::string &fileName, const std::ios_base::openmode &openMode=std::ios_base::in|std::ios_base::out)
Opens the serial port associated with the specified file name and the specified mode.
Definition SerialStream.cpp:83
LibSerial::SerialStream::GetAvailableSerialPorts
std::vector< std::string > GetAvailableSerialPorts()
Gets a list of available serial ports.
LibSerial::SerialStream::FlushOutputBuffer
void FlushOutputBuffer()
Flushes the serial port output buffer.
Definition SerialStream.cpp:173
LibSerial::SerialStream::GetParity
Parity GetParity()
Gets the parity type for the serial port.
Definition SerialStream.cpp:488
LibSerial::SerialStream::GetRTS
bool GetRTS()
Get the status of the RTS line.
Definition SerialStream.cpp:774
LibSerial::SerialStream::SetParity
void SetParity(const Parity &parityType)
Sets the parity type for the serial port.
Definition SerialStream.cpp:457