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
80 explicit SerialStreamBuf(const std::string& fileName,
81 const BaudRate& baudRate = BaudRate::BAUD_DEFAULT,
82 const CharacterSize& characterSize = CharacterSize::CHAR_SIZE_DEFAULT,
83 const FlowControl& flowControlType = FlowControl::FLOW_CONTROL_DEFAULT,
84 const Parity& parityType = Parity::PARITY_DEFAULT,
85 const StopBits& stopBits = StopBits::STOP_BITS_DEFAULT,
86 bool exclusive = true) ;
87
92 virtual ~SerialStreamBuf() ;
93
97 SerialStreamBuf(const SerialStreamBuf& otherSerialStreamBuf) = delete ;
98
102 SerialStreamBuf(const SerialStreamBuf&& otherSerialStreamBuf) = delete ;
103
107 SerialStreamBuf& operator=(const SerialStreamBuf& otherSerialStreamBuf) = delete ;
108
112 SerialStreamBuf& operator=(const SerialStreamBuf&& otherSerialStreamBuf) = delete ;
113
123 void Open(const std::string& fileName,
124 const std::ios_base::openmode& openMode = std::ios_base::in | std::ios_base::out,
125 bool exclusive = true) ;
126
131 void Close() ;
132
136 void DrainWriteBuffer() ;
137
141 void FlushInputBuffer() ;
142
146 void FlushOutputBuffer() ;
147
151 void FlushIOBuffers() ;
152
157 bool IsDataAvailable() ;
158
163 bool IsOpen() const ;
164
169
174 void SetBaudRate(const BaudRate& baudRate) ;
175
180 BaudRate GetBaudRate() const ;
181
186 void SetCharacterSize(const CharacterSize& characterSize) ;
187
192 CharacterSize GetCharacterSize() const ;
193
198 void SetFlowControl(const FlowControl& flowControlType) ;
199
204 FlowControl GetFlowControl() const ;
205
210 void SetParity(const Parity& parityType) ;
211
216 Parity GetParity() const ;
217
222 void SetStopBits(const StopBits& stopBits) ;
223
228 StopBits GetStopBits() const ;
229
235 void SetVMin(const short vmin) ;
236
243 short GetVMin() const ;
244
250 void SetVTime(const short vtime) ;
251
256 short GetVTime() const ;
257
263 void SetDTR(const bool dtrState = true) ;
264
269 bool GetDTR() const ;
270
276 void SetRTS(const bool rtsState = true) ;
277
282 bool GetRTS() const ;
283
288 bool GetCTS() ;
289
294 bool GetDSR() ;
295
300 int GetFileDescriptor() const ;
301
307
308#ifdef __linux__
314 std::vector<std::string> GetAvailableSerialPorts() const ;
315#endif
316
317 protected:
318
339 virtual std::streambuf* setbuf(char_type* character,
340 std::streamsize numberOfBytes) override ;
341
350 virtual std::streamsize xsputn(const char_type* character,
351 std::streamsize numberOfBytes) override ;
352
361 virtual std::streamsize xsgetn(char_type* character,
362 std::streamsize numberOfBytes) override ;
363
369 virtual int_type overflow(const int_type character) override ;
370
378 virtual int_type underflow() override ;
379
387 virtual int_type uflow() override ;
388
397 virtual int_type pbackfail(const int_type character) override ;
398
413 virtual std::streamsize showmanyc() override ;
414
415 private:
416
421 class Implementation;
422
426 std::unique_ptr<Implementation> mImpl;
427
428 } ; // class SerialStreamBuf
429
430} // 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.
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 Open(const std::string &fileName, const std::ios_base::openmode &openMode=std::ios_base::in|std::ios_base::out, bool exclusive=true)
Opens the serial port associated with the specified file name and the specified mode.
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:...