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
17namespace gbxserialacfr {
18
22class SerialException : public std::exception
23{
24 std::string message_;
25public:
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
43class Serial : public Uncopyable
44{
45public:
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
151private:
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 readStringUntil(std::string &str, char termchar)
Definition serial.cpp:650
int fileDescriptor()
This gives direct access to the file descriptor: be careful with this...
Definition serial.h:145
const Timeout & timeout() const
Definition serial.h:82
int read(void *buf, int count)
Definition serial.cpp:585
int readLine(std::string &str)
Definition serial.h:115
std::string getStatusString()
Print some diagnostic information about the current status of the port to cout.
Definition serial.cpp:794
void flush()
Definition serial.cpp:825
void setTimeout(const Timeout &timeout)
Definition serial.cpp:410
Serial(const std::string &dev, int baudRate, const Timeout &timeout, int debuglevel=0, bool useLockFile=true)
Definition serial.cpp:359
void setBaudRate(int baud)
Sets the baud rate. Flushes any data.
Definition serial.cpp:455
int readFull(void *buf, int count)
Definition serial.cpp:612
int write(const void *buf, int count)
Writes some data. Returns the number of bytes written.
Definition serial.cpp:849
~Serial()
Destructor closes serial port.
Definition serial.cpp:403
void drain()
Finishes transmission from output buffers and drains input buffers.
Definition serial.cpp:837
int writeString(const char *buf)
Definition serial.cpp:757
int bytesAvailable()
Returns the number of bytes available for reading (non-blocking).
Definition serial.cpp:704
int bytesAvailableWait()
Definition serial.cpp:719
void setDebugLevel(int debugLevel)
Debug messages are printed to stdout. debugLevel should be in the range [0,3].
Definition serial.h:71
Simple serial port interface.
Definition gbxnovatelacfr/driver.h:21
 

Generated for GearBox by  doxygen 1.4.5