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
77 explicit SerialPort(const std::string& fileName,
78 const BaudRate& baudRate = BaudRate::BAUD_DEFAULT,
79 const CharacterSize& characterSize = CharacterSize::CHAR_SIZE_DEFAULT,
80 const FlowControl& flowControlType = FlowControl::FLOW_CONTROL_DEFAULT,
81 const Parity& parityType = Parity::PARITY_DEFAULT,
82 const StopBits& stopBits = StopBits::STOP_BITS_DEFAULT,
83 bool exclusive = true) ;
84
89 virtual ~SerialPort() noexcept;
90
94 SerialPort(const SerialPort& otherSerialPort) = delete ;
95
99 SerialPort(SerialPort&& otherSerialPort) ;
100
104 SerialPort& operator=(const SerialPort& otherSerialPort) = delete ;
105
109 SerialPort& operator=(SerialPort&& otherSerialPort) ;
110
119 void Open(const std::string& fileName,
120 const std::ios_base::openmode& openMode = std::ios_base::in | std::ios_base::out,
121 bool exclusive = true) ;
122
127 void Close() ;
128
132 void DrainWriteBuffer() ;
133
137 void FlushInputBuffer() ;
138
142 void FlushOutputBuffer() ;
143
147 void FlushIOBuffers() ;
148
153 bool IsDataAvailable() ;
154
159 bool IsOpen() const ;
160
165
170 void SetBaudRate(const BaudRate& baudRate) ;
171
176 BaudRate GetBaudRate() const ;
177
182 void SetCharacterSize(const CharacterSize& characterSize) ;
183
188 CharacterSize GetCharacterSize() const ;
189
194 void SetFlowControl(const FlowControl& flowControlType) ;
195
200 FlowControl GetFlowControl() const ;
201
206 void SetParity(const Parity& parityType) ;
207
212 Parity GetParity() const ;
213
218 void SetStopBits(const StopBits& stopBits) ;
219
224 StopBits GetStopBits() const ;
225
231 void SetVMin(const short vmin) ;
232
239 short GetVMin() const ;
240
246 void SetVTime(const short vtime) ;
247
252 short GetVTime() const ;
253
259 void SetDTR(const bool dtrState = true) ;
260
265 bool GetDTR() const ;
266
272 void SetRTS(const bool rtsState = true) ;
273
278 bool GetRTS() const ;
279
284 bool GetCTS() ;
285
290 bool GetDSR() ;
291
296 int GetFileDescriptor() const ;
297
303
304#ifdef __linux__
310 std::vector<std::string> GetAvailableSerialPorts() const ;
311#endif
312
328 void Read(DataBuffer& dataBuffer,
329 size_t numberOfBytes = 0,
330 size_t msTimeout = 0) ;
331
347 void Read(std::string& dataString,
348 size_t numberOfBytes = 0,
349 size_t msTimeout = 0) ;
350
360 void ReadByte(char& charBuffer,
361 size_t msTimeout = 0) ;
362
372 void ReadByte(unsigned char& charBuffer,
373 size_t msTimeout = 0) ;
374
388 void ReadLine(std::string& dataString,
389 char lineTerminator = '\n',
390 size_t msTimeout = 0) ;
391
396 void Write(const DataBuffer& dataBuffer) ;
397
402 void Write(const std::string& dataString) ;
403
408 void WriteByte(char charbuffer) ;
409
414 void WriteByte(unsigned char charbuffer) ;
415
421 void SetSerialPortBlockingStatus(bool blockingStatus) ;
422
427 bool GetSerialPortBlockingStatus() const ;
428
436 void SetModemControlLine(int modemLine,
437 bool lineState) ;
438
446 bool GetModemControlLine(int modemLine) ;
447
448 protected:
449
450 private:
455 class Implementation;
456
460 std::unique_ptr<Implementation> mImpl;
461
462 } ; // class SerialPort
463
504 template<typename Fn, typename... Args>
505 typename std::result_of<Fn(Args...)>::type
506 call_with_retry(Fn func, Args... args)
507 {
508 using result_type = typename std::result_of<Fn(Args...)>::type ;
509 result_type result ;
510 do {
511 result = func(std::forward<Args>(args)...);
512 } while((result == -1) and (errno == EINTR)) ;
513 return result ;
514 }
515} // 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.
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.
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.
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.