#ifndef _SERIALPORT_H_ #define _SERIALPORT_H_ #include "abstractio.hpp" #include "debugout.h" #include #include #include #include #include #include #include #include #include class SerialPort: public AbstractIo { private: speed_t speed; public: SerialPort() :speed(B9600), fd(0) { } SerialPort(int fileDesc) :fd(fileDesc) { speed = B9600; setDescriptor(fd); } SerialPort(std::string _tty) :fd(0), tty(_tty) { speed = B9600; } ~SerialPort() { close(); } int setSpeed(int newspeed) { int ret = 0; switch(newspeed) { case 2400: speed = B2400; break; case 4800: speed = B4800; break; case 9600: speed = B9600; break; case 19200: speed = B19200; break; case 38400: speed = B38400; break; default: ret = -EINVAL; } return ret; } bool open() { fd = ::open(tty.c_str(), O_RDWR, O_NOCTTY); return setupDevice(); } bool isOpen() { return (fd > 0); } int fileDescriptor() { return fd; } bool close() { ::close(fd); } std::string read() { char buff; std::string result; int bytesread = 0; while((bytesread = ::read(fd, &buff, 1)) > 0 ) { result += buff; } if(bytesread == -1) perror("Error while reading: "); return result; } void write(const std::string & data) { int written = ::write(fd, data.c_str(), data.length()); if(written == -1) { DebugOut(DebugOut::Warning)<<"Unable to write ("<