summaryrefslogtreecommitdiff
path: root/plugins/common/serialport.hpp
blob: 1d5038e64c4b771b34609da9b32048756354a4d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#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>
#include <stdio.h>

class SerialPort: public AbstractIo
{
private:
	speed_t speed;

public:
	SerialPort()
		:fd(0), speed(B9600)
	{

	}

	SerialPort(int fileDesc)
		:fd(fileDesc)
	{
		speed = B9600;

		setDescriptor(fd);
	}

	SerialPort(std::string _tty)
		:tty(_tty), fd(0)
	{
		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 ("<<fd<<")"<<endl;
			perror("write error: ");
		}
	}

	void setDescriptor(int d)
	{
		fd = d;
		//setupDevice();
	}

private: ///methods

	bool setupDevice()
	{
		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, speed);
		cfsetospeed(&oldtio, speed);

		tcflush(fd, TCIFLUSH);
		tcsetattr(fd, TCSANOW, &oldtio);

		fcntl(fd,F_SETFL,O_NONBLOCK);

		return true;
	}

private:
	int fd;
	std::string tty;

};


#endif