Loading...
Searching...
No Matches
SerialStreamBuf.h
/******************************************************************************
* @file SerialStreamBuf.h *
* @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. *
*****************************************************************************/
#pragma once
#include <libserial/SerialPortConstants.h>
#include <memory>
#include <streambuf>
#include <vector>
namespace LibSerial
{
class SerialStreamBuf : public std::streambuf
{
public:
explicit SerialStreamBuf() ;
explicit 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) ;
virtual ~SerialStreamBuf() ;
SerialStreamBuf(const SerialStreamBuf& otherSerialStreamBuf) = delete ;
SerialStreamBuf(const SerialStreamBuf&& otherSerialStreamBuf) = delete ;
SerialStreamBuf& operator=(const SerialStreamBuf& otherSerialStreamBuf) = delete ;
SerialStreamBuf& operator=(const SerialStreamBuf&& otherSerialStreamBuf) = delete ;
void Open(const std::string& fileName,
const std::ios_base::openmode& openMode = std::ios_base::in | std::ios_base::out) ;
void Close() ;
void DrainWriteBuffer() ;
void FlushInputBuffer() ;
void FlushOutputBuffer() ;
void FlushIOBuffers() ;
bool IsDataAvailable() ;
bool IsOpen() const ;
void SetDefaultSerialPortParameters() ;
void SetBaudRate(const BaudRate& baudRate) ;
BaudRate GetBaudRate() const ;
void SetCharacterSize(const CharacterSize& characterSize) ;
CharacterSize GetCharacterSize() const ;
void SetFlowControl(const FlowControl& flowControlType) ;
FlowControl GetFlowControl() const ;
void SetParity(const Parity& parityType) ;
Parity GetParity() const ;
void SetStopBits(const StopBits& stopBits) ;
StopBits GetStopBits() const ;
void SetVMin(const short vmin) ;
short GetVMin() const ;
void SetVTime(const short vtime) ;
short GetVTime() const ;
void SetDTR(const bool dtrState = true) ;
bool GetDTR() const ;
void SetRTS(const bool rtsState = true) ;
bool GetRTS() const ;
bool GetCTS() ;
bool GetDSR() ;
int GetFileDescriptor() const ;
int GetNumberOfBytesAvailable() ;
#ifdef __linux__
std::vector<std::string> GetAvailableSerialPorts() const ;
#endif
protected:
virtual std::streambuf* setbuf(char_type* character,
std::streamsize numberOfBytes) override ;
virtual std::streamsize xsputn(const char_type* character,
std::streamsize numberOfBytes) override ;
virtual std::streamsize xsgetn(char_type* character,
std::streamsize numberOfBytes) override ;
virtual int_type overflow(const int_type character) override ;
virtual int_type underflow() override ;
virtual int_type uflow() override ;
virtual int_type pbackfail(const int_type character) override ;
virtual std::streamsize showmanyc() override ;
private:
class Implementation;
std::unique_ptr<Implementation> mImpl;
} ; // class SerialStreamBuf
} // namespace LibSerial
LibSerial::SerialStreamBuf::Implementation
SerialStreamBuf::Implementation is the SerialStreamBuf implementation class.
Definition SerialStreamBuf.cpp:49
LibSerial::SerialStreamBuf::GetCharacterSize
CharacterSize GetCharacterSize() const
Gets the character size being used for serial communication.
Definition SerialStreamBuf.cpp:481
LibSerial::SerialStreamBuf::GetCTS
bool GetCTS()
Get the status of the CTS line.
Definition SerialStreamBuf.cpp:571
LibSerial::SerialStreamBuf::IsOpen
bool IsOpen() const
Determines if the serial port is open for I/O.
Definition SerialStreamBuf.cpp:451
LibSerial::SerialStreamBuf::~SerialStreamBuf
virtual ~SerialStreamBuf()
Default Destructor for a SerialStreamBuf object. Closes the serial port associated with mFileDescript...
LibSerial::SerialStreamBuf::setbuf
virtual std::streambuf * setbuf(char_type *character, std::streamsize numberOfBytes) override
Performs an operation that is defined separately for each class derived from streambuf....
Definition SerialStreamBuf.cpp:603
LibSerial::SerialStreamBuf::GetNumberOfBytesAvailable
int GetNumberOfBytesAvailable()
Gets the number of bytes available in the read buffer.
Definition SerialStreamBuf.cpp:589
LibSerial::SerialStreamBuf::showmanyc
virtual std::streamsize showmanyc() override
Checks whether input is available on the port. If you call SerialStream::in_avail,...
Definition SerialStreamBuf.cpp:645
LibSerial::SerialStreamBuf::overflow
virtual int_type overflow(const int_type character) override
Writes the specified character to the associated serial port.
Definition SerialStreamBuf.cpp:621
LibSerial::SerialStreamBuf::IsDataAvailable
bool IsDataAvailable()
Checks if data is available at the input of the serial port.
Definition SerialStreamBuf.cpp:445
LibSerial::SerialStreamBuf::pbackfail
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 ...
Definition SerialStreamBuf.cpp:639
LibSerial::SerialStreamBuf::SerialStreamBuf
SerialStreamBuf()
Default Constructor.
Definition SerialStreamBuf.cpp:382
LibSerial::SerialStreamBuf::FlushOutputBuffer
void FlushOutputBuffer()
Flushes the serial port output buffer.
Definition SerialStreamBuf.cpp:433
LibSerial::SerialStreamBuf::xsgetn
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...
Definition SerialStreamBuf.cpp:615
LibSerial::SerialStreamBuf::SetDefaultSerialPortParameters
void SetDefaultSerialPortParameters()
Sets all serial port paramters to their default values.
Definition SerialStreamBuf.cpp:457
LibSerial::SerialStreamBuf::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 SerialStreamBuf.cpp:407
LibSerial::SerialStreamBuf::SerialStreamBuf
SerialStreamBuf(const SerialStreamBuf &&otherSerialStreamBuf)=delete
Move construction is disallowed.
LibSerial::SerialStreamBuf::GetStopBits
StopBits GetStopBits() const
Gets the number of stop bits currently being used by the serial.
Definition SerialStreamBuf.cpp:517
LibSerial::SerialStreamBuf::SetDTR
void SetDTR(const bool dtrState=true)
Sets the DTR line to the specified value.
Definition SerialStreamBuf.cpp:547
LibSerial::SerialStreamBuf::GetVMin
short GetVMin() const
Gets the VMIN value for the device, which represents the minimum number of characters for non-canonic...
Definition SerialStreamBuf.cpp:529
LibSerial::SerialStreamBuf::GetDSR
bool GetDSR()
Get the status of the DSR line.
Definition SerialStreamBuf.cpp:577
LibSerial::SerialStreamBuf::xsputn
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 th...
Definition SerialStreamBuf.cpp:609
LibSerial::SerialStreamBuf::SerialStreamBuf
SerialStreamBuf(const SerialStreamBuf &otherSerialStreamBuf)=delete
Copy construction is disallowed.
LibSerial::SerialStreamBuf::SetVTime
void SetVTime(const short vtime)
Sets character buffer timeout for non-canonical reads in deciseconds.
Definition SerialStreamBuf.cpp:535
LibSerial::SerialStreamBuf::GetRTS
bool GetRTS() const
Get the status of the RTS line.
Definition SerialStreamBuf.cpp:565
LibSerial::SerialStreamBuf::underflow
virtual int_type underflow() override
Reads and returns the next character from the associated serial port if one otherwise returns traits:...
Definition SerialStreamBuf.cpp:627
LibSerial::SerialStreamBuf::GetVTime
short GetVTime() const
Gets the current timeout value for non-canonical reads in deciseconds.
Definition SerialStreamBuf.cpp:541
LibSerial::SerialStreamBuf::GetDTR
bool GetDTR() const
Gets the status of the DTR line.
Definition SerialStreamBuf.cpp:553
LibSerial::SerialStreamBuf::SetCharacterSize
void SetCharacterSize(const CharacterSize &characterSize)
Sets the character size for the serial port.
Definition SerialStreamBuf.cpp:475
LibSerial::SerialStreamBuf::SetVMin
void SetVMin(const short vmin)
Sets the minimum number of characters for non-canonical reads.
Definition SerialStreamBuf.cpp:523
LibSerial::SerialStreamBuf::GetFlowControl
FlowControl GetFlowControl() const
Gets the current flow control setting.
Definition SerialStreamBuf.cpp:493
LibSerial::SerialStreamBuf::DrainWriteBuffer
void DrainWriteBuffer()
Waits until the write buffer is drained and then returns.
Definition SerialStreamBuf.cpp:421
LibSerial::SerialStreamBuf::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 SerialStreamBuf.cpp:415
LibSerial::SerialStreamBuf::GetBaudRate
BaudRate GetBaudRate() const
Gets the current baud rate for the serial port.
Definition SerialStreamBuf.cpp:469
LibSerial::SerialStreamBuf::SetParity
void SetParity(const Parity &parityType)
Sets the parity type for the serial port.
Definition SerialStreamBuf.cpp:499
LibSerial::SerialStreamBuf::SetBaudRate
void SetBaudRate(const BaudRate &baudRate)
Sets the baud rate for the serial port to the specified value.
Definition SerialStreamBuf.cpp:463
LibSerial::SerialStreamBuf::GetParity
Parity GetParity() const
Gets the parity type for the serial port.
Definition SerialStreamBuf.cpp:505
LibSerial::SerialStreamBuf::FlushIOBuffers
void FlushIOBuffers()
Flushes the serial port input and output buffers.
Definition SerialStreamBuf.cpp:439
LibSerial::SerialStreamBuf::SetFlowControl
void SetFlowControl(const FlowControl &flowControlType)
Sets flow control for the serial port.
Definition SerialStreamBuf.cpp:487
LibSerial::SerialStreamBuf::FlushInputBuffer
void FlushInputBuffer()
Flushes the serial port input buffer.
Definition SerialStreamBuf.cpp:427
LibSerial::SerialStreamBuf::operator=
SerialStreamBuf & operator=(const SerialStreamBuf &otherSerialStreamBuf)=delete
Copy assignment is disallowed.
LibSerial::SerialStreamBuf::GetFileDescriptor
int GetFileDescriptor() const
Gets the serial port file descriptor.
Definition SerialStreamBuf.cpp:583
LibSerial::SerialStreamBuf::operator=
SerialStreamBuf & operator=(const SerialStreamBuf &&otherSerialStreamBuf)=delete
Move assignment is disallowed.
LibSerial::SerialStreamBuf::SetStopBits
void SetStopBits(const StopBits &stopBits)
Sets the number of stop bits to be used with the serial port.
Definition SerialStreamBuf.cpp:511
LibSerial::SerialStreamBuf::SetRTS
void SetRTS(const bool rtsState=true)
Set the RTS line to the specified value.
Definition SerialStreamBuf.cpp:559
LibSerial::SerialStreamBuf::uflow
virtual int_type uflow() override
Reads and returns the next character from the associated serial port if one otherwise returns traits:...
Definition SerialStreamBuf.cpp:633