INTRODUCTION
Overview
Download and Install
Documentation
Publications

REPOSITORY
Libraries

DEVELOPER
Dev Guide
Dashboard

PEOPLE
Contributors
Users

SourceForge.net Logo
Project
Download
Mailing lists

 

         
serial.h
1 /*
2  * GearBox Project: Peer-Reviewed Open-Source Libraries for Robotics
3  * http://gearbox.sf.net/
4  * Copyright (c) 2004-2010 Alex Brooks, Mathew Ridley
5  *
6  * This distribution is licensed to you under the terms described in
7  * the LICENSE file included in this distribution.
8  *
9  */
10 #ifndef GBXSERIALACFR_SERIAL_H
11 #define GBXSERIALACFR_SERIAL_H
12 
13 #include <string>
14 #include <gbxserialacfr/uncopyable.h>
15 #include <gbxserialacfr/lockfile/lockfile.h>
16 
17 namespace gbxserialacfr {
18 
22 class SerialException : public std::exception
23 {
24  std::string message_;
25 public:
26  SerialException(const char *message)
27  : message_(message) {}
28  SerialException(const std::string &message)
29  : message_(message) {}
30  ~SerialException()throw(){}
31  virtual const char* what() const throw() { return message_.c_str(); }
32 };
33 
43 class Serial : public Uncopyable
44 {
45 public:
46 
47  struct Timeout {
48  Timeout( int s, int us )
49  : sec(s), usec(us)
50  {}
51  int sec;
52  int usec;
53  };
54 
61  Serial( const std::string &dev,
62  int baudRate,
63  const Timeout &timeout,
64  int debuglevel = 0,
65  bool useLockFile = true );
66 
68  ~Serial();
69 
71  void setDebugLevel( int debugLevel ) { debugLevel_ = debugLevel; }
72 
74  void setBaudRate(int baud);
75 
78  void setTimeout( const Timeout &timeout );
79 
82  const Timeout &timeout() const { return timeout_; }
83 
87  int read(void *buf, int count);
88 
99  int readFull(void *buf, int count);
100 
111  int readStringUntil( std::string &str, char termchar );
112 
115  int readLine( std::string &str )
116  { return readStringUntil(str,'\n'); }
117 
119  int bytesAvailable();
120 
125  int bytesAvailableWait();
126 
128  int write(const void *buf, int count);
129 
132  int writeString(const char *buf);
133  inline int writeString(const std::string &s) {
134  return writeString(s.c_str());
135  }
136 
139  void flush();
140 
142  void drain();
143 
145  int fileDescriptor() { return portFd_; }
146 
148  std::string getStatusString();
149 
150 
151 private:
152 
153  // Utility function to wait up to the timeout for data to appear.
154  // Returns:
155  enum WaitStatus {
156  TIMED_OUT,
157  DATA_AVAILABLE,
158  };
159  WaitStatus waitForDataOrTimeout(void);
160 
161  // Opens a device @c dev.
162  void open(int flags=0);
163 
164  // Won't throw exceptions.
165  void close() throw();
166 
167  bool timeoutsEnabled() const
168  { return !( timeout_.sec == 0 && timeout_.usec == 0 ); }
169 
170  const std::string dev_;
171  int portFd_;
172 
173  Timeout timeout_;
174 
175  int debugLevel_;
176 
177  lockfile::LockFile *lockFile_;
178 };
179 
180 }
181 
182 #endif
int readLine(std::string &str)
Definition: serial.h:115
Encapsulates a serial port.
Definition: serial.h:43
Simple serial port interface.
Definition: gbxnovatelacfr/driver.h:21
int fileDescriptor()
This gives direct access to the file descriptor: be careful with this...
Definition: serial.h:145
Exception thrown by Serial.
Definition: serial.h:22
const Timeout & timeout() const
Definition: serial.h:82
void setDebugLevel(int debugLevel)
Debug messages are printed to stdout. debugLevel should be in the range [0,3].
Definition: serial.h:71
Definition: lockfile.h:57
 

Generated for GearBox by  doxygen 1.4.5