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
100 explicit SerialStream(const std::string& fileName,
101 const BaudRate& baudRate = BaudRate::BAUD_DEFAULT,
102 const CharacterSize& characterSize = CharacterSize::CHAR_SIZE_DEFAULT,
103 const FlowControl& flowControlType = FlowControl::FLOW_CONTROL_DEFAULT,
104 const Parity& parityType = Parity::PARITY_DEFAULT,
105 const StopBits& stopBits = StopBits::STOP_BITS_DEFAULT) ;
106
113 virtual ~SerialStream() ;
114
119 SerialStream(const SerialStream& otherSerialStream) = delete;
120
124 SerialStream(const SerialStream&& otherSerialStream) = delete;
125
130 SerialStream& operator=(const SerialStream& otherSerialStream) = delete;
131
135 SerialStream& operator=(const SerialStream&& otherSerialStream) = delete;
136
144 void Open(const std::string& fileName,
145 const std::ios_base::openmode& openMode = std::ios_base::in | std::ios_base::out) ;
146
151 void Close() ;
152
156 void DrainWriteBuffer() ;
157
161 void FlushInputBuffer() ;
162
166 void FlushOutputBuffer() ;
167
171 void FlushIOBuffers() ;
172
177 bool IsDataAvailable() ;
178
183 bool IsOpen() ;
184
189 void SetBaudRate(const BaudRate& baudRate) ;
190
195 BaudRate GetBaudRate() ;
196
201 void SetCharacterSize(const CharacterSize& characterSize) ;
202
207 CharacterSize GetCharacterSize() ;
208
213 void SetFlowControl(const FlowControl& flowControlType) ;
214
219 FlowControl GetFlowControl() ;
220
225 void SetParity(const Parity& parityType) ;
226
231 Parity GetParity() ;
232
237 void SetStopBits(const StopBits& stopBits) ;
238
243 StopBits GetStopBits() ;
244
250 void SetVMin(const short vmin) ;
251
258 short GetVMin() ;
259
265 void SetVTime(const short vtime) ;
266
271 short GetVTime() ;
272
278 void SetDTR(const bool dtrState = true) ;
279
284 bool GetDTR() ;
285
291 void SetRTS(const bool rtsState = true) ;
292
297 bool GetRTS() ;
298
303 bool GetCTS() ;
304
309 bool GetDSR() ;
310
315 int GetFileDescriptor() ;
316
322
330 std::vector<std::string> GetAvailableSerialPorts() ;
331
332
333 protected:
334
335 private:
340 std::unique_ptr<SerialStreamBuf> mIOBuffer {nullptr} ;
341
342 } ; // class SerialStream
343
344} // 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.
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.
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.