-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathserialdev.cpp
More file actions
177 lines (157 loc) · 5.44 KB
/
Copy pathserialdev.cpp
File metadata and controls
177 lines (157 loc) · 5.44 KB
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
/*
(C) Copyright 2015 Jeremy Burton
This file is part of Sark-100-antenna-analyzer.
Sark-100-antenna-analyzer is free software: you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
Sark-100-antenna-analyzer is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty
of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Foobar. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#ifndef WIN32
#include <poll.h>
#include <termios.h>
#include <sys/ioctl.h>
#endif
#include <fcntl.h>
#include "config.h"
#include "serialdev.h"
SerialDev::SerialDev(const char *dev,int speed)
{
rxbufflen=0;
devname = dev;
devfd = open(dev, O_RDWR | O_NOCTTY | O_NDELAY);
if (devfd != -1)
{
struct termios termattr;
tcgetattr(devfd, &termattr);
cfmakeraw(&termattr);
printf("\tc_cflag=%x\n",termattr.c_cflag);
termattr.c_oflag &= ~(ONLCR);
//#if defined(__APPLE__) && defined(__MACH__)
termattr.c_cflag &= ~(CRTSCTS|PARENB|CSTOPB);
//#else
// termattr.c_cflag &= ~(CRTSCTS|PARENB|CSTOPB|CBAUD);
//#endif
termattr.c_cflag |= CS8 | CLOCAL;
printf("\tc_cflag=%x\n",termattr.c_cflag);
switch (speed)
{
case 300: cfsetspeed(&termattr, B300); break;
case 600: cfsetspeed(&termattr, B600); break;
case 1200: cfsetspeed(&termattr, B1200); break;
case 2400: cfsetspeed(&termattr, B2400); break;
case 4800: cfsetspeed(&termattr, B4800); break;
case 9600: cfsetspeed(&termattr, B9600); break;
case 19200: cfsetspeed(&termattr, B19200); break;
case 38400: cfsetspeed(&termattr, B38400); break;
case 57600: cfsetspeed(&termattr, B57600); break;
case 115200: cfsetspeed(&termattr, B115200); break;
}
printf("\tc_cflag=%x\n",termattr.c_cflag);
tcsetattr(devfd, TCSANOW, &termattr);
}
printf("\tdevfd %d\n",devfd);
}
SerialDev::~SerialDev()
{
if (devfd != -1)
close(devfd);
}
int SerialDev::RxFlush()
{
pollfd fds[] = {{devfd, POLLIN, 0 }};
int timeout = 50; //500msec, move somewhere
int pstatus,rstatus;
printf("%ld> RxFlush\n",time(NULL));
for (rxbufflen=0;;)
{
pstatus = poll(fds, 1, timeout);
if (pstatus > 0) //Success
{
if (fds[0].revents & (POLLERR|POLLHUP|POLLNVAL))
return 3; //Error, eof, hup etc quit
else
if (fds[0].revents & POLLIN) // There's some data to read
{
rstatus = read(devfd, rxbuff+rxbufflen, 1);
if (rstatus == -1)
{
if (errno == EAGAIN) // Treat would block error as a timeout
return 1; //Timeout
else
return 4; //Error reading
}
else
{
//printf("%ld> RxFlush: %c\n",time(NULL),rxbuff[rxbufflen]);
}
}
}
else if (pstatus == 0) //Timeout
return 1;
else if (pstatus == -1) //Error polling
return 2;
}
}
int SerialDev::RxLine()
{
pollfd fds[] = {{devfd, POLLIN, 0 }};
int timeout = 4000; //10sec, move somewhere
int pstatus,rstatus;
for (rxbufflen=0;;)
{
pstatus = poll(fds, 1, timeout);
if (pstatus > 0) //Success
{
if (fds[0].revents & (POLLERR|POLLHUP|POLLNVAL))
return 3; //Error, eof, hup etc quit
else
if (fds[0].revents & POLLIN) // There's some data to read
{
rstatus = read(devfd, rxbuff+rxbufflen, 1);
if (rstatus == -1)
{
if (errno == EAGAIN) // Treat would block error as a timeout
return 1; //Timeout
else
return 4; //Error reading
}
else
{
//printf("%ld> RxLine: c=%c (0x%02x)\n",time(NULL),rxbuff[rxbufflen],rxbuff[rxbufflen]);
switch (rxbuff[rxbufflen])
{
case '\r': break;
case '\n':
rxbuff[rxbufflen] = '\0';
return 0;
default:
rxbufflen += rstatus;
break;
}
}
}
}
else if (pstatus == 0) //Timeout
return 1;
else if (pstatus == -1) //Error polling
return 2;
}
}
bool SerialDev::IsUp()
{
return devfd != -1;
}
void SerialDev::TxData(const void *buf, size_t count)
{
write(devfd,buf,count);
}