GpCoreTools/Serial.h
Go to the documentation of this file.
00001 /***************************************************************************
00002 **
00003 **  This file is part of GpCoreTools.
00004 **
00005 **  This file may be distributed and/or modified under the terms of the
00006 **  GNU General Public License version 2 or 3 as published by the Free
00007 **  Software Foundation and appearing in the file LICENSE.GPL included
00008 **  in the packaging of this file.
00009 **
00010 **  This file is distributed in the hope that it will be useful, but WITHOUT
00011 **  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 **  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
00013 **  more details.
00014 **
00015 **  You should have received a copy of the GNU General Public License
00016 **  along with this program. If not, see <http://www.gnu.org/licenses/>.
00017 **
00018 **  See http://www.geopsy.org for more information.
00019 **
00020 **  Created: 2009-06-26
00021 **  Authors:
00022 **    Marc Wathelet
00023 **    Marc Wathelet (LGIT, Grenoble, France)
00024 **
00025 ***************************************************************************/
00026 
00027 #ifndef SERIAL_H
00028 #define SERIAL_H
00029 
00030 #include <unistd.h>
00031 #include <termios.h>
00032 #include <string>
00033 
00034 #include "Stream.h"
00035 #include "DynamicBuffer.h"
00036 #include "GpCoreToolsDLLExport.h"
00037 
00038 struct termios;
00039 
00040 namespace GpCoreTools {
00041 
00042 class GPCORETOOLS_EXPORT Serial : public Stream
00043 {
00044 public:
00045   Serial(const std::string& devicePath, speed_t speed, DynamicBuffer * buffer=0);
00046   virtual ~Serial();
00047 
00048   bool connect();
00049 
00050   virtual short eventTypes() {return POLLIN | POLLPRI | POLLRDHUP | POLLHUP;}
00051   virtual void event(short type);
00052   virtual void hungUp();
00053 
00054   bool isConnected();
00055 
00056   inline bool write(const char * buf, ssize_t byteCount);
00057   DynamicBuffer * buffer() const {return _buffer;}
00058 
00059   int lock() {return pthread_mutex_lock(&_mutex);}
00060   bool tryLock() {return pthread_mutex_trylock(&_mutex)==0;}
00061   int unlock() {return pthread_mutex_unlock(&_mutex);}
00062 private:
00063   bool open();
00064   void init();
00065 
00066   static int outputSpeed(termios * t);
00067 
00068   pthread_mutex_t _mutex;
00069   DynamicBuffer * _buffer;
00070   std::string _devicePath;
00071   speed_t _speed;
00072 };
00073 
00074 inline bool Serial::write(const char * buf, ssize_t byteCount)
00075 {
00076   return ::write(fileDescriptor(), buf, byteCount)==byteCount;
00077 }
00078 
00079 } // namespace GpCoreTools
00080 
00081 #endif // SERIAL_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines