libcyberradio  22.01.24
SerialPort.h
1 /***************************************************************************
2  * \file SerialPort.h
3  *
4  * \brief Serial port connection manager.
5  *
6  * \author DA
7  * \copyright Copyright (c) 2015-2021 CyberRadio Solutions, Inc.
8  *
9  */
10 
11 #ifndef INCLUDED_LIBCYBERRADIO_SERIALPORT_H_
12 #define INCLUDED_LIBCYBERRADIO_SERIALPORT_H_
13 
14 #include "LibCyberRadio/Common/Debuggable.h"
15 #include <string>
16 #include <termios.h>
17 
18 
22 namespace LibCyberRadio
23 {
24 
28  class SerialPort : public Debuggable
29  {
30  public:
45  SerialPort(
46  const std::string& device,
47  int baudrate = 115200,
48  char parity = 'N',
49  int databits = 8,
50  int stopbits = 1,
51  bool xonxoff = false,
52  bool rtscts = false,
53  bool debug = false
54  );
58  ~SerialPort();
63  SerialPort(const SerialPort& other);
69  SerialPort& operator=(const SerialPort& other);
75  bool open();
81  bool close();
85  std::string read();
91  bool write(const std::string& data);
97  int getBaudRate() const;
105  bool setBaudRate(int baudrate);
112  char getParity() const;
121  bool setParity(char parity);
127  int getDataBits() const;
135  bool setDataBits(int databits);
141  int getStopBits() const;
149  bool setStopBits(int stopbits);
156  bool usesXonXoffFlowControl() const;
164  bool enableXonXoffFlowControl(bool enabled);
171  bool usesRtsCtsFlowControl() const;
179  bool enableRtsCtsFlowControl(bool enabled);
184  std::string getLastError() const;
185 
186 
187  protected:
188  // Serial device name
189  std::string _device;
190  // Baud rate
191  int _baudrate;
192  // Parity setting
193  char _parity;
194  // Data bits
195  int _databits;
196  // Stop bits
197  int _stopbits;
198  // Use XON/XOFF software flow control
199  bool _useXonXoff;
200  // Use RTS/CTS hardware flow control
201  bool _useRtsCts;
202  // Use DTS/DTR hardware flow control
203  bool _useDtsDtr;
204  // File descriptor
205  int _fd;
206  // Last error message
207  std::string _lastError;
208  // Receive buffer
209  char* _recvBuf;
210  // Receive buffer size
211  int _recvBufSize;
212  // Terminal settings
213  struct termios _settings;
214  };
215 
216 } // namespace LibCyberRadio
217 
218 
219 
220 
221 #endif /* INCLUDED_LIBCYBERRADIO_SERIALPORT_H_ */
int getDataBits() const
Gets the current number of data bits.
Definition: SerialPort.cpp:481
int getStopBits() const
Gets the current number of stop bits.
Definition: SerialPort.cpp:537
bool setBaudRate(int baudrate)
Sets the current baud rate.
Definition: SerialPort.cpp:308
~SerialPort()
Destroys a SerialPort object.
Definition: SerialPort.cpp:53
bool setDataBits(int databits)
Sets the current number of data bits.
Definition: SerialPort.cpp:486
SerialPort(const std::string &device, int baudrate=115200, char parity='N', int databits=8, int stopbits=1, bool xonxoff=false, bool rtscts=false, bool debug=false)
Constructs a SerialPort object.
Definition: SerialPort.cpp:25
Class that manages communications with a serial port.
Definition: SerialPort.h:28
bool write(const std::string &data)
Writes data to the serial port.
Definition: SerialPort.cpp:271
char getParity() const
Gets the current parity setting.
Definition: SerialPort.cpp:432
Class that supports debug output.
Definition: Debuggable.h:38
bool setStopBits(int stopbits)
Sets the current number of stop bits.
Definition: SerialPort.cpp:542
bool open()
Open the serial port.
Definition: SerialPort.cpp:104
std::string getLastError() const
Gets the last error message.
Definition: SerialPort.cpp:659
bool enableXonXoffFlowControl(bool enabled)
Enables XON/XOFF (software) flow control on the serial port.
Definition: SerialPort.cpp:586
SerialPort & operator=(const SerialPort &other)
Assignment operator for SerialPort objects.
Definition: SerialPort.cpp:81
virtual int debug(const char *format,...)
Outputs debug information.
Definition: Debuggable.cpp:95
Defines functionality for LibCyberRadio applications.
Definition: App.h:23
bool usesRtsCtsFlowControl() const
Gets whether the serial port currently uses RTS/CTS (hardware) flow control.
Definition: SerialPort.cpp:620
bool setParity(char parity)
Sets the current parity setting.
Definition: SerialPort.cpp:437
bool close()
Close the serial port.
Definition: SerialPort.cpp:212
bool usesXonXoffFlowControl() const
Gets whether the serial port currently uses XON/XOFF (software) flow control.
Definition: SerialPort.cpp:581
bool enableRtsCtsFlowControl(bool enabled)
Enables RTS/CTS (hardware) flow control on the serial port.
Definition: SerialPort.cpp:625
std::string read()
Reads data from the serial port.
Definition: SerialPort.cpp:231
int getBaudRate() const
Gets the current baud rate.
Definition: SerialPort.cpp:303