CerealPort.h
Go to the documentation of this file.
00001 /*********************************************************************
00002 *
00003 * Software License Agreement (BSD License)
00004 *
00005 *  Copyright (c) 2010, ISR University of Coimbra.
00006 *  All rights reserved.
00007 *
00008 *  Redistribution and use in source and binary forms, with or without
00009 *  modification, are permitted provided that the following conditions
00010 *  are met:
00011 *
00012 *   * Redistributions of source code must retain the above copyright
00013 *     notice, this list of conditions and the following disclaimer.
00014 *   * Redistributions in binary form must reproduce the above
00015 *     copyright notice, this list of conditions and the following
00016 *     disclaimer in the documentation and/or other materials provided
00017 *     with the distribution.
00018 *   * Neither the name of the ISR University of Coimbra nor the names of its
00019 *     contributors may be used to endorse or promote products derived
00020 *     from this software without specific prior written permission.
00021 *
00022 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00025 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00026 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00027 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00028 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00029 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00030 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00032 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00033 *  POSSIBILITY OF SUCH DAMAGE.
00034 *
00035 * Author: Gonçalo Cabrita on 01/10/2010
00036 *********************************************************************/
00037 #include <stdexcept>
00038 #include <termios.h>
00039 #include <string>
00040 #include <vector>
00041 #include <stdint.h>
00042 
00043 #include <boost/function.hpp>
00044 #include <boost/thread/thread.hpp>
00045 
00046 #define MAX_LENGTH 128
00047 
00048 namespace cereal
00049 {
00051         #define DEF_EXCEPTION(name, parent) \
00052         class name : public parent { \
00053                 public: \
00054         name(const char* msg) : parent(msg) {} \
00055         }
00056   
00058         DEF_EXCEPTION(Exception, std::runtime_error);
00059 
00061         DEF_EXCEPTION(TimeoutException, Exception);
00062 
00063         #undef DEF_EXCEPTION
00064 
00070         class CerealPort
00071         {
00072                 public:
00074                 CerealPort();
00076                 ~CerealPort();
00077 
00079 
00086                 void open(const char * port_name, int baud_rate = 115200);
00087 
00089 
00092                 void close();
00093 
00095                 bool portOpen() { return fd_ != -1; }
00096 
00098                 int baudRate() { return baud_; }
00099 
00101 
00109                 int write(const char * data, int length = -1);
00110                 
00112 
00121             int read(char * data, int max_length, int timeout = -1);
00122 
00124 
00135             int readBytes(char * data, int length, int timeout = -1);
00136 
00138 
00149             int readLine(char * data, int length, int timeout = -1);
00150             
00152 
00162             bool readLine(std::string * data, int timeout = -1);
00163             
00165 
00175                 //TODO: int readBetween(char * data, int length, char start, char end, int timeout = -1);
00176             bool readBetween(std::string * data, char start, char end, int timeout = -1);
00177 
00179             int flush();
00180             
00181             //*** Stream functions ***
00182             
00184 
00193             bool startReadStream(boost::function<void(char*, int)> f);
00194             
00196 
00205             bool startReadLineStream(boost::function<void(std::string*)> f);
00206             
00208 
00219             bool startReadBetweenStream(boost::function<void(std::string*)> f, char start, char end);
00220             
00222                 void stopStream();
00224                 void pauseStream();
00226                 void resumeStream();
00227                 
00228                 private:
00230                 int fd_;
00232                 int baud_;
00233                 
00234                 //std::vector<char> leftovers;
00235                 
00237 
00240                 void readThread();
00241                 
00243 
00246             void readLineThread();
00247             
00249 
00256             void readBetweenThread(char start, char end);
00257                 
00259                 boost::thread * stream_thread_;
00260                 
00262                 boost::function<void(char*, int)> readCallback;
00264                 boost::function<void(std::string*)> readLineCallback;
00266                 boost::function<void(std::string*)> readBetweenCallback;
00267                 
00269                 bool stream_paused_;
00271                 bool stream_stopped_;
00272         };
00273 
00274 }
00275 
00276 // EOF


cereal_port
Author(s): Gonçalo Cabrita and Pedro Sousa
autogenerated on Mon Jan 6 2014 11:25:14