Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
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 }
00080
00081 #endif // SERIAL_H