Robotics Library  0.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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 // * Neither the name of the Technische Universitaet Muenchen nor the names of
14 // its contributors may be used to endorse or promote products derived from
15 // this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 // POSSIBILITY OF SUCH DAMAGE.
28 //
29 
30 #ifndef _RL_HAL_SERIAL_H_
31 #define _RL_HAL_SERIAL_H_
32 
33 #include <string>
34 #include <rl/math/Real.h>
35 
36 #ifdef WIN32
37 #include <windows.h>
38 #else // WIN32
39 #include <termios.h>
40 #endif // WIN32
41 
42 #include "Com.h"
43 
44 namespace rl
45 {
46  namespace hal
47  {
48  class Serial : public Com
49  {
50  public:
51  enum BaudRate
52  {
67 #ifdef WIN32
68 
69  BAUDRATE_14400BPS,
70 #endif // WIN32
71 
78 #ifdef __QNX__
80 #else // __QNX__
82 #endif // __QNX__
83 #ifdef WIN32
84 
85  BAUDRATE_128000BPS,
87  BAUDRATE_256000BPS
88 #else // WIN32
89 #ifndef __QNX__
90 
116 #endif // __QNX__
117 #endif // WIN32
118  };
119 
120  enum DataBits
121  {
130  };
131 
133  {
140  };
141 
142  enum Parity
143  {
150  };
151 
152  enum StopBits
153  {
158  };
159 
160  Serial(
161  const ::std::string& filename,
165  const Parity& parity = PARITY_NOPARITY,
167  );
168 
169  virtual ~Serial();
170 
171  void changeParameters();
172 
173  void close();
174 
175  void flush(const bool& read, const bool& write);
176 
177  BaudRate getBaudRate() const;
178 
179  DataBits getDataBits() const;
180 
181  ::std::string getFilename() const;
182 
183  FlowControl getFlowControl() const;
184 
185  Parity getParity() const;
186 
187  StopBits getStopBits() const;
188 
189  void open();
190 
191  ::std::size_t read(void* buf, const ::std::size_t& count);
192 
193  ::std::size_t select(const bool& read, const bool& write, const ::rl::math::Real& timeout);
194 
195  void setBaudRate(const BaudRate& baudRate);
196 
197  void setDataBits(const DataBits& dataBits);
198 
199  void setFilename(const ::std::string& filename);
200 
202 
203  void setParity(const Parity& parity);
204 
205  void setStopBits(const StopBits& stopBits);
206 
207  ::std::size_t write(const void* buf, const ::std::size_t& count);
208 
209  protected:
210 
211  private:
213 
214 #ifdef WIN32
215  DCB current;
216 #else // WIN32
217  struct termios current;
218 #endif // WIN32
219 
221 
222 #ifdef WIN32
223  HANDLE fd;
224 #else // WIN32
225  int fd;
226 #endif // WIN32
227 
228  ::std::string filename;
229 
231 
233 
234 #ifdef WIN32
235  DCB restore;
236 #else // WIN32
237  struct termios restore;
238 #endif // WIN32
239 
241  };
242  }
243 }
244 
245 #endif // _RL_HAL_SERIAL_H_