LibSerial 1.0.0
LibSerial provides a convenient, object oriented approach to accessing serial ports on POSIX systems.
Loading...
Searching...
No Matches
SerialStream.h
1/******************************************************************************
2 * @file SerialStream.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#include <libserial/SerialStreamBuf.h>
38
39#include <istream>
40#include <memory>
41
42namespace LibSerial
43{
73 class SerialStream : public std::iostream
74 {
75 public:
76
83 explicit SerialStream() ;
84
101 explicit SerialStream(const std::string& fileName,
102 const BaudRate& baudRate = BaudRate::BAUD_DEFAULT,
103 const CharacterSize& characterSize = CharacterSize::CHAR_SIZE_DEFAULT,
104 const FlowControl& flowControlType = FlowControl::FLOW_CONTROL_DEFAULT,
105 const Parity& parityType = Parity::PARITY_DEFAULT,
106 const StopBits& stopBits = StopBits::STOP_BITS_DEFAULT,
107 bool exclusive = true) ;
108
115 virtual ~SerialStream() ;
116
121 SerialStream(const SerialStream& otherSerialStream) = delete;
122
126 SerialStream(const SerialStream&& otherSerialStream) = delete;
127
132 SerialStream& operator=(const SerialStream& otherSerialStream) = delete;
133
137 SerialStream& operator=(const SerialStream&& otherSerialStream) = delete;
138
148 void Open(const std::string& fileName,
149 const std::ios_base::openmode& openMode = std::ios_base::in | std::ios_base::out,
150 bool exclusive = true) ;
151
156 void Close() ;
157
161 void DrainWriteBuffer() ;
162
166 void FlushInputBuffer() ;
167
171 void FlushOutputBuffer() ;
172
176 void FlushIOBuffers() ;
177
182 bool IsDataAvailable() ;
183
188 bool IsOpen() ;
189
194 void SetBaudRate(const BaudRate& baudRate) ;
195
200 BaudRate GetBaudRate() ;
201
206 void SetCharacterSize(const CharacterSize& characterSize) ;
207
212 CharacterSize GetCharacterSize() ;
213
218 void SetFlowControl(const FlowControl& flowControlType) ;
219
224 FlowControl GetFlowControl() ;
225
230 void SetParity(const Parity& parityType) ;
231
236 Parity GetParity() ;
237
242 void SetStopBits(const StopBits& stopBits) ;
243
248 StopBits GetStopBits() ;
249
255 void SetVMin(const short vmin) ;
256
263 short GetVMin() ;
264
270 void SetVTime(const short vtime) ;
271
276 short GetVTime() ;
277
283 void SetDTR(const bool dtrState = true) ;
284
289 bool GetDTR() ;
290
296 void SetRTS(const bool rtsState = true) ;
297
302 bool GetRTS() ;
303
308 bool GetCTS() ;
309
314 bool GetDSR() ;
315
320 int GetFileDescriptor() ;
321
327
335 std::vector<std::string> GetAvailableSerialPorts() ;
336
337
338 protected:
339
340 private:
345 std::unique_ptr<SerialStreamBuf> mIOBuffer {nullptr} ;
346
347 } ; // class SerialStream
348
349} // namespace LibSerial
SerialStream is a stream class for accessing serial ports on POSIX operating systems....
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...
SerialStream & operator=(const SerialStream &otherSerialStream)=delete
Prevents copying of objects of this class by declaring the assignment operator private....
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.
SerialStream(const SerialStream &otherSerialStream)=delete
Prevents copying of objects of this class by declaring the copy constructor private....
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.
SerialStream & operator=(const SerialStream &&otherSerialStream)=delete
Move assignment is not allowed.
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.
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.
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.
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.
SerialStream(const SerialStream &&otherSerialStream)=delete
Move construction is disallowed.
bool GetRTS()
Get the status of the RTS line.
void SetParity(const Parity &parityType)
Sets the parity type for the serial port.