LibSerial 1.0.0
LibSerial provides a convenient, object oriented approach to accessing serial ports on POSIX systems.
Loading...
Searching...
No Matches
SerialPortConstants.h
1/******************************************************************************
2 * @file SerialPortConstants.h *
3 * @copyright (C) 2004-2018 LibSerial Development Team. All rights reserved. *
4 * crayzeewulf@gmail.com *
5 * *
6 * Redistribution and use in source and binary forms, with or without *
7 * modification, are permitted provided that the following conditions *
8 * are met: *
9 * *
10 * 1. Redistributions of source code must retain the above copyright *
11 * notice, this list of conditions and the following disclaimer. *
12 * 2. Redistributions in binary form must reproduce the above copyright *
13 * notice, this list of conditions and the following disclaimer in *
14 * the documentation and/or other materials provided with the *
15 * distribution. *
16 * 3. Neither the name PX4 nor the names of its contributors may be *
17 * used to endorse or promote products derived from this software *
18 * without specific prior written permission. *
19 * *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT *
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS *
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE *
24 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, *
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, *
26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS *
27 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED *
28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT *
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN *
30 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE *
31 * POSSIBILITY OF SUCH DAMAGE. *
32 *****************************************************************************/
33
34#pragma once
35
36#include <cstdint>
37#include <limits>
38#include <stdexcept>
39#include <string>
40#include <termios.h>
41#include <vector>
42
43namespace LibSerial
44{
48 const std::string ERR_MSG_INVALID_BAUD_RATE = "Invalid baud rate.";
49 const std::string ERR_MSG_INVALID_CHARACTER_SIZE = "Invalid character size.";
50 const std::string ERR_MSG_INVALID_FLOW_CONTROL = "Invalid flow control.";
51 const std::string ERR_MSG_INVALID_PARITY = "Invalid parity setting.";
52 const std::string ERR_MSG_INVALID_STOP_BITS = "Invalid number of stop bits.";
53 const std::string ERR_MSG_READ_TIMEOUT = "Read timeout";
54 const std::string ERR_MSG_PORT_ALREADY_OPEN = "Serial port already open.";
55 const std::string ERR_MSG_PORT_NOT_OPEN = "Serial port not open.";
56 const std::string ERR_MSG_INVALID_MODEM_LINE = "Invalid modem line." ;
57
61 constexpr int MICROSECONDS_PER_MS = 1000 ;
62 constexpr int MILLISECONDS_PER_SEC = 1000 ;
63 constexpr int MICROSECONDS_PER_SEC = 1000000 ;
64
68 constexpr int BITS_PER_BYTE = 8 ;
69
73 constexpr short VMIN_DEFAULT = 1 ;
74
78 constexpr short VTIME_DEFAULT = 0 ;
79
84 constexpr char CTRL_Q = 0x11 ;
85
90 constexpr char CTRL_S = 0x13 ;
91
95 using DataBuffer = std::vector<uint8_t> ;
96
97
125 class NotOpen : public std::logic_error
126 {
127 public:
131 explicit NotOpen(const std::string& whatArg [[maybe_unused]])
132 : logic_error(whatArg)
133 {
134 }
135 } ;
136
140 class AlreadyOpen : public std::logic_error
141 {
142 public:
146 explicit AlreadyOpen(const std::string& whatArg [[maybe_unused]])
147 : logic_error(whatArg)
148 {
149 }
150 } ;
151
155 class OpenFailed : public std::runtime_error
156 {
157 public:
161 explicit OpenFailed(const std::string& whatArg [[maybe_unused]])
162 : runtime_error(whatArg)
163 {
164 }
165 } ;
166
171 class ReadTimeout : public std::runtime_error
172 {
173 public:
178 explicit ReadTimeout(const std::string& whatArg [[maybe_unused]])
179 : runtime_error(whatArg)
180 {
181 }
182 } ;
183
191 enum class BaudRate : speed_t
192 {
193 BAUD_50 = B50,
194 BAUD_75 = B75,
195 BAUD_110 = B110,
196 BAUD_134 = B134,
197 BAUD_150 = B150,
198 BAUD_200 = B200,
199 BAUD_300 = B300,
200 BAUD_600 = B600,
201 BAUD_1200 = B1200,
202 BAUD_1800 = B1800,
203 BAUD_2400 = B2400,
204 BAUD_4800 = B4800,
205 BAUD_9600 = B9600,
206 BAUD_19200 = B19200,
207 BAUD_38400 = B38400,
208 BAUD_57600 = B57600,
209 BAUD_115200 = B115200,
210 BAUD_230400 = B230400,
211
212// @note: >B230400 are defined in Linux but not other POSIX systems, (e.g. Mac OS X).
213#ifdef __linux__
214 BAUD_460800 = B460800,
215 BAUD_500000 = B500000,
216 BAUD_576000 = B576000,
217 BAUD_921600 = B921600,
218 BAUD_1000000 = B1000000,
219 BAUD_1152000 = B1152000,
220 BAUD_1500000 = B1500000,
221#if __MAX_BAUD > B2000000
222 BAUD_2000000 = B2000000,
223 BAUD_2500000 = B2500000,
224 BAUD_3000000 = B3000000,
225 BAUD_3500000 = B3500000,
226 BAUD_4000000 = B4000000,
227#endif // __MAX_BAUD
228#endif // __linux__
229 BAUD_DEFAULT = BAUD_115200,
230 BAUD_INVALID = std::numeric_limits<speed_t>::max()
231 } ;
232
236 enum class CharacterSize : tcflag_t
237 {
238 CHAR_SIZE_5 = CS5, // !< 5 bit characters.
239 CHAR_SIZE_6 = CS6, // !< 6 bit characters.
240 CHAR_SIZE_7 = CS7, // !< 7 bit characters.
241 CHAR_SIZE_8 = CS8, // !< 8 bit characters.
242 CHAR_SIZE_DEFAULT = CS8, // !< 8 bit characters.
243 CHAR_SIZE_INVALID = std::numeric_limits<tcflag_t>::max()
244 } ;
245
249 enum class FlowControl : tcflag_t
250 {
251 FLOW_CONTROL_HARDWARE,
252 FLOW_CONTROL_SOFTWARE,
253 FLOW_CONTROL_NONE,
254 FLOW_CONTROL_DEFAULT = FLOW_CONTROL_NONE,
255 FLOW_CONTROL_INVALID = std::numeric_limits<tcflag_t>::max()
256 } ;
257
261 enum class Parity : tcflag_t
262 {
263 PARITY_EVEN, // !< Even parity.
264 PARITY_ODD, // !< Odd parity.
265 PARITY_NONE, // !< No parity i.e. parity checking disabled.
266 PARITY_DEFAULT = PARITY_NONE, // !< No parity i.e. parity checking disabled.
267 PARITY_INVALID = std::numeric_limits<tcflag_t>::max() // !< Invalid parity value.
268 } ;
269
273 enum class StopBits : tcflag_t
274 {
275 STOP_BITS_1, // !< 1 stop bit.
276 STOP_BITS_2, // !< 2 stop bits.
277 STOP_BITS_DEFAULT = STOP_BITS_1, // !< 1 stop bit.
278 STOP_BITS_INVALID = std::numeric_limits<tcflag_t>::max()
279 } ;
280
281} // namespace LibSerial
Exception error thrown when the serial port is already open.
AlreadyOpen(const std::string &whatArg)
Exception error thrown when the serial port is already open.
Exception error thrown when the serial port is not open.
NotOpen(const std::string &whatArg)
Exception error thrown when the serial port is not open.
Exception error thrown when the serial port could not be opened.
OpenFailed(const std::string &whatArg)
Exception error thrown when the serial port could not be opened.
Exception error thrown when data could not be read from the serial port before the timeout had been e...
ReadTimeout(const std::string &whatArg)
Exception error thrown when data could not be read from the serial port before the timeout had been e...