forked from e-puck2/monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSerialComm.cpp
More file actions
154 lines (138 loc) · 4.12 KB
/
Copy pathSerialComm.cpp
File metadata and controls
154 lines (138 loc) · 4.12 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
/*
* SerialComm.cpp
* EPuckMonitor
*
* Created by Stefano Morgani on 11/18/08.
*
* Copyright 2008 GCtronic
*
* This file is part of EPuckMonitor.
*
* EPuckMonitor 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 2 of the License, or
* (at your option) any later version.
*
* EPuckMonitor 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 EPuckMonitor; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include "SerialComm.h"
SerialComm::SerialComm() {
}
SerialComm::~SerialComm() {
}
int SerialComm::connect(char *path) {
int err = 0;
speed_t baud_rate = B115200;
// open the USB stream
fd = open(path, O_RDWR | O_NONBLOCK | O_NDELAY);
if(fd==-1){
return -1;
}
// get the baud rate and the flags
struct termios term;
tcgetattr(fd, &term);
err = cfsetispeed(&term, baud_rate);
if(err != 0) {
std::cerr << "error setting input speed" << std::endl;
}
err = cfsetospeed(&term, baud_rate);
if(err != 0) {
std::cerr << "error setting output speed" << std::endl;
}
term.c_iflag = 0;
term.c_oflag = 0;
term.c_cflag = CLOCAL | CREAD | CS8;
term.c_lflag = 0;
term.c_cc[ VTIME ] = 0; //timeout in tenths of a second
term.c_cc[ VMIN ] = 0; //no wait for any char
if( tcsetattr(fd, TCSAFLUSH, &term) < 0) //termios flags setting
{
perror("tcsetattr: ");
}
return 0;
}
void SerialComm::disconnect() {
tcflush(fd,TCIOFLUSH);
close(fd);
}
void SerialComm::flush() {
tcflush(fd,TCIOFLUSH);
}
int SerialComm::writeData(char *buf, int num_bytes, uint32_t usleep_time) {
tcflush(fd,TCIOFLUSH); //flush the input and output buffers before writing new data/commands
int bytes_written=0;
int current_bytes_written=0;
errno=0;
uint32_t timeout = usleep_time/70; //wait 70 useconds between every read; the loop lasts for usleep_time/70 times
while(bytes_written<num_bytes) {
current_bytes_written=write(fd, &buf[bytes_written], num_bytes-bytes_written);
if(current_bytes_written>=0) { //if write ok
bytes_written+=current_bytes_written;
} //else {
//FILE *fp = fopen("write_error.txt", "a");
//fprintf(fp, "Error writing to device: %s\n", strerror(errno));
//fclose(fp);
//errno=0;
//}
if(bytes_written<num_bytes) {
usleep(70); //1/(115200/8) = 70 useconds per byte
timeout--;
}
if(timeout==0) {
break;
}
}
return bytes_written;
}
int SerialComm::readData(char *buf, int num_bytes, uint32_t usleep_time) {
int bytes_red=0;
int current_bytes_red=0;
errno=0;
uint32_t timeout = usleep_time/70; //wait 70 useconds between every read; the loop lasts for usleep_time/70 times
while(bytes_red<num_bytes) {
current_bytes_red=read(fd,&buf[bytes_red],num_bytes-bytes_red);
if(current_bytes_red>=0) { //if read ok
bytes_red+=current_bytes_red;
} //else {
//FILE *fp = fopen("read_error.txt", "a");
//fprintf(fp, "Error reading from device: %s (code %d)\n", strerror(errno), errno);
//fclose(fp);
//errno=0;
//}
if(bytes_red<num_bytes) { //wait to receive something new
usleep(70); //1/(115200/8) = 70 useconds per byte
timeout--;
}
if(timeout==0) {
break;
}
}
return bytes_red;
}
void SerialComm::discard(int num_bytes) {
char *temp = (char *)malloc(num_bytes*sizeof(char));
int bytes_red=0;
int current_bytes_red=0;
errno=0;
while(bytes_red<num_bytes) {
current_bytes_red=read(fd,&temp[bytes_red],num_bytes-bytes_red);
if(current_bytes_red>=0) { //if read ok
bytes_red+=current_bytes_red;
} //else {
//FILE *fp = fopen("discard_error.txt", "a");
//fprintf(fp, "Error discarding data: %s\n", strerror(errno));
//fclose(fp);
//errno=0;
//}
}
free(temp);
return;
}