Loading...
Searching...
No Matches
SerialStream.h
/******************************************************************************
* @file SerialStream.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 <libserial/SerialStreamBuf.h>
#include <istream>
#include <memory>
namespace LibSerial
{
class SerialStream : public std::iostream
{
public:
explicit SerialStream() ;
explicit SerialStream(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 ~SerialStream() ;
SerialStream(const SerialStream& otherSerialStream) = delete;
SerialStream(const SerialStream&& otherSerialStream) = delete;
SerialStream& operator=(const SerialStream& otherSerialStream) = delete;
SerialStream& operator=(const SerialStream&& otherSerialStream) = 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() ;
void SetBaudRate(const BaudRate& baudRate) ;
BaudRate GetBaudRate() ;
void SetCharacterSize(const CharacterSize& characterSize) ;
CharacterSize GetCharacterSize() ;
void SetFlowControl(const FlowControl& flowControlType) ;
FlowControl GetFlowControl() ;
void SetParity(const Parity& parityType) ;
Parity GetParity() ;
void SetStopBits(const StopBits& stopBits) ;
StopBits GetStopBits() ;
void SetVMin(const short vmin) ;
short GetVMin() ;
void SetVTime(const short vtime) ;
short GetVTime() ;
void SetDTR(const bool dtrState = true) ;
bool GetDTR() ;
void SetRTS(const bool rtsState = true) ;
bool GetRTS() ;
bool GetCTS() ;
bool GetDSR() ;
int GetFileDescriptor() ;
int GetNumberOfBytesAvailable() ;
std::vector<std::string> GetAvailableSerialPorts() ;
protected:
private:
std::unique_ptr<SerialStreamBuf> mIOBuffer {nullptr} ;
} ; // class SerialStream
} // namespace LibSerial
LibSerial::SerialStream
SerialStream is a stream class for accessing serial ports on POSIX operating systems....
Definition SerialStream.h:74
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::operator=
SerialStream & operator=(const SerialStream &otherSerialStream)=delete
Prevents copying of objects of this class by declaring the assignment operator private....
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::SerialStream
SerialStream(const SerialStream &otherSerialStream)=delete
Prevents copying of objects of this class by declaring the copy constructor private....
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::operator=
SerialStream & operator=(const SerialStream &&otherSerialStream)=delete
Move assignment is not allowed.
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::SerialStream
SerialStream(const SerialStream &&otherSerialStream)=delete
Move construction is disallowed.
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