summaryrefslogtreecommitdiff
path: root/plugins/common/serialport.hpp
diff options
context:
space:
mode:
authorKevron Rees <tripzero.kev@gmail.com>2013-09-03 21:11:18 -0700
committerKevron Rees <tripzero.kev@gmail.com>2013-09-03 21:11:51 -0700
commit2b6a74685f691c0b8525fc2098634a1b1dd9f186 (patch)
treed9e9707c7d34616b1ce28e670915921e62e47fa3 /plugins/common/serialport.hpp
parentda8ae575df87bce3fd0787a7d0ffbbab44bd2d96 (diff)
downloadautomotive-message-broker-2b6a74685f691c0b8525fc2098634a1b1dd9f186.tar.gz
added common library for plugins
Diffstat (limited to 'plugins/common/serialport.hpp')
-rw-r--r--plugins/common/serialport.hpp101
1 files changed, 101 insertions, 0 deletions
diff --git a/plugins/common/serialport.hpp b/plugins/common/serialport.hpp
new file mode 100644
index 00000000..50623806
--- /dev/null
+++ b/plugins/common/serialport.hpp
@@ -0,0 +1,101 @@
+#ifndef _SERIALPORT_H_
+#define _SERIALPORT_H_
+
+#include "abstractio.hpp"
+#include "debugout.h"
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <termios.h>
+#include <errno.h>
+
+class SerialPort: public AbstractIo
+{
+public:
+ SerialPort(std::string _tty)
+ :tty(_tty)
+ {
+
+ }
+
+ ~SerialPort()
+ {
+ close();
+ }
+
+ bool open()
+ {
+ fd = ::open(tty.c_str(), O_RDWR, O_NOCTTY);
+
+ if(fd == -1)
+ {
+ DebugOut()<<"Cannot open serial device."<<endl;
+ return false;
+ }
+
+ struct termios oldtio;
+ tcgetattr(fd,&oldtio);
+
+ oldtio.c_cflag |= CS8 | CLOCAL | CREAD;
+
+ oldtio.c_iflag |= IGNPAR;
+ oldtio.c_iflag &= ~(ICRNL | IMAXBEL);
+
+
+ oldtio.c_oflag &= ~OPOST;
+
+ oldtio.c_lflag |= ECHOE | ECHOK | ECHOCTL | ECHOKE;
+ oldtio.c_lflag &= ~(ECHO | ICANON | ISIG);
+
+ //oldtio.c_cc[VEOL] = '\r';
+
+ cfsetispeed(&oldtio, B9600);
+ cfsetospeed(&oldtio, B9600);
+
+ tcflush(fd, TCIFLUSH);
+ tcsetattr(fd, TCSANOW, &oldtio);
+
+ fcntl(fd,F_SETFL,O_NONBLOCK);
+
+ return true;
+ }
+
+ int fileDescriptor() { return fd; }
+
+ bool close()
+ {
+ ::close(fd);
+ }
+
+ std::string read()
+ {
+ memset(buff,'\0',sizeof(buff));
+ int bytesread = ::read(fd,buff,512);
+
+ /*if(bytesread == -1)
+ perror("Error while reading: ");*/
+
+ return std::string(buff);
+ }
+
+ void write(std::string data)
+ {
+ int written = ::write(fd,data.c_str(),data.length());
+ if(written == -1)
+ {
+ DebugOut()<<"Unable to write"<<endl;
+ }
+ }
+
+private:
+ int fd;
+ std::string tty;
+ char buff[512];
+};
+
+
+#endif