Robotics Library  0.7.0
Serial.h
Go to the documentation of this file.
1 //
2 // Copyright (c) 2009, Markus Rickert
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 //
8 // * Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright notice,
11 // this list of conditions and the following disclaimer in the documentation
12 // and/or other materials provided with the distribution.
13 //
14 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
18 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 // POSSIBILITY OF SUCH DAMAGE.
25 //
26 
27 #ifndef RL_HAL_SERIAL_H
28 #define RL_HAL_SERIAL_H
29 
30 #ifdef WIN32
31 #include <windows.h>
32 #else // WIN32
33 #include <fcntl.h>
34 #include <termios.h>
35 #endif // WIN32
36 
37 #include <chrono>
38 #include <string>
39 #include <rl/math/Real.h>
40 
41 #include "Com.h"
42 
43 namespace rl
44 {
45  namespace hal
46  {
47  class Serial : public Com
48  {
49  public:
50  enum BaudRate
51  {
66 #ifdef WIN32
67 
68  BAUDRATE_14400BPS,
69 #endif // WIN32
70 
77 #ifdef __QNX__
79 #else // __QNX__
81 #endif // __QNX__
82 #ifdef WIN32
83 
84  BAUDRATE_128000BPS,
86  BAUDRATE_256000BPS
87 #else // WIN32
88 #ifndef __QNX__
89 
115 #endif // __QNX__
116 #endif // WIN32
117  };
118 
119  enum DataBits
120  {
129  };
130 
132  {
139  };
140 
141  enum Parity
142  {
149  };
150 
151  enum StopBits
152  {
157  };
158 
159  Serial(
160  const ::std::string& filename,
164  const Parity& parity = PARITY_NOPARITY,
166 #ifdef WIN32
167  const int& flags = GENERIC_READ | GENERIC_WRITE
168 #else // WIN32
169  const int& flags = O_RDWR | O_NONBLOCK | O_NOCTTY
170 #endif // WIN32
171  );
172 
173  virtual ~Serial();
174 
175  void changeParameters();
176 
177  void close();
178 
179  void doBreak(const bool& doOn);
180 
181  void doDtr(const bool& doOn);
182 
183  void doModemStatus(bool& ctsOn, bool& dsrOn, bool& riOn, bool& dcdOn);
184 
185  void doRts(const bool& doOn);
186 
187  void flush(const bool& read, const bool& write);
188 
189  const BaudRate& getBaudRate() const;
190 
191  const DataBits& getDataBits() const;
192 
193  const ::std::string& getFilename() const;
194 
195  const FlowControl& getFlowControl() const;
196 
197  const Parity& getParity() const;
198 
199  const StopBits& getStopBits() const;
200 
201  void open();
202 
203  ::std::size_t read(void* buf, const ::std::size_t& count);
204 
205  ::std::size_t select(const bool& read, const bool& write, const ::std::chrono::nanoseconds& timeout);
206 
207  void setBaudRate(const BaudRate& baudRate);
208 
209  void setDataBits(const DataBits& dataBits);
210 
211  void setFilename(const ::std::string& filename);
212 
214 
215  void setParity(const Parity& parity);
216 
217  void setStopBits(const StopBits& stopBits);
218 
219  ::std::size_t write(const void* buf, const ::std::size_t& count);
220 
221  protected:
222 
223  private:
225 
226 #ifdef WIN32
227  DCB current;
228 #else // WIN32
229  struct termios current;
230 #endif // WIN32
231 
233 
234 #ifdef WIN32
235  HANDLE fd;
236 #else // WIN32
237  int fd;
238 #endif // WIN32
239 
240  ::std::string filename;
241 
242  int flags;
243 
245 
247 
248 #ifdef WIN32
249  DCB restore;
250 #else // WIN32
251  struct termios restore;
252 #endif // WIN32
253 
255  };
256  }
257 }
258 
259 #endif // RL_HAL_SERIAL_H
rl::hal::Serial::BAUDRATE_1500000BPS
@ BAUDRATE_1500000BPS
1,500,000 bps.
Definition: Serial.h:104
endif
dummy cpp endif() if(CMAKE_SIZEOF_VOID_P EQUAL 4) target_compile_definitions(math INTERFACE -DEIGEN_DONT_ALIGN_STATICALLY) endif() if(NOT CMAKE_VERSION VERSION_LESS 3.8) target_compile_features(math INTERFACE cxx_std_11) endif() target_include_directories(math INTERFACE $< BUILD_INTERFACE
Definition: CMakeLists.txt:66
rl::hal::Serial::BAUDRATE_2500000BPS
@ BAUDRATE_2500000BPS
2,500,000 bps.
Definition: Serial.h:108
rl::hal::Serial::setFlowControl
void setFlowControl(const FlowControl &flowControl)
Definition: Serial.cpp:634
rl::hal::Serial::BAUDRATE_57600BPS
@ BAUDRATE_57600BPS
57,600 bps.
Definition: Serial.h:75
rl::hal::Serial::BAUDRATE_3500000BPS
@ BAUDRATE_3500000BPS
3,500,000 bps.
Definition: Serial.h:112
rl::hal::Serial::DataBits
DataBits
Definition: Serial.h:120
rl::hal::Serial::PARITY_EVENPARITY
@ PARITY_EVENPARITY
Even parity.
Definition: Serial.h:144
rl::hal::Serial::BAUDRATE_2400BPS
@ BAUDRATE_2400BPS
2,400 bps.
Definition: Serial.h:61
rl::hal::Serial::flags
int flags
Definition: Serial.h:242
Com.h
rl::hal::Serial::BAUDRATE_230400BPS
@ BAUDRATE_230400BPS
230,400 bps.
Definition: Serial.h:90
rl::hal::Serial::BAUDRATE_4000000BPS
@ BAUDRATE_4000000BPS
4,000,000 bps.
Definition: Serial.h:114
rl::hal::Serial::PARITY_NOPARITY
@ PARITY_NOPARITY
No parity.
Definition: Serial.h:146
rl::hal::Serial::BAUDRATE_600BPS
@ BAUDRATE_600BPS
600 bps.
Definition: Serial.h:57
rl::hal::Serial::doBreak
void doBreak(const bool &doOn)
Definition: Serial.cpp:147
rl::hal::Serial::DATABITS_7BITS
@ DATABITS_7BITS
7 data bits.
Definition: Serial.h:126
rl::hal::Serial::BAUDRATE_3000000BPS
@ BAUDRATE_3000000BPS
3,000,000 bps.
Definition: Serial.h:110
rl::hal::Serial::DATABITS_5BITS
@ DATABITS_5BITS
5 data bits.
Definition: Serial.h:122
rl::hal::Serial::BAUDRATE_460800BPS
@ BAUDRATE_460800BPS
460,800 bps.
Definition: Serial.h:92
rl::hal::Serial::parity
Parity parity
Definition: Serial.h:246
rl::hal::Serial::BAUDRATE_921600BPS
@ BAUDRATE_921600BPS
921,600 bps.
Definition: Serial.h:98
rl::hal::Serial::dataBits
DataBits dataBits
Definition: Serial.h:232
rl::hal::Serial::BAUDRATE_1200BPS
@ BAUDRATE_1200BPS
1,200 bps.
Definition: Serial.h:59
rl::hal::Serial
Definition: Serial.h:48
rl::hal::Serial::DATABITS_8BITS
@ DATABITS_8BITS
8 data bits.
Definition: Serial.h:128
rl::hal::Serial::getParity
const Parity & getParity() const
Definition: Serial.cpp:301
rl::hal::Serial::current
struct termios current
Definition: Serial.h:229
rl::hal::Serial::stopBits
StopBits stopBits
Definition: Serial.h:254
rl::hal::Serial::getStopBits
const StopBits & getStopBits() const
Definition: Serial.cpp:307
rl::hal::Serial::setFilename
void setFilename(const ::std::string &filename)
Definition: Serial.cpp:628
rl::hal::Serial::PARITY_ODDPARITY
@ PARITY_ODDPARITY
Odd parity.
Definition: Serial.h:148
rl::hal::Serial::~Serial
virtual ~Serial()
Definition: Serial.cpp:88
rl::hal::Serial::BAUDRATE_38400BPS
@ BAUDRATE_38400BPS
38,400 bps.
Definition: Serial.h:73
rl::hal::Serial::close
void close()
Definition: Serial.cpp:113
rl::hal::Serial::BAUDRATE_1152000BPS
@ BAUDRATE_1152000BPS
1,152000 bps.
Definition: Serial.h:102
rl::hal::Serial::setStopBits
void setStopBits(const StopBits &stopBits)
Definition: Serial.cpp:747
rl::hal::Serial::open
void open()
Definition: Serial.cpp:313
rl::hal::Serial::STOPBITS_1BIT
@ STOPBITS_1BIT
1 stop bit.
Definition: Serial.h:154
rl::hal::Serial::BAUDRATE_1000000BPS
@ BAUDRATE_1000000BPS
1,000,000 bps.
Definition: Serial.h:100
rl::hal::Serial::read
::std::size_t read(void *buf, const ::std::size_t &count)
Definition: Serial.cpp:366
rl::hal::Com
Definition: Com.h:35
rl::hal::Serial::select
::std::size_t select(const bool &read, const bool &write, const ::std::chrono::nanoseconds &timeout)
Definition: Serial.cpp:392
rl::hal::Serial::getFlowControl
const FlowControl & getFlowControl() const
Definition: Serial.cpp:295
rl::hal::Serial::getDataBits
const DataBits & getDataBits() const
Definition: Serial.cpp:283
rl::hal::Serial::restore
struct termios restore
Definition: Serial.h:251
rl::hal::Serial::doRts
void doRts(const bool &doOn)
Definition: Serial.cpp:209
rl::hal::Serial::FLOWCONTROL_RTSCTS
@ FLOWCONTROL_RTSCTS
Hardware flow control (RTS/CTS).
Definition: Serial.h:136
rl::hal::Serial::FlowControl
FlowControl
Definition: Serial.h:132
rl::hal::Serial::getBaudRate
const BaudRate & getBaudRate() const
Definition: Serial.cpp:277
rl::hal::Serial::BAUDRATE_576000BPS
@ BAUDRATE_576000BPS
576,000 bps.
Definition: Serial.h:96
rl::hal::Serial::fd
int fd
Definition: Serial.h:237
rl::hal::Serial::BAUDRATE_110BPS
@ BAUDRATE_110BPS
110 bps.
Definition: Serial.h:53
rl::hal::Serial::changeParameters
void changeParameters()
Definition: Serial.cpp:97
rl::hal::Serial::baudRate
BaudRate baudRate
Definition: Serial.h:224
Real.h
rl::hal::Serial::BAUDRATE_19200BPS
@ BAUDRATE_19200BPS
19,200 bps.
Definition: Serial.h:71
rl::hal::Serial::setDataBits
void setDataBits(const DataBits &dataBits)
Definition: Serial.cpp:576
rl::hal::Serial::FLOWCONTROL_OFF
@ FLOWCONTROL_OFF
No flow control.
Definition: Serial.h:134
rl::hal::Serial::flowControl
FlowControl flowControl
Definition: Serial.h:244
rl::hal::Serial::BaudRate
BaudRate
Definition: Serial.h:51
rl::hal::Serial::FLOWCONTROL_XONXOFF
@ FLOWCONTROL_XONXOFF
Software flow control (XON/XOFF).
Definition: Serial.h:138
rl::hal::Serial::BAUDRATE_4800BPS
@ BAUDRATE_4800BPS
4,800 bps.
Definition: Serial.h:63
rl::hal::Serial::Parity
Parity
Definition: Serial.h:142
rl::hal::Serial::BAUDRATE_115200BPS
@ BAUDRATE_115200BPS
115,200 bps.
Definition: Serial.h:80
rl::hal::Serial::flush
void flush(const bool &read, const bool &write)
Definition: Serial.cpp:225
rl::hal::Serial::Serial
Serial(const ::std::string &filename, const BaudRate &baudRate=BAUDRATE_9600BPS, const DataBits &dataBits=DATABITS_8BITS, const FlowControl &flowControl=FLOWCONTROL_OFF, const Parity &parity=PARITY_NOPARITY, const StopBits &stopBits=STOPBITS_1BIT, const int &flags=O_RDWR|O_NONBLOCK|O_NOCTTY)
Definition: Serial.cpp:57
rl::hal::Serial::filename
::std::string filename
Definition: Serial.h:240
rl::hal::Serial::DATABITS_6BITS
@ DATABITS_6BITS
6 data bits.
Definition: Serial.h:124
rl::hal::Serial::BAUDRATE_500000BPS
@ BAUDRATE_500000BPS
500,000 bps.
Definition: Serial.h:94
rl::hal::Serial::setBaudRate
void setBaudRate(const BaudRate &baudRate)
Definition: Serial.cpp:426
rl::hal::Serial::BAUDRATE_2000000BPS
@ BAUDRATE_2000000BPS
2,000,000 bps.
Definition: Serial.h:106
rl::hal::Serial::doModemStatus
void doModemStatus(bool &ctsOn, bool &dsrOn, bool &riOn, bool &dcdOn)
Definition: Serial.cpp:179
rl::hal::Serial::StopBits
StopBits
Definition: Serial.h:152
rl::hal::Serial::getFilename
const ::std::string & getFilename() const
Definition: Serial.cpp:289
rl::hal::Serial::BAUDRATE_9600BPS
@ BAUDRATE_9600BPS
9,600 bps.
Definition: Serial.h:65
rl::hal::Serial::setParity
void setParity(const Parity &parity)
Definition: Serial.cpp:702
rl::hal::Serial::STOPBITS_2BITS
@ STOPBITS_2BITS
2 stop bits.
Definition: Serial.h:156
rl::hal::Serial::BAUDRATE_300BPS
@ BAUDRATE_300BPS
300 bps.
Definition: Serial.h:55
rl::hal::Serial::write
::std::size_t write(const void *buf, const ::std::size_t &count)
Definition: Serial.cpp:779
rl
Robotics Library.
Definition: AnalogInput.cpp:30
rl::hal::Serial::doDtr
void doDtr(const bool &doOn)
Definition: Serial.cpp:163