Skip to content

Commit 0528cdd

Browse files
committed
Merge branch 'develop'
2 parents 7ceed20 + 18eb907 commit 0528cdd

File tree

8 files changed

+385
-112
lines changed

8 files changed

+385
-112
lines changed

.gitignore

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@ cmake-build-debug/
44
.vscode/
55

66
# Sphinx build dir
7-
docs/_build/
7+
docs/_build/
8+
9+
# Build artifacts
10+
a.out

.vscode/c_cpp_properties.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@
6060
},
6161
"compilerPath": "/usr/bin/gcc",
6262
"cStandard": "c11",
63-
"cppStandard": "c++17"
63+
"cppStandard": "c++17",
64+
"configurationProvider": "ms-vscode.cmake-tools"
6465
},
6566
{
6667
"name": "Win32",

CHANGELOG.md

+11
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
77

88
## [Unreleased]
99

10+
## [v2.1.0] - 2020-11-08
11+
12+
### Added
13+
- Support for custom baud rates.
14+
- Support for all standard UNIX baud rates.
15+
- Improved Doxygen documentation.
16+
- Improved README.md documentation.
17+
18+
### Removed
19+
- Dependencies from the README, they weren't that useful and were not accurate anyway.
20+
1021
## [v2.0.3] - 2020-10-13
1122

1223
### Added

README.md

+14-37
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
# CppLinuxSerial
22

3-
Serial port library written in C++
3+
Linux serial port library written in C++.
44

55
[![Build Status](https://travis-ci.org/gbmhunter/CppLinuxSerial.svg?branch=master)](https://travis-ci.org/gbmhunter/CppLinuxSerial)
66

77
## Description
88

99
Library for communicating with COM ports on a Linux system.
1010

11-
Uses fstream to the file I/O.
11+
* Simple API
12+
* Supports custom baud rates
13+
* cmake based build system
1214

1315
## Installation
1416

@@ -66,18 +68,20 @@ using namespace mn::CppLinuxSerial;
6668

6769
int main() {
6870
// Create serial port object and open serial port
69-
SerialPort serialPort0("/dev/ttyUSB0", BaudRate::B_57600);
70-
serialPort0.Open();
71+
SerialPort serialPort("/dev/ttyUSB0", BaudRate::B_57600);
72+
// Use SerialPort serialPort("/dev/ttyACM0", 13000); instead if you want to provide a custom baud rate
73+
serialPort.SetTimeout(-1); // Block when reading until any data is received
74+
serialPort.Open();
7175

7276
// Write some ASCII datae
73-
serialPort0.Write("Hello");
77+
serialPort.Write("Hello");
7478

75-
// Read some data back
79+
// Read some data back (will block until at least 1 byte is received due to the SetTimeout(-1) call above)
7680
std::string readData;
77-
serialPort0.Read(readData);
81+
serialPort.Read(readData);
7882

7983
// Close the serial port
80-
serialPort0.Close();
84+
serialPort.Close();
8185
}
8286
```
8387
@@ -87,42 +91,15 @@ If the above code was in a file called `main.cpp` and you had installed `CppLinu
8791
g++ main.cpp -lCppLinuxSerial
8892
```
8993

90-
For more examples, see the `.cpp` files in `test/unit/`.
91-
92-
## Dependencies
93-
94-
The following table lists all of the libraries dependencies.
95-
96-
<table>
97-
<thead>
98-
<tr>
99-
<td>Dependency</td>
100-
<td>Comments</td>
101-
</tr>
102-
</thead>
103-
<tbody>
104-
<tr>
105-
<td>C++14</td>
106-
<td>C++14 used for strongly typed enums, `std::chrono` and literals.</td>
107-
</tr>
108-
<tr>
109-
<td>stdio.h</td>
110-
<td>snprintf()</td>
111-
</tr>
112-
<tr>
113-
<td>stty</td>
114-
<td>Used in unit tests to verify the serial port is configured correctly.</td>
115-
</tr>
116-
</tbody>
117-
</table>
94+
For more examples, see the files in `test/`.
11895

11996
## Issues
12097

12198
See GitHub Issues.
12299

123100
## FAQ
124101

125-
1. My code stalls when calling functions like `SerialPort::Read()`. This is probably because the library is set up to do a blocking read, and not enough characters have been received to allow `SerialPort::Read()` to return. Use `SerialPort::SetNumCharsToWait()` to determine how many characters to wait for before returning (set to 0 for non-blocking mode).
102+
1. My code stalls when calling functions like `SerialPort::Read()`. This is probably because the library is set up to do a blocking read, and not enough characters have been received to allow `SerialPort::Read()` to return. Call `SerialPort::SetTimeout(0)` before the serial port is open to set a non-blocking mode.
126103

127104
## Changelog
128105

include/CppLinuxSerial/SerialPort.hpp

+56-8
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,56 @@
1515
#include <string>
1616
#include <fstream> // For file I/O (reading/writing to COM port)
1717
#include <sstream>
18-
#include <termios.h> // POSIX terminal control definitions (struct termios)
18+
// #include <termios.h> // POSIX terminal control definitions (struct termios)
19+
// #include <asm/termios.h> // Terminal control definitions (struct termios)
1920
#include <vector>
21+
#include <asm/ioctls.h>
22+
#include <asm/termbits.h>
2023

2124
// User headers
2225
#include "Exception.hpp"
2326

2427
namespace mn {
2528
namespace CppLinuxSerial {
2629

30+
/// \brief Represents the baud rate "types" that can be used with the serial port. STANDARD represents all
31+
/// the standard baud rates as provided by UNIX, CUSTOM represents a baud rate defined by an arbitray integer.
32+
enum class BaudRateType {
33+
STANDARD,
34+
CUSTOM,
35+
};
36+
2737
/// \brief Strongly-typed enumeration of baud rates for use with the SerialPort class
38+
/// \details Specifies all the same baud rates as UNIX, as well as B_CUSTOM to specify your
39+
/// own. See https://linux.die.net/man/3/cfsetispeed for list of supported UNIX baud rates.
2840
enum class BaudRate {
41+
B_0,
42+
B_50,
43+
B_75,
44+
B_110,
45+
B_134,
46+
B_150,
47+
B_200,
48+
B_300,
49+
B_600,
50+
B_1200,
51+
B_1800,
52+
B_2400,
53+
B_4800,
2954
B_9600,
55+
B_19200,
3056
B_38400,
3157
B_57600,
3258
B_115200,
33-
CUSTOM
59+
B_230400,
60+
B_460800,
61+
B_CUSTOM, // Placeholder
3462
};
3563

64+
/// \brief Represents the state of the serial port.
3665
enum class State {
3766
CLOSED,
38-
OPEN
67+
OPEN,
3968
};
4069

4170
/// \brief SerialPort object is used to perform rx/tx serial communication.
@@ -48,15 +77,22 @@ namespace mn {
4877
/// \brief Constructor that sets up serial port with the basic (required) parameters.
4978
SerialPort(const std::string &device, BaudRate baudRate);
5079

51-
//! @brief Destructor. Closes serial port if still open.
80+
/// \brief Constructor that sets up serial port with the basic (required) parameters.
81+
SerialPort(const std::string &device, speed_t baudRate);
82+
83+
/// \brief Destructor. Closes serial port if still open.
5284
virtual ~SerialPort();
5385

5486
/// \brief Sets the device to use for serial port communications.
5587
/// \details Method can be called when serial port is in any state.
5688
void SetDevice(const std::string &device);
5789

90+
/// \brief Allows the user to set a standard baud rate.
5891
void SetBaudRate(BaudRate baudRate);
5992

93+
/// \brief Allows the user to set a custom baud rate.
94+
void SetBaudRate(speed_t baudRate);
95+
6096
/// \brief Sets the read timeout (in milliseconds)/blocking mode.
6197
/// \details Only call when state != OPEN. This method manupulates VMIN and VTIME.
6298
/// \param timeout_ms Set to -1 to infinite timeout, 0 to return immediately with any data (non
@@ -92,22 +128,34 @@ namespace mn {
92128
private:
93129

94130
/// \brief Returns a populated termios structure for the passed in file descriptor.
95-
termios GetTermios();
131+
// termios GetTermios();
96132

97133
/// \brief Configures the tty device as a serial port.
98134
/// \warning Device must be open (valid file descriptor) when this is called.
99135
void ConfigureTermios();
100136

101-
void SetTermios(termios myTermios);
137+
// void SetTermios(termios myTermios);
138+
139+
/// \brief Returns a populated termios2 structure for the serial port pointed to by the file descriptor.
140+
termios2 GetTermios2();
141+
142+
/// \brief Assigns the provided tty settings to the serial port pointed to by the file descriptor.
143+
void SetTermios2(termios2 tty);
102144

103145
/// \brief Keeps track of the serial port's state.
104146
State state_;
105147

106148
/// \brief The file path to the serial port device (e.g. "/dev/ttyUSB0").
107149
std::string device_;
108150

109-
/// \brief The current baud rate.
110-
BaudRate baudRate_;
151+
/// \brief The type of baud rate that the user has specified.
152+
BaudRateType baudRateType_;
153+
154+
/// \brief The current baud rate if baudRateType_ == STANDARD.
155+
BaudRate baudRateStandard_;
156+
157+
/// \brief The current baud rate if baudRateType_ == CUSTOM.
158+
speed_t baudRateCustom_;
111159

112160
/// \brief The file descriptor for the open file. This gets written to when Open() is called.
113161
int fileDesc_;

0 commit comments

Comments
 (0)