SerialDevice.h
Go to the documentation of this file.
00001 /*
00002  * SerialDevice.h
00003  *
00004  *  Created on: Oct 13, 2011
00005  *      Author: mriedel
00006  */
00007 
00008 #ifndef SERIALDEVICE_H_
00009 #define SERIALDEVICE_H_
00010 
00011 #include <telekyb_defines/telekyb_defines.hpp>
00012 
00013 #include <telekyb_serial/SerialException.h>
00014 
00015 // File Stuff
00016 #include <fcntl.h>
00017 #include <termios.h>
00018 
00019 // stl
00020 #include <iostream>
00021 
00022 // boost
00023 #include <boost/thread/mutex.hpp>
00024 
00025 
00026 
00027 #define DEFAULT_BUFFERSIZE 1024
00028 #define BUNDEF 0111111
00029 
00030 namespace TELEKYB_NAMESPACE
00031 {
00032 
00033 // represents an SerialDevice
00034 class SerialDevice {
00035 protected:
00036         std::string deviceName;
00037         //bool deviceOpen; // true if fd is a valid file descriptor
00038         int deviceFD;
00039         struct termios deviceAttr;
00040 
00041         // writing is mutex protected
00042         mutable boost::mutex readMutex;
00043         boost::mutex writeMutex;
00044 
00045 public:
00046         SerialDevice();
00047         // Throws SerialException! if autoOpen == true
00048         SerialDevice(const std::string& deviceName_, bool autoOpen = true, int oflag = O_RDWR | O_NOCTTY | O_NONBLOCK);
00049         virtual ~SerialDevice();
00050 
00051         // Throws SerialException!
00052         void openDevice(int oflag = O_RDWR | O_NOCTTY | O_NONBLOCK);
00053         void closeDevice();
00054 
00055         bool isOpen() const;
00056 
00057         // readAvailable (check FD if data is available)
00058         bool readAvailable(timeval timeout) const;
00059 
00060         // read
00061         int readDevice(char* buffer, size_t size, std::string terminalChars = std::string("\r\n")) const;
00062 
00063         // write
00064         int writeDevice(const void* buffer, size_t size) throw(SerialException);
00065 
00066         // streaming operator
00067         friend std::ostream& operator<<(std::ostream& stream, const SerialDevice& device);
00068         friend std::string& operator<<(std::string& string, const SerialDevice& device);
00069 
00070         friend std::istream& operator>>(std::istream& stream, SerialDevice& device);
00071         friend std::string& operator>>(std::string& string, SerialDevice& device);
00072 
00073 
00074         // Termios Attributes
00075         bool setTermiosAttr(
00076                         tcflag_t c_iflag,
00077                         tcflag_t c_oflag,
00078                         tcflag_t c_cflag,
00079                         tcflag_t c_lflag,
00080                         speed_t ispeed = BUNDEF, speed_t ospeed = BUNDEF, int optionalOptions = TCSAFLUSH );
00081         void printTermiosAttr() const;
00082 
00083         // convenience functions
00084         bool setTermiosAttrIFlag(tcflag_t c_iflag, int optionalOptions = TCSAFLUSH );
00085         bool setTermiosAttrOFlag(tcflag_t c_oflag, int optionalOptions = TCSAFLUSH );
00086         bool setTermiosAttrCFlag(tcflag_t c_cflag, int optionalOptions = TCSAFLUSH );
00087         bool setTermiosAttrLFlag(tcflag_t c_lflag, int optionalOptions = TCSAFLUSH );
00088         bool setTermiosAttrSpeed(speed_t ispeed, speed_t ospeed, int optionalOptions = TCSAFLUSH );
00089 };
00090 
00091 } // namespace
00092 
00093 #endif /* SERIALDEVICE_H_ */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Defines


telekyb_serial
Author(s): Martin Riedel
autogenerated on Mon Nov 11 2013 11:14:08