4 #include "abstractio.hpp"
10 #include <sys/types.h>
17 class SerialPort: public AbstractIo
29 SerialPort(int fileDesc)
37 SerialPort(std::string _tty)
48 int setSpeed(int newspeed)
76 fd = ::open(tty.c_str(), O_RDWR, O_NOCTTY);
86 int fileDescriptor() { return fd; }
98 while( bytesread = ::read(fd, &buff, 1) > 0 )
104 perror("Error while reading: ");
109 void write(std::string data)
111 int written = ::write(fd,data.c_str(),data.length());
114 DebugOut(DebugOut::Warning)<<"Unable to write ("<<fd<<")"<<endl;
115 perror("write error: ");
119 void setDescriptor(int d)
131 DebugOut()<<"Cannot open serial device."<<endl;
135 struct termios oldtio;
136 tcgetattr(fd,&oldtio);
138 oldtio.c_cflag |= CS8 | CLOCAL | CREAD;
140 oldtio.c_iflag |= IGNPAR;
141 oldtio.c_iflag &= ~(ICRNL | IMAXBEL);
144 oldtio.c_oflag &= ~OPOST;
146 oldtio.c_lflag |= ECHOE | ECHOK | ECHOCTL | ECHOKE;
147 oldtio.c_lflag &= ~(ECHO | ICANON | ISIG);
149 //oldtio.c_cc[VEOL] = '\r';
151 cfsetispeed(&oldtio, speed);
152 cfsetospeed(&oldtio, speed);
154 tcflush(fd, TCIFLUSH);
155 tcsetattr(fd, TCSANOW, &oldtio);
157 fcntl(fd,F_SETFL,O_NONBLOCK);