-
Notifications
You must be signed in to change notification settings - Fork 1
/
serial.cpp
184 lines (169 loc) · 3.9 KB
/
serial.cpp
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include <iostream>
#include <string.h>
#include <dirent.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <sys/epoll.h>
#include "serial.h"
#include "types.h"
#include "log.h"
bool SerialPort::open(const string &path)
{
m_device = path;
return open();
}
bool SerialPort::open()
{
m_ttyfd = ::open(m_device.c_str(), O_RDWR | O_NOCTTY | O_NONBLOCK);
if (m_ttyfd < 0)
{
m_isopen = false;
return false;
}
m_isopen = true;
setUart();
return true;
}
void SerialPort::setUart()
{
struct termios tio;
struct termios settings;
int retval;
memset(&tio, 0, sizeof(tio));
tio.c_iflag = 0;
tio.c_oflag = 0;
tio.c_cflag = CS8 | CREAD | CLOCAL; // 8n1, see termios.h for more information
tio.c_lflag = 0;
tio.c_cc[VMIN] = 1;
tio.c_cc[VTIME] = 5;
cfsetospeed(&tio, B115200); // 115200 baud
cfsetispeed(&tio, B115200); // 115200 baud
tcsetattr(m_ttyfd, TCSANOW, &tio);
retval = tcgetattr(m_ttyfd, &settings);
if (-1 == retval)
{
LOGE << "setUart error" << endl;
return;
}
cfmakeraw(&settings);
settings.c_cflag |= CREAD | CLOCAL;
tcflush(m_ttyfd, TCIOFLUSH);
tcsetattr(m_ttyfd, TCSANOW, &settings);
}
bool SerialPort::isOpen() const
{
return m_isopen;
}
int SerialPort::write(const void *data, int length)
{
int ret;
struct epoll_event ev;
struct epoll_event events[1];
int epfd = epoll_create(1);
if (epfd < 0)
{
LOGE << "epoll create failed" << endl;
return -1;
}
#ifndef EPOLLRDHUP
#define EPOLLRDHUP 0x2000
#endif
ev.events = EPOLLOUT | EPOLLRDHUP;
ev.data.fd = m_ttyfd;
epoll_ctl(epfd, EPOLL_CTL_ADD, m_ttyfd, &ev);
int num = epoll_wait(epfd, events, 1, timeout);
if (num > 0)
{
if (events[0].events & EPOLLOUT)
{
ret = ::write(m_ttyfd, data, length);
if (ret == length)
ret = 0;
else
{
LOGI << "write incomplete or encounter error, length=" << length << ", ret = " << ret << endl;
ret = -1;
}
}
else
{
LOGI << "epoll event " << hex << events[0].events << dec << endl;
ret = -1;
}
}
else if (num == 0)
{
/* TIMEOUT */
LOGI << "epoll wait timeout" << endl;
ret = -1;
}
else
{
LOGI << "epoll_wait error" << endl;
ret = -1;
}
::close(epfd);
return ret;
}
int SerialPort::read(void *data, int length)
{
return read(data, length, NULL);
}
int SerialPort::read(void *data, int length, int *reallen)
{
int ret;
struct epoll_event ev;
struct epoll_event events[1];
int epfd = epoll_create(1);
if (epfd < 0)
{
LOGE << "epoll create failed" << endl;
return -1;
}
if (reallen)
*reallen = 0;
ev.events = EPOLLIN | EPOLLRDHUP;
ev.data.fd = m_ttyfd;
epoll_ctl(epfd, EPOLL_CTL_ADD, m_ttyfd, &ev);
int num = epoll_wait(epfd, events, 1, timeout);
if (num > 0)
{
if (events[0].events & EPOLLIN)
{
ret = ::read(m_ttyfd, data, length);
if (reallen)
*reallen = ret;
if (ret == length)
ret = 0;
else
{
LOGI << "warnning: want read " << length << " bytes actually read " << ret << " bytes" << std::endl;
ret = -1;
}
}
else
{
LOGI << "epoll event " << hex << events[0].events << dec << endl;
ret = -1;
}
}
else if (num == 0)
{
/* TIMEOUT */
LOGI << "epoll wait timeout" << endl;
ret = -1;
}
else
{
LOGI << "epoll_wait error" << endl;
ret = -1;
}
::close(epfd);
return ret;
}
void SerialPort::close()
{
::close(m_ttyfd);
m_isopen = false;
}