SerialPort.hpp
2.86 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
//!
//! @file SerialPort.hpp
//! @author Geoffrey Hunter <gbmhunter@gmail.com> ()
//! @created 2014/01/07
//! @last-modified 2014/05/14
//! @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 <fstream> // For file I/O (reading/writing to COM port)
#include <sstream>
#include <termios.h> // POSIX terminal control definitions (struct termios)
// User headers
#include "lib/SmartPrint/include/Sp.hpp"
namespace SerialPort
{
//! @brief Strongly-typed enumeration of baud rates for use with the SerialPort class
enum class BaudRates
{
none,
b9600,
b57600
};
//! @brief SerialPort object is used to perform rx/tx serial communication.
class SerialPort
{
public:
//! @brief Constructor
SerialPort();
//! @brief Destructor
virtual ~SerialPort();
//! @brief Sets the file path to use for communications. The file path must be set before Open() is called, otherwise Open() will return an error.
void SetFilePath(std::string filePath);
void SetBaudRate(BaudRates baudRate);
//! @brief Controls what happens when Read() is called.
//! @param numOfCharToWait Minimum number of characters to wait for before returning. Set to 0 for non-blocking mode.
void SetNumCharsToWait(uint32_t numCharsToWait);
//! @brief Opens the COM port for use.
//! @throws {std::runtime_error} if filename has not been set.
//! {std::system_error} if system open() operation fails.
//! @note Must call this before you can configure the COM port.
void Open();
//! @brief Sets all settings for the com port to common defaults.
void SetEverythingToCommonDefaults();
//! @brief Closes the COM port.
void Close();
//! @brief Sends a message over the com port.
//! @param str Reference to an string containing the characters to write to the COM port.
//! @throws {std::runtime_error} if filename has not been set.
//! {std::system_error} if system write() operation fails.
void Write(std::string* str);
//! @brief Use to read from the COM port.
//! @param str Reference to a string that the read characters from the COM port will be saved to.
//! @throws {std::runtime_error} if filename has not been set.
//! {std::system_error} if system read() operation fails.
void Read(std::string* str);
private:
std::string filePath;
BaudRates baudRate;
//! @brief The file descriptor for the open file. This gets written to when Open() is called.
int fileDesc;
//! @brief Object for printing debug and error messages with
SmartPrint::Sp* sp;
//! @brief Returns a populated termios structure for the passed in file descriptor.
termios GetTermios();
void SetTermios(termios myTermios);
};
} // namespace SerialPort
#endif // #ifndef SERIAL_PORT_SERIAL_PORT_H