LibSerial 1.0.0
LibSerial provides a convenient, object oriented approach to accessing serial ports on POSIX systems.
Loading...
Searching...
No Matches
SerialStreamBuf.h
1/******************************************************************************
2 * @file SerialStreamBuf.h *
3 * @copyright (C) 2004-2018 LibSerial Development Team. All rights reserved. *
4 * crayzeewulf@gmail.com *
5 * *
6 * Redistribution and use in source and binary forms, with or without *
7 * modification, are permitted provided that the following conditions *
8 * are met: *
9 * *
10 * 1. Redistributions of source code must retain the above copyright *
11 * notice, this list of conditions and the following disclaimer. *
12 * 2. Redistributions in binary form must reproduce the above copyright *
13 * notice, this list of conditions and the following disclaimer in *
14 * the documentation and/or other materials provided with the *
15 * distribution. *
16 * 3. Neither the name PX4 nor the names of its contributors may be *
17 * used to endorse or promote products derived from this software *
18 * without specific prior written permission. *
19 * *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT *
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS *
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE *
24 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, *
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, *
26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS *
27 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED *
28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT *
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN *
30 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE *
31 * POSSIBILITY OF SUCH DAMAGE. *
32 *****************************************************************************/
33
34#pragma once
35
36#include <libserial/SerialPortConstants.h>
37
38#include <memory>
39#include <streambuf>
40#include <vector>
41
42namespace LibSerial
43{
58 class SerialStreamBuf : public std::streambuf
59 {
60 public:
61
65 explicit SerialStreamBuf() ;
66
79 explicit SerialStreamBuf(const std::string& fileName,
80 const BaudRate& baudRate = BaudRate::BAUD_DEFAULT,
81 const CharacterSize& characterSize = CharacterSize::CHAR_SIZE_DEFAULT,
82 const FlowControl& flowControlType = FlowControl::FLOW_CONTROL_DEFAULT,
83 const Parity& parityType = Parity::PARITY_DEFAULT,
84 const StopBits& stopBits = StopBits::STOP_BITS_DEFAULT) ;
85
90 virtual ~SerialStreamBuf() ;
91
95 SerialStreamBuf(const SerialStreamBuf& otherSerialStreamBuf) = delete ;
96
100 SerialStreamBuf(const SerialStreamBuf&& otherSerialStreamBuf) = delete ;
101
105 SerialStreamBuf& operator=(const SerialStreamBuf& otherSerialStreamBuf) = delete ;
106
110 SerialStreamBuf& operator=(const SerialStreamBuf&& otherSerialStreamBuf) = delete ;
111
119 void Open(const std::string& fileName,
120 const std::ios_base::openmode& openMode = std::ios_base::in | std::ios_base::out) ;
121
126 void Close() ;
127
131 void DrainWriteBuffer() ;
132
136 void FlushInputBuffer() ;
137
141 void FlushOutputBuffer() ;
142
146 void FlushIOBuffers() ;
147
152 bool IsDataAvailable() ;
153
158 bool IsOpen() const ;
159
164
169 void SetBaudRate(const BaudRate& baudRate) ;
170
175 BaudRate GetBaudRate() const ;
176
181 void SetCharacterSize(const CharacterSize& characterSize) ;
182
187 CharacterSize GetCharacterSize() const ;
188
193 void SetFlowControl(const FlowControl& flowControlType) ;
194
199 FlowControl GetFlowControl() const ;
200
205 void SetParity(const Parity& parityType) ;
206
211 Parity GetParity() const ;
212
217 void SetStopBits(const StopBits& stopBits) ;
218
223 StopBits GetStopBits() const ;
224
230 void SetVMin(const short vmin) ;
231
238 short GetVMin() const ;
239
245 void SetVTime(const short vtime) ;
246
251 short GetVTime() const ;
252
258 void SetDTR(const bool dtrState = true) ;
259
264 bool GetDTR() const ;
265
271 void SetRTS(const bool rtsState = true) ;
272
277 bool GetRTS() const ;
278
283 bool GetCTS() ;
284
289 bool GetDSR() ;
290
295 int GetFileDescriptor() const ;
296
302
303#ifdef __linux__
309 std::vector<std::string> GetAvailableSerialPorts() const ;
310#endif
311
312 protected:
313
334 virtual std::streambuf* setbuf(char_type* character,
335 std::streamsize numberOfBytes) override ;
336
345 virtual std::streamsize xsputn(const char_type* character,
346 std::streamsize numberOfBytes) override ;
347
356 virtual std::streamsize xsgetn(char_type* character,
357 std::streamsize numberOfBytes) override ;
358
364 virtual int_type overflow(const int_type character) override ;
365
373 virtual int_type underflow() override ;
374
382 virtual int_type uflow() override ;
383
392 virtual int_type pbackfail(const int_type character) override ;
393
408 virtual std::streamsize showmanyc() override ;
409
410 private:
411
416 class Implementation;
417
421 std::unique_ptr<Implementation> mImpl;
422
423 } ; // class SerialStreamBuf
424
425} // namespace LibSerial
SerialStreamBuf::Implementation is the SerialStreamBuf implementation class.
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.
bool IsOpen() const
Determines if the serial port is open for I/O.
virtual ~SerialStreamBuf()
Default Destructor for a SerialStreamBuf object. Closes the serial port associated with mFileDescript...
virtual std::streambuf * setbuf(char_type *character, std::streamsize numberOfBytes) override
Performs an operation that is defined separately for each class derived from streambuf....
int GetNumberOfBytesAvailable()
Gets the number of bytes available in the read buffer.
virtual std::streamsize showmanyc() override
Checks whether input is available on the port. If you call SerialStream::in_avail,...
virtual int_type overflow(const int_type character) override
Writes the specified character to the associated serial port.
bool IsDataAvailable()
Checks if data is available at the input of the serial port.
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 ...
SerialStreamBuf()
Default Constructor.
void FlushOutputBuffer()
Flushes the serial port output 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...
void SetDefaultSerialPortParameters()
Sets all serial port paramters to their default values.
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.
SerialStreamBuf(const SerialStreamBuf &&otherSerialStreamBuf)=delete
Move construction is disallowed.
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.
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...
SerialStreamBuf(const SerialStreamBuf &otherSerialStreamBuf)=delete
Copy construction is disallowed.
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.
virtual int_type underflow() override
Reads and returns the next character from the associated serial port if one otherwise returns traits:...
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.
void Close()
Closes the serial port. All settings of the serial port will be lost and no more I/O can be performed...
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.
SerialStreamBuf & operator=(const SerialStreamBuf &otherSerialStreamBuf)=delete
Copy assignment is disallowed.
int GetFileDescriptor() const
Gets the serial port file descriptor.
SerialStreamBuf & operator=(const SerialStreamBuf &&otherSerialStreamBuf)=delete
Move assignment is disallowed.
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.
virtual int_type uflow() override
Reads and returns the next character from the associated serial port if one otherwise returns traits:...