SerialPort.hpp
4.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
///
/// \file SerialPort.hpp
/// \author Geoffrey Hunter <gbmhunter@gmail.com> ()
/// \created 2014-01-07
/// \last-modified 2017-11-23
/// \brief The main serial port class.
/// \details
/// See README.rst in repo root dir for more info.
// Header guard
#ifndef SERIAL_PORT_SERIAL_PORT_H
#define SERIAL_PORT_SERIAL_PORT_H
// System headers
#include <string>
#include <fstream> // For file I/O (reading/writing to COM port)
#include <sstream>
#include <termios.h> // POSIX terminal control definitions (struct termios)
#include <vector>
// User headers
namespace mn {
namespace CppLinuxSerial {
/// \brief Strongly-typed enumeration of baud rates for use with the SerialPort class
enum class BaudRate {
B_9600,
B_38400,
B_57600,
B_115200,
CUSTOM
};
enum class State {
CLOSED,
OPEN
};
/// \brief SerialPort object is used to perform rx/tx serial communication.
class SerialPort {
public:
/// \brief Default constructor. You must specify at least the device before calling Open().
SerialPort();
/// \brief Constructor that sets up serial port with the basic (required) parameters.
SerialPort(const std::string &device, BaudRate baudRate);
//! @brief Destructor. Closes serial port if still open.
virtual ~SerialPort();
/// \brief Sets the device to use for serial port communications.
/// \details Method can be called when serial port is in any state.
void SetDevice(const std::string &device);
void SetBaudRate(BaudRate baudRate);
/// \brief Sets the read timeout (in milliseconds)/blocking mode.
/// \details Only call when state != OPEN. This method manupulates VMIN and VTIME.
/// \param timeout_ms Set to -1 to infinite timeout, 0 to return immediately with any data (non
/// blocking, or >0 to wait for data for a specified number of milliseconds). Timeout will
/// be rounded to the nearest 100ms (a Linux API restriction). Maximum value limited to
/// 25500ms (another Linux API restriction).
void SetTimeout(int32_t timeout_ms);
/// \brief Enables/disables echo.
/// \param value Pass in true to enable echo, false to disable echo.
void SetEcho(bool value);
/// \brief Opens the COM port for use.
/// \throws CppLinuxSerial::Exception if device cannot be opened.
/// \note Must call this before you can configure the COM port.
void Open();
/// \brief Closes the COM port.
void Close();
/// \brief Sends a message over the com port.
/// \param data The data that will be written to the COM port.
/// \throws CppLinuxSerial::Exception if state != OPEN.
void Write(const std::string& data);
/// \brief Use to read from the COM port.
/// \param data The object the read characters from the COM port will be saved to.
/// \param wait_ms The amount of time to wait for data. Set to 0 for non-blocking mode. Set to -1
/// to wait indefinitely for new data.
/// \throws CppLinuxSerial::Exception if state != OPEN.
void Read(std::string& data);
private:
/// \brief Returns a populated termios structure for the passed in file descriptor.
termios GetTermios();
/// \brief Configures the tty device as a serial port.
/// \warning Device must be open (valid file descriptor) when this is called.
void ConfigureTermios();
void SetTermios(termios myTermios);
/// \brief Keeps track of the serial port's state.
State state_;
/// \brief The file path to the serial port device (e.g. "/dev/ttyUSB0").
std::string device_;
/// \brief The current baud rate.
BaudRate baudRate_;
/// \brief The file descriptor for the open file. This gets written to when Open() is called.
int fileDesc_;
bool echo_;
int32_t timeout_ms_;
std::vector<char> readBuffer_;
unsigned char readBufferSize_B_;
static constexpr BaudRate defaultBaudRate_ = BaudRate::B_57600;
static constexpr int32_t defaultTimeout_ms_ = -1;
static constexpr unsigned char defaultReadBufferSize_B_ = 255;
};
} // namespace CppLinuxSerial
} // namespace mn
#endif // #ifndef SERIAL_PORT_SERIAL_PORT_H