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
22namespace LibCyberRadio
23{
24
28 class SerialPort : public Debuggable
29 {
30 public:
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 );
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_ */
virtual int debug(const char *format,...)
Outputs debug information.
Debuggable(bool debug=false, const std::string &debug_name="", FILE *debug_fp=DEBUG_FP, const std::string &debug_timefmt=DEBUG_TIME_FMT)
Constructs a Debuggable object.
std::string read()
Reads data from the serial port.
SerialPort & operator=(const SerialPort &other)
Assignment operator for SerialPort objects.
~SerialPort()
Destroys a SerialPort object.
bool usesXonXoffFlowControl() const
Gets whether the serial port currently uses XON/XOFF (software) flow control.
std::string getLastError() const
Gets the last error message.
bool close()
Close the serial port.
int getStopBits() const
Gets the current number of stop bits.
bool write(const std::string &data)
Writes data to the serial port.
bool setStopBits(int stopbits)
Sets the current number of stop bits.
bool setDataBits(int databits)
Sets the current number of data bits.
bool enableRtsCtsFlowControl(bool enabled)
Enables RTS/CTS (hardware) flow control on the serial port.
int getBaudRate() const
Gets the current baud rate.
bool enableXonXoffFlowControl(bool enabled)
Enables XON/XOFF (software) flow control on the serial port.
char getParity() const
Gets the current parity setting.
int getDataBits() const
Gets the current number of data bits.
bool setBaudRate(int baudrate)
Sets the current baud rate.
bool setParity(char parity)
Sets the current parity setting.
bool open()
Open the serial port.
bool usesRtsCtsFlowControl() const
Gets whether the serial port currently uses RTS/CTS (hardware) flow control.
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.
Defines functionality for LibCyberRadio applications.
Definition App.h:24