33#include "libserial/SerialStream.h"
45 const BaudRate& baudRate,
46 const CharacterSize& characterSize,
47 const FlowControl& flowControlType,
48 const Parity& parityType,
49 const StopBits& stopBits) :
50 std::iostream(nullptr)
52 this->
Open(fileName) ;
84 const std::ios_base::openmode& openMode)
88 if (mIOBuffer ==
nullptr)
90 mIOBuffer = std::make_unique<SerialStreamBuf>() ;
91 assert(mIOBuffer !=
nullptr) ;
92 this->rdbuf(mIOBuffer.get()) ;
96 mIOBuffer->Open(fileName, openMode) ;
98 catch (
const std::exception&)
100 setstate(std::ios_base::failbit) ;
109 mIOBuffer = nullptr ;
121 if (my_buffer !=
nullptr)
136 catch (
const std::exception&)
138 setstate(std::ios_base::failbit) ;
151 if (my_buffer !=
nullptr)
166 catch (
const std::exception&)
168 setstate(std::ios_base::failbit) ;
181 if (my_buffer !=
nullptr)
196 catch (
const std::exception&)
198 setstate(std::ios_base::failbit) ;
211 if (my_buffer !=
nullptr)
226 catch (
const std::exception&)
228 setstate(std::ios_base::failbit) ;
241 if (my_buffer !=
nullptr)
254 catch (
const std::exception&)
256 setstate(std::ios_base::failbit) ;
266 if (mIOBuffer ==
nullptr)
270 return mIOBuffer->IsOpen() ;
272 catch (
const std::exception&)
274 setstate(std::ios_base::failbit) ;
287 if (my_buffer !=
nullptr)
302 catch (
const std::exception&)
304 setstate(std::ios_base::failbit) ;
317 if (my_buffer !=
nullptr)
329 return BaudRate::BAUD_INVALID;
331 catch (
const std::exception&)
333 setstate(std::ios_base::failbit) ;
346 if (my_buffer !=
nullptr)
361 catch (
const std::exception&)
363 setstate(std::ios_base::failbit) ;
376 if (my_buffer !=
nullptr)
388 return CharacterSize::CHAR_SIZE_INVALID;
390 catch (
const std::exception&)
392 setstate(std::ios_base::failbit) ;
405 if (my_buffer !=
nullptr)
421 catch (
const std::exception&)
423 setstate(std::ios_base::failbit) ;
436 if (my_buffer !=
nullptr)
448 return FlowControl::FLOW_CONTROL_INVALID;
450 catch (
const std::exception&)
452 setstate(std::ios_base::failbit) ;
465 if (my_buffer !=
nullptr)
481 catch (
const std::exception&)
483 setstate(std::ios_base::failbit) ;
496 if (my_buffer !=
nullptr)
508 return Parity::PARITY_INVALID;
510 catch (
const std::exception&)
512 setstate(std::ios_base::failbit) ;
525 if (my_buffer !=
nullptr)
541 catch (
const std::exception&)
543 setstate(std::ios_base::failbit) ;
556 if (my_buffer !=
nullptr)
568 return StopBits::STOP_BITS_INVALID;
570 catch (
const std::exception&)
572 setstate(std::ios_base::failbit) ;
585 if (my_buffer !=
nullptr)
599 catch (
const std::exception&)
601 setstate(std::ios_base::failbit) ;
614 if (my_buffer !=
nullptr)
626 catch (
const std::exception&)
628 setstate(std::ios_base::failbit) ;
641 if (my_buffer !=
nullptr)
655 catch (
const std::exception&)
657 setstate(std::ios_base::failbit) ;
670 if (my_buffer !=
nullptr)
682 catch (
const std::exception&)
684 setstate(std::ios_base::failbit) ;
697 if (my_buffer !=
nullptr)
700 my_buffer->
SetDTR(dtrState) ;
711 catch (
const std::exception&)
713 setstate(std::ios_base::failbit) ;
726 if (my_buffer !=
nullptr)
729 return my_buffer->
GetDTR() ;
738 catch (
const std::exception&)
740 setstate(std::ios_base::failbit) ;
753 if (my_buffer !=
nullptr)
756 my_buffer->
SetRTS(rtsState) ;
767 catch (
const std::exception&)
769 setstate(std::ios_base::failbit) ;
782 if (my_buffer !=
nullptr)
785 return my_buffer->
GetRTS() ;
794 catch (
const std::exception&)
796 setstate(std::ios_base::failbit) ;
809 if (my_buffer !=
nullptr)
812 return my_buffer->
GetCTS() ;
821 catch (
const std::exception&)
823 setstate(std::ios_base::failbit) ;
836 if (my_buffer !=
nullptr)
839 return my_buffer->
GetDSR() ;
848 catch (
const std::exception&)
850 setstate(std::ios_base::failbit) ;
863 if (my_buffer !=
nullptr)
875 catch (
const std::exception&)
877 setstate(std::ios_base::failbit) ;
890 if (my_buffer !=
nullptr)
903 catch (
const std::exception&)
905 setstate(std::ios_base::failbit) ;
910 std::vector<std::string>
914 auto my_buffer =
dynamic_cast<SerialStreamBuf *
>(this->rdbuf()) ;
919 if (my_buffer !=
nullptr)
922 return my_buffer->GetAvailableSerialPorts() ;
931 catch (
const std::exception&)
933 setstate(std::ios_base::failbit) ;
SerialStreamBuf is the streambuf subclass used by SerialStream. This subclass takes care of opening t...
CharacterSize GetCharacterSize() const
Gets the character size being used for serial communication.
bool GetCTS()
Get the status of the CTS line.
int GetNumberOfBytesAvailable()
Gets the number of bytes available in the read buffer.
bool IsDataAvailable()
Checks if data is available at the input of the serial port.
void FlushOutputBuffer()
Flushes the serial port output buffer.
StopBits GetStopBits() const
Gets the number of stop bits currently being used by the serial.
void SetDTR(const bool dtrState=true)
Sets the DTR line to the specified value.
short GetVMin() const
Gets the VMIN value for the device, which represents the minimum number of characters for non-canonic...
bool GetDSR()
Get the status of the DSR line.
void SetVTime(const short vtime)
Sets character buffer timeout for non-canonical reads in deciseconds.
bool GetRTS() const
Get the status of the RTS line.
short GetVTime() const
Gets the current timeout value for non-canonical reads in deciseconds.
bool GetDTR() const
Gets the status of the DTR line.
void SetCharacterSize(const CharacterSize &characterSize)
Sets the character size for the serial port.
void SetVMin(const short vmin)
Sets the minimum number of characters for non-canonical reads.
FlowControl GetFlowControl() const
Gets the current flow control setting.
void DrainWriteBuffer()
Waits until the write buffer is drained and then returns.
BaudRate GetBaudRate() const
Gets the current baud rate for the serial port.
void SetParity(const Parity &parityType)
Sets the parity type for the serial port.
void SetBaudRate(const BaudRate &baudRate)
Sets the baud rate for the serial port to the specified value.
Parity GetParity() const
Gets the parity type for the serial port.
void FlushIOBuffers()
Flushes the serial port input and output buffers.
void SetFlowControl(const FlowControl &flowControlType)
Sets flow control for the serial port.
void FlushInputBuffer()
Flushes the serial port input buffer.
int GetFileDescriptor() const
Gets the serial port file descriptor.
void SetStopBits(const StopBits &stopBits)
Sets the number of stop bits to be used with the serial port.
void SetRTS(const bool rtsState=true)
Set the RTS line to the specified value.
void FlushIOBuffers()
Flushes the serial port input and output buffers.
void DrainWriteBuffer()
Waits until the write buffer is drained and then returns.
short GetVMin()
Gets the VMIN value for the device, which represents the minimum number of characters for non-canonic...
BaudRate GetBaudRate()
Gets the current baud rate for the serial port.
bool GetDSR()
Get the status of the DSR line.
void SetCharacterSize(const CharacterSize &characterSize)
Sets the character size for the serial port.
bool GetCTS()
Get the status of the CTS line.
void SetBaudRate(const BaudRate &baudRate)
Sets the baud rate for the serial port to the specified value.
int GetNumberOfBytesAvailable()
Gets the number of bytes available in the read buffer.
bool IsDataAvailable()
Checks if data is available at the input of the serial port.
void FlushInputBuffer()
Flushes the serial port input buffer.
short GetVTime()
Gets the current timeout value for non-canonical reads in deciseconds.
void Close()
Closes the serial port. All settings of the serial port will be lost and no more I/O can be performed...
virtual ~SerialStream()
Default Destructor for a SerialStream object Closes the stream associated with mFileDescriptor,...
StopBits GetStopBits()
Gets the number of stop bits currently being used by the serial.
void SetDTR(const bool dtrState=true)
Sets the DTR line to the specified value.
void SetFlowControl(const FlowControl &flowControlType)
Sets flow control for the serial port.
int GetFileDescriptor()
Gets the serial port file descriptor.
SerialStream()
Default Contructor. Creates a new SerialStream object but does not open it. The Open() method will ne...
FlowControl GetFlowControl()
Gets the current flow control setting.
void SetVMin(const short vmin)
Sets the minimum number of characters for non-canonical reads.
void SetStopBits(const StopBits &stopBits)
Sets the number of stop bits to be used with the serial port.
void SetVTime(const short vtime)
Sets character buffer timeout for non-canonical reads in deciseconds.
bool IsOpen()
Determines if the serial port is open for I/O.
bool GetDTR()
Gets the status of the DTR line.
CharacterSize GetCharacterSize()
Gets the character size being used for serial communication.
void SetRTS(const bool rtsState=true)
Set the RTS line to the specified value.
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.
std::vector< std::string > GetAvailableSerialPorts()
Gets a list of available serial ports.
void FlushOutputBuffer()
Flushes the serial port output buffer.
Parity GetParity()
Gets the parity type for the serial port.
bool GetRTS()
Get the status of the RTS line.
void SetParity(const Parity &parityType)
Sets the parity type for the serial port.