00001 /* 00002 * ThreadedSerialDevice.hpp 00003 * 00004 * Created on: Nov 23, 2011 00005 * Author: mriedel 00006 */ 00007 00008 #ifndef THREADEDSERIALDEVICE_HPP_ 00009 #define THREADEDSERIALDEVICE_HPP_ 00010 00011 #include <telekyb_defines/telekyb_defines.hpp> 00012 #include <telekyb_serial/SerialDevice.h> 00013 00014 #include <set> 00015 #include <vector> 00016 00017 // boost 00018 #include <boost/thread.hpp> 00019 00020 namespace TELEKYB_NAMESPACE { 00021 00022 // Listener Definition 00023 class SerialDeviceListener 00024 { 00025 public: 00026 virtual void handleReadSerialData(const std::vector<char>& data) = 0; 00027 virtual ~SerialDeviceListener() {} 00028 }; 00029 00030 class ThreadedSerialDevice : public SerialDevice 00031 { 00032 protected: 00033 std::set<SerialDeviceListener*> serialDeviceListenerSet; 00034 boost::thread* readingThread; // NULL if not running! 00035 std::string terminalChars; 00036 00037 bool readingThreadStopRequest; 00038 00039 //void initReadingThread(); 00040 void readingThreadFcn(); 00041 void stopReadingThread(); 00042 00043 void informListeners(const std::vector<char>& data); 00044 00045 public: 00046 // Device is opened blocking. Read Thread can block! 00047 ThreadedSerialDevice(); 00048 // Throws SerialException! if Autoopen == true 00049 ThreadedSerialDevice(const std::string& deviceName_, const std::string& terminalChars_ = "\r\n", 00050 bool autoOpen = true, int oflag = O_RDWR | O_NOCTTY | O_NONBLOCK); 00051 virtual ~ThreadedSerialDevice(); 00052 00053 // Throws SerialException 00054 void openDevice(int oflag = O_RDWR | O_NOCTTY | O_NONBLOCK); 00055 void closeDevice(); 00056 00057 void registerSerialDeviceListener(SerialDeviceListener* listener); 00058 void unRegisterSerialDeviceListener(SerialDeviceListener* listener); 00059 00060 std::string getTerminalChars() const; 00061 void setTerminalChars(const std::string& terminalChars_); 00062 00063 }; 00064 00065 } 00066 00067 #endif /* THREADEDSERIALDEVICE_HPP_ */