Robotics Library  0.6.2
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 #include <string>
31 #include <rl/math/Real.h>
32 
33 #ifdef WIN32
34 #include <windows.h>
35 #else // WIN32
36 #include <termios.h>
37 #endif // WIN32
38 
39 #include "Com.h"
40 
41 namespace rl
42 {
43  namespace hal
44  {
45  class Serial : public Com
46  {
47  public:
48  enum BaudRate
49  {
64 #ifdef WIN32
65 
66  BAUDRATE_14400BPS,
67 #endif // WIN32
68 
75 #ifdef __QNX__
77 #else // __QNX__
79 #endif // __QNX__
80 #ifdef WIN32
81 
82  BAUDRATE_128000BPS,
84  BAUDRATE_256000BPS
85 #else // WIN32
86 #ifndef __QNX__
87 
113 #endif // __QNX__
114 #endif // WIN32
115  };
116 
117  enum DataBits
118  {
127  };
128 
130  {
137  };
138 
139  enum Parity
140  {
147  };
148 
149  enum StopBits
150  {
155  };
156 
157  Serial(
158  const ::std::string& filename,
162  const Parity& parity = PARITY_NOPARITY,
164  );
165 
166  virtual ~Serial();
167 
168  void changeParameters();
169 
170  void close();
171 
172  void doBreak(const bool& doOn);
173 
174  void doDtr(const bool& doOn);
175 
176  void doModemStatus(bool& ctsOn, bool& dsrOn, bool& riOn, bool& dcdOn);
177 
178  void doRts(const bool& doOn);
179 
180  void flush(const bool& read, const bool& write);
181 
182  BaudRate getBaudRate() const;
183 
184  DataBits getDataBits() const;
185 
186  ::std::string getFilename() const;
187 
188  FlowControl getFlowControl() const;
189 
190  Parity getParity() const;
191 
192  StopBits getStopBits() const;
193 
194  void open();
195 
196  ::std::size_t read(void* buf, const ::std::size_t& count);
197 
198  ::std::size_t select(const bool& read, const bool& write, const ::rl::math::Real& timeout);
199 
200  void setBaudRate(const BaudRate& baudRate);
201 
202  void setDataBits(const DataBits& dataBits);
203 
204  void setFilename(const ::std::string& filename);
205 
207 
208  void setParity(const Parity& parity);
209 
210  void setStopBits(const StopBits& stopBits);
211 
212  ::std::size_t write(const void* buf, const ::std::size_t& count);
213 
214  protected:
215 
216  private:
218 
219 #ifdef WIN32
220  DCB current;
221 #else // WIN32
222  struct termios current;
223 #endif // WIN32
224 
226 
227 #ifdef WIN32
228  HANDLE fd;
229 #else // WIN32
230  int fd;
231 #endif // WIN32
232 
233  ::std::string filename;
234 
236 
238 
239 #ifdef WIN32
240  DCB restore;
241 #else // WIN32
242  struct termios restore;
243 #endif // WIN32
244 
246  };
247  }
248 }
249 
250 #endif // _RL_HAL_SERIAL_H_
rl::hal::Serial::BAUDRATE_1500000BPS
@ BAUDRATE_1500000BPS
1,500,000 bps.
Definition: Serial.h:102
rl::hal::Serial::BAUDRATE_2500000BPS
@ BAUDRATE_2500000BPS
2,500,000 bps.
Definition: Serial.h:106
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)
Definition: Serial.cpp:58
rl::hal::Serial::setFlowControl
void setFlowControl(const FlowControl &flowControl)
Definition: Serial.cpp:710
rl::hal::Serial::BAUDRATE_57600BPS
@ BAUDRATE_57600BPS
57,600 bps.
Definition: Serial.h:73
rl::hal::Serial::BAUDRATE_3500000BPS
@ BAUDRATE_3500000BPS
3,500,000 bps.
Definition: Serial.h:110
rl::hal::Serial::getParity
Parity getParity() const
Definition: Serial.cpp:340
rl::hal::Serial::DataBits
DataBits
Definition: Serial.h:118
rl::hal::Serial::PARITY_EVENPARITY
@ PARITY_EVENPARITY
Even parity.
Definition: Serial.h:142
rl::hal::Serial::BAUDRATE_2400BPS
@ BAUDRATE_2400BPS
2,400 bps.
Definition: Serial.h:59
Com.h
rl::hal::Serial::BAUDRATE_230400BPS
@ BAUDRATE_230400BPS
230,400 bps.
Definition: Serial.h:88
rl::hal::Serial::BAUDRATE_4000000BPS
@ BAUDRATE_4000000BPS
4,000,000 bps.
Definition: Serial.h:112
rl::hal::Serial::PARITY_NOPARITY
@ PARITY_NOPARITY
No parity.
Definition: Serial.h:144
rl::hal::Serial::BAUDRATE_600BPS
@ BAUDRATE_600BPS
600 bps.
Definition: Serial.h:55
rl::hal::Serial::doBreak
void doBreak(const bool &doOn)
Definition: Serial.cpp:158
rl::hal::Serial::DATABITS_7BITS
@ DATABITS_7BITS
7 data bits.
Definition: Serial.h:124
rl::hal::Serial::BAUDRATE_3000000BPS
@ BAUDRATE_3000000BPS
3,000,000 bps.
Definition: Serial.h:108
rl::hal::Serial::DATABITS_5BITS
@ DATABITS_5BITS
5 data bits.
Definition: Serial.h:120
rl::hal::Serial::BAUDRATE_460800BPS
@ BAUDRATE_460800BPS
460,800 bps.
Definition: Serial.h:90
rl::hal::Serial::parity
Parity parity
Definition: Serial.h:237
rl::hal::Serial::BAUDRATE_921600BPS
@ BAUDRATE_921600BPS
921,600 bps.
Definition: Serial.h:96
rl::hal::Serial::dataBits
DataBits dataBits
Definition: Serial.h:225
rl::hal::Serial::BAUDRATE_1200BPS
@ BAUDRATE_1200BPS
1,200 bps.
Definition: Serial.h:57
rl::hal::Serial
Definition: Serial.h:46
rl::hal::Serial::DATABITS_8BITS
@ DATABITS_8BITS
8 data bits.
Definition: Serial.h:126
rl::hal::Serial::current
struct termios current
Definition: Serial.h:222
rl::hal::Serial::stopBits
StopBits stopBits
Definition: Serial.h:245
rl::hal::Serial::setFilename
void setFilename(const ::std::string &filename)
Definition: Serial.cpp:704
rl::hal::Serial::PARITY_ODDPARITY
@ PARITY_ODDPARITY
Odd parity.
Definition: Serial.h:146
rl::hal::Serial::select
::std::size_t select(const bool &read, const bool &write, const ::rl::math::Real &timeout)
Definition: Serial.cpp:447
rl::hal::Serial::~Serial
virtual ~Serial()
Definition: Serial.cpp:87
rl::hal::Serial::getDataBits
DataBits getDataBits() const
Definition: Serial.cpp:322
rl::hal::Serial::BAUDRATE_38400BPS
@ BAUDRATE_38400BPS
38,400 bps.
Definition: Serial.h:71
rl::hal::Serial::close
void close()
Definition: Serial.cpp:116
rl::hal::Serial::BAUDRATE_1152000BPS
@ BAUDRATE_1152000BPS
1,152000 bps.
Definition: Serial.h:100
rl::hal::Serial::setStopBits
void setStopBits(const StopBits &stopBits)
Definition: Serial.cpp:823
rl::hal::Serial::open
void open()
Definition: Serial.cpp:352
rl::hal::Serial::STOPBITS_1BIT
@ STOPBITS_1BIT
1 stop bit.
Definition: Serial.h:152
rl::hal::Serial::BAUDRATE_1000000BPS
@ BAUDRATE_1000000BPS
1,000,000 bps.
Definition: Serial.h:98
rl::hal::Serial::read
::std::size_t read(void *buf, const ::std::size_t &count)
Definition: Serial.cpp:417
rl::hal::Serial::getFilename
::std::string getFilename() const
Definition: Serial.cpp:328
rl::hal::Com
Definition: Com.h:35
rl::hal::Serial::restore
struct termios restore
Definition: Serial.h:242
rl::hal::Serial::doRts
void doRts(const bool &doOn)
Definition: Serial.cpp:232
rl::hal::Serial::FLOWCONTROL_RTSCTS
@ FLOWCONTROL_RTSCTS
Hardware flow control (RTS/CTS).
Definition: Serial.h:134
rl::hal::Serial::FlowControl
FlowControl
Definition: Serial.h:130
rl::hal::Serial::BAUDRATE_576000BPS
@ BAUDRATE_576000BPS
576,000 bps.
Definition: Serial.h:94
rl::hal::Serial::fd
int fd
Definition: Serial.h:230
rl::hal::Serial::BAUDRATE_110BPS
@ BAUDRATE_110BPS
110 bps.
Definition: Serial.h:51
rl::hal::Serial::changeParameters
void changeParameters()
Definition: Serial.cpp:96
rl::hal::Serial::baudRate
BaudRate baudRate
Definition: Serial.h:217
Real.h
rl::hal::Serial::BAUDRATE_19200BPS
@ BAUDRATE_19200BPS
19,200 bps.
Definition: Serial.h:69
rl::hal::Serial::setDataBits
void setDataBits(const DataBits &dataBits)
Definition: Serial.cpp:652
rl::hal::Serial::FLOWCONTROL_OFF
@ FLOWCONTROL_OFF
No flow control.
Definition: Serial.h:132
rl::hal::Serial::flowControl
FlowControl flowControl
Definition: Serial.h:235
rl::hal::Serial::BaudRate
BaudRate
Definition: Serial.h:49
rl::hal::Serial::FLOWCONTROL_XONXOFF
@ FLOWCONTROL_XONXOFF
Software flow control (XON/XOFF).
Definition: Serial.h:136
rl::hal::Serial::getFlowControl
FlowControl getFlowControl() const
Definition: Serial.cpp:334
rl::hal::Serial::BAUDRATE_4800BPS
@ BAUDRATE_4800BPS
4,800 bps.
Definition: Serial.h:61
rl::hal::Serial::Parity
Parity
Definition: Serial.h:140
rl::hal::Serial::BAUDRATE_115200BPS
@ BAUDRATE_115200BPS
115,200 bps.
Definition: Serial.h:78
rl::hal::Serial::flush
void flush(const bool &read, const bool &write)
Definition: Serial.cpp:252
rl::hal::Serial::filename
::std::string filename
Definition: Serial.h:233
rl::hal::Serial::DATABITS_6BITS
@ DATABITS_6BITS
6 data bits.
Definition: Serial.h:122
rl::hal::Serial::BAUDRATE_500000BPS
@ BAUDRATE_500000BPS
500,000 bps.
Definition: Serial.h:92
rl::hal::Serial::setBaudRate
void setBaudRate(const BaudRate &baudRate)
Definition: Serial.cpp:502
rl::hal::Serial::BAUDRATE_2000000BPS
@ BAUDRATE_2000000BPS
2,000,000 bps.
Definition: Serial.h:104
rl::math::Real
double Real
Definition: Real.h:34
rl::hal::Serial::doModemStatus
void doModemStatus(bool &ctsOn, bool &dsrOn, bool &riOn, bool &dcdOn)
Definition: Serial.cpp:198
rl::hal::Serial::StopBits
StopBits
Definition: Serial.h:150
rl::hal::Serial::BAUDRATE_9600BPS
@ BAUDRATE_9600BPS
9,600 bps.
Definition: Serial.h:63
rl::hal::Serial::getStopBits
StopBits getStopBits() const
Definition: Serial.cpp:346
rl::hal::Serial::setParity
void setParity(const Parity &parity)
Definition: Serial.cpp:778
rl::hal::Serial::getBaudRate
BaudRate getBaudRate() const
Definition: Serial.cpp:316
rl::hal::Serial::STOPBITS_2BITS
@ STOPBITS_2BITS
2 stop bits.
Definition: Serial.h:154
rl::hal::Serial::BAUDRATE_300BPS
@ BAUDRATE_300BPS
300 bps.
Definition: Serial.h:53
rl::hal::Serial::write
::std::size_t write(const void *buf, const ::std::size_t &count)
Definition: Serial.cpp:855
rl
Definition: Ati.cpp:35
rl::hal::Serial::doDtr
void doDtr(const bool &doOn)
Definition: Serial.cpp:178