-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathSerialPort.cpp
More file actions
177 lines (137 loc) · 3.74 KB
/
SerialPort.cpp
File metadata and controls
177 lines (137 loc) · 3.74 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
#include <stdio.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <string>
#include <cstring>
#include <unistd.h>
#include <termios.h>
#include <fcntl.h>
#include <errno.h>
#include <iostream>
#include <stdexcept>
#include <thread>
#include <chrono>
class SerialPort {
private:
int ReadBufferSize;
int timeout;
char terminationChar;
bool termination;
std::string id;
int baudRate;
int fd;
struct termios tty;
int err;
public:
SerialPort(int baudRate, std::string id)
{
Init(baudRate, id);
}
void Init(int baudRate, std::string id) {
int USB = open(id.c_str(), O_RDWR| O_NOCTTY);
if (USB < 0)
{
std::cerr << "Could not open " << id.c_str() << " as a TTY:";
perror("");
throw std::runtime_error("");
}
memset(&tty, 0, sizeof(tty));
this->baudRate = baudRate;
this->id = id;
cfsetospeed(&tty, (speed_t)baudRate);
cfsetispeed(&tty, (speed_t)baudRate);
tty.c_cflag &= ~PARENB;
tty.c_cflag &= ~CSTOPB;
tty.c_cflag &= ~CSIZE;
tty.c_cflag |= CS8;
//tty.c_cflag = CS8|CREAD|CLOCAL;
//tty.c_cflag &= ~CRTSCTS;
tty.c_cc[VMIN] = 1;
tty.c_cc[VTIME] = 10;
tty.c_cflag |= CREAD | CLOCAL;
cfmakeraw(&tty);
this->fd = USB;
tcflush(this->fd, TCIOFLUSH);
if(tcsetattr(USB,TCSANOW,&tty) != 0) std::cout << "Failed to initialize serial." << std::endl;
}
void SetReadBufferSize(int size) {
this->ReadBufferSize = size;
}
void SetTimeout(int timeout) {
this->timeout = timeout;
tty.c_cc[VTIME] = timeout*10;
cfmakeraw(&tty);
if(tcsetattr(this->fd,TCSANOW,&tty) != 0) std::cout << "Failed to initialize serial in SetTimeout." << std::endl;;
}
void EnableTermination(char c) {
this->termination = true;
this->terminationChar = c;
}
void Flush() {
tcflush(this->fd, TCOFLUSH);
}
void Write(char *data, int length) {
int n_written = 0, spot = 0;
do {
n_written = write( this->fd, &data[spot], length );
if (n_written > 0)
spot += n_written;
} while (data[spot-1] != terminationChar);
}
int GetBytesReceived() {
int bytes_avail;
ioctl(this->fd, FIONREAD, &bytes_avail);
return bytes_avail;
}
int Read(char *data, int size) {
int n = 0, loc = 0;
char buf = '\0';
memset(data, '\0', size);
err = 0;
do {
n = read(this->fd, &buf, 1);
sprintf( &data[loc], "%c", buf );
loc += n;
if(n == 0) err++;
if(err > 100)
{
err = 0;
Reset();
Close();
std::this_thread::sleep_for(std::chrono::milliseconds(30000));
Init(this->baudRate, this->id);
SetTimeout(this->timeout);
SetReadBufferSize(this->ReadBufferSize);
EnableTermination(this->terminationChar);
Reset();
break;
}
} while( buf != terminationChar && loc < size);
if (n < 0) {
std::cout << "Error reading: " << strerror(errno) << std::endl;
}
else if (n == 0) {
std::cout << "Read nothing!" << std::endl;
}
else {
//std::cout << "Response: " << data << std::endl;
}
return loc;
}
void WaitForData()
{
fd_set readfds;
struct timeval tv;
FD_ZERO(&readfds);
FD_SET(this->fd, &readfds);
tv.tv_sec = 0;
tv.tv_usec = 100000;
select(this->fd + 1, &readfds, NULL, NULL, &tv);
}
void Reset() {
tcflush(this->fd, TCIOFLUSH);
}
void Close() {
close(this->fd);
}
};