LibSerial 1.0.0
LibSerial provides a convenient, object oriented approach to accessing serial ports on POSIX systems.
Loading...
Searching...
No Matches
SerialPort.h
1/******************************************************************************
2 * @file SerialPort.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 <ios>
39#include <memory>
40
44namespace LibSerial
45{
56 {
57 public:
58
62 explicit SerialPort() ;
63
76 explicit SerialPort(const std::string& fileName,
77 const BaudRate& baudRate = BaudRate::BAUD_DEFAULT,
78 const CharacterSize& characterSize = CharacterSize::CHAR_SIZE_DEFAULT,
79 const FlowControl& flowControlType = FlowControl::FLOW_CONTROL_DEFAULT,
80 const Parity& parityType = Parity::PARITY_DEFAULT,
81 const StopBits& stopBits = StopBits::STOP_BITS_DEFAULT) ;
82
87 virtual ~SerialPort() noexcept;
88
92 SerialPort(const SerialPort& otherSerialPort) = delete ;
93
97 SerialPort(SerialPort&& otherSerialPort) ;
98
102 SerialPort& operator=(const SerialPort& otherSerialPort) = delete ;
103
107 SerialPort& operator=(SerialPort&& otherSerialPort) ;
108
116 void Open(const std::string& fileName,
117 const std::ios_base::openmode& openMode = std::ios_base::in | std::ios_base::out) ;
118
123 void Close() ;
124
128 void DrainWriteBuffer() ;
129
133 void FlushInputBuffer() ;
134
138 void FlushOutputBuffer() ;
139
143 void FlushIOBuffers() ;
144
149 bool IsDataAvailable() ;
150
155 bool IsOpen() const ;
156
161
166 void SetBaudRate(const BaudRate& baudRate) ;
167
172 BaudRate GetBaudRate() const ;
173
178 void SetCharacterSize(const CharacterSize& characterSize) ;
179
184 CharacterSize GetCharacterSize() const ;
185
190 void SetFlowControl(const FlowControl& flowControlType) ;
191
196 FlowControl GetFlowControl() const ;
197
202 void SetParity(const Parity& parityType) ;
203
208 Parity GetParity() const ;
209
214 void SetStopBits(const StopBits& stopBits) ;
215
220 StopBits GetStopBits() const ;
221
227 void SetVMin(const short vmin) ;
228
235 short GetVMin() const ;
236
242 void SetVTime(const short vtime) ;
243
248 short GetVTime() const ;
249
255 void SetDTR(const bool dtrState = true) ;
256
261 bool GetDTR() const ;
262
268 void SetRTS(const bool rtsState = true) ;
269
274 bool GetRTS() const ;
275
280 bool GetCTS() ;
281
286 bool GetDSR() ;
287
292 int GetFileDescriptor() const ;
293
299
300#ifdef __linux__
306 std::vector<std::string> GetAvailableSerialPorts() const ;
307#endif
308
324 void Read(DataBuffer& dataBuffer,
325 size_t numberOfBytes = 0,
326 size_t msTimeout = 0) ;
327
343 void Read(std::string& dataString,
344 size_t numberOfBytes = 0,
345 size_t msTimeout = 0) ;
346
356 void ReadByte(char& charBuffer,
357 size_t msTimeout = 0) ;
358
368 void ReadByte(unsigned char& charBuffer,
369 size_t msTimeout = 0) ;
370
384 void ReadLine(std::string& dataString,
385 char lineTerminator = '\n',
386 size_t msTimeout = 0) ;
387
392 void Write(const DataBuffer& dataBuffer) ;
393
398 void Write(const std::string& dataString) ;
399
404 void WriteByte(char charbuffer) ;
405
410 void WriteByte(unsigned char charbuffer) ;
411
417 void SetSerialPortBlockingStatus(bool blockingStatus) ;
418
423 bool GetSerialPortBlockingStatus() const ;
424
432 void SetModemControlLine(int modemLine,
433 bool lineState) ;
434
442 bool GetModemControlLine(int modemLine) ;
443
444 protected:
445
446 private:
451 class Implementation;
452
456 std::unique_ptr<Implementation> mImpl;
457
458 } ; // class SerialPort
459
500 template<typename Fn, typename... Args>
501 typename std::result_of<Fn(Args...)>::type
502 call_with_retry(Fn func, Args... args)
503 {
504 using result_type = typename std::result_of<Fn(Args...)>::type ;
505 result_type result ;
506 do {
507 result = func(std::forward<Args>(args)...);
508 } while((result == -1) and (errno == EINTR)) ;
509 return result ;
510 }
511} // namespace LibSerial
SerialPort::Implementation is the SerialPort implementation class.
SerialPort allows an object oriented approach to serial port communication. A serial port object can ...
Definition SerialPort.h:56
void SetBaudRate(const BaudRate &baudRate)
Sets the baud rate for the serial port to the specified value.
void SetModemControlLine(int modemLine, bool lineState)
Set the specified modem control line to the specified value.
void SetParity(const Parity &parityType)
Sets 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)
Opens the serial port associated with the specified file name and the specified mode.
bool GetCTS()
Get the status of the CTS line.
bool GetSerialPortBlockingStatus() const
Gets the current state of the serial port blocking status.
void WriteByte(char charbuffer)
Writes a single byte to the serial port.
void FlushInputBuffer()
Flushes the serial port input buffer.
void SetFlowControl(const FlowControl &flowControlType)
Sets flow control for the serial port.
void SetDefaultSerialPortParameters()
Sets all serial port paramters to their default values.
SerialPort()
Default Constructor.
FlowControl GetFlowControl() const
Gets the current flow control setting.
CharacterSize GetCharacterSize() const
Gets the character size being used for serial communication.
void Write(const DataBuffer &dataBuffer)
Writes a DataBuffer to the serial port.
bool GetDTR() const
Gets the status of the DTR line.
void SetRTS(const bool rtsState=true)
Set the RTS line to the specified value.
void SetVMin(const short vmin)
Sets the minimum number of characters for non-canonical reads.
virtual ~SerialPort() noexcept
Default Destructor for a SerialPort object. Closes the serial port associated with mFileDescriptor if...
StopBits GetStopBits() const
Gets the number of stop bits currently being used by the serial.
bool GetRTS() const
Get the status of the RTS line.
void Close()
Closes the serial port. All settings of the serial port will be lost and no more I/O can be performed...
void SetCharacterSize(const CharacterSize &characterSize)
Sets the character size for the serial port.
bool IsDataAvailable()
Checks if data is available at the input of the serial port.
BaudRate GetBaudRate() const
Gets the current baud rate for the serial port.
void SetVTime(const short vtime)
Sets character buffer timeout for non-canonical reads in deciseconds.
void FlushIOBuffers()
Flushes the serial port input and output buffers.
void FlushOutputBuffer()
Flushes the serial port output buffer.
Parity GetParity() const
Gets the parity type for the serial port.
short GetVMin() const
Gets the VMIN value for the device, which represents the minimum number of characters for non-canonic...
bool GetModemControlLine(int modemLine)
Get the current state of the specified modem control line.
short GetVTime() const
Gets the current timeout value for non-canonical reads in deciseconds.
void Read(DataBuffer &dataBuffer, size_t numberOfBytes=0, size_t msTimeout=0)
Reads the specified number of bytes from the serial port. The method will timeout if no data is recei...
bool GetDSR()
Get the status of the DSR line.
void ReadByte(char &charBuffer, size_t msTimeout=0)
Reads a single byte from the serial port. If no data is available within the specified number of mill...
int GetNumberOfBytesAvailable()
Gets the number of bytes available in the read buffer.
int GetFileDescriptor() const
Gets the serial port file descriptor.
void SetDTR(const bool dtrState=true)
Sets the DTR line to the specified value.
void ReadLine(std::string &dataString, char lineTerminator='\n', size_t msTimeout=0)
Reads a line of characters from the serial port. The method will timeout if no data is received in th...
void DrainWriteBuffer()
Waits until the write buffer is drained and then returns.
bool IsOpen() const
Determines if the serial port is open for I/O.
void SetSerialPortBlockingStatus(bool blockingStatus)
Sets the current state of the serial port blocking status.
void SetStopBits(const StopBits &stopBits)
Sets the number of stop bits to be used with the serial port.