This repository has been archived by the owner on Mar 21, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serial.cpp
339 lines (285 loc) · 9.33 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/****************************************************************
*
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
* X Bonaventure Robotics Laboratory X
* X X
* X serial Version 1.4 X
* X serial.cpp X
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
* Copyright (C) 2001, 2002, 2004 Robert M. Harlan
*
* This program 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; version 2 of the License,
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
*
* You may obtain a copy of the GNU General Public License by writing to
* the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307 USA
*
* Developed by the Computer Science Departmetn, St. Bonaventure University.
* Contributors: Dr. Robert Harlan, Shelley McClarigan, Catherine Mellon,
* Brian C. Zimmel, and Josh Goodberry
* Contact: [email protected]
* http://web.sbu.edu/cs/roboticsLab
*
* This version should write to a linux serial port
* Rev 1.1 Shelley McClarigan
*
* This is a serial port class to handle communication with
* a khepera robot and the host computer. The class can open
* and close the connection to the robot through a specified
* serial port. It also includes a function that sends the
* commands and receives responses from the robot.
*
* Modified from khep_serial.c written by M. von Holzen and
* L. Tettoni and released by Olivier Michel as part of the
* code we orignially got with the khepera.
*
* Rev 1.2 Dr. Robert Harlan
*
* Added default arguments to readline() and Talk() to permit
* return of lines > 64 as needed by vision turret
* Allows buffer lengths to be set by user. (4/22/01)
*
* Rev 1.3 Brian Zimmel, Mike Neel, Josh Goodberry
*
* Added error checking to notify user if a problem has
* occured. (5/24/01)
*
* Rev 1.4 Robert Harlan
* Parameter added to Serial constructor so that verbose status
* set by kRobot (default is false) is passed to Serial object (4/15/04)
*
* Date Created: 2/00
* Last Update: 4/15/04
*****************************************************************/
#include "serial.h"
//Public Functions
//Purpose: Constructor.
//Preconditions: None.
//PostConditions: Sets Verbose to verbose status passed, message size,
// and default timeout and retry.
Serial::Serial(bool verboseStatus)
{
VERBOSE = verboseStatus;
MESSAGE_SIZE = 200;
DEFAULT_TIMEOUT = 5;
DEFAULT_RETRY = 3;
}//end Serial()
/**************************************************************************/
//Purpose: Display all of the debugging messages on the screen,
//Preconditions: None.
//Postconditions: VERBOSE set to true.
void Serial::VerboseOn()
{
VERBOSE = true;
}//end VerboseOn()
/**************************************************************************/
//Purpose: Stops the displaying of all of the debugging messages on the screen.
//Preconditions: None.
//Postconditions: VERBOSE set to false.
void Serial::VerboseOff()
{
VERBOSE = false;
}//end VerboseOff()
/**************************************************************************/
//Purpose: Open connection to Khepera on port passed
// as a parameter e.g., "ttya".
//Preconditions: Port name is defined
//Postconditions: Connection open to given port. Returns true if successful,
// false otherwise.
bool Serial::Open(apstring portname)
{
if (VERBOSE)
cout << "Serial::Entering Serial Open" << endl;
int i;
char buffer[64];
//if (VERBOSE)
//cout << "Serial::buffer=";
// convert apstring to a c string
for (i=0; i<portname.length(); i++){
//if (VERBOSE)
//cout << portname[i];
buffer[i] = portname[i];
}
buffer[i] = '\0';
SERIAL_ID = open(buffer,O_RDWR|O_EXCL);
//cerr << "Serial ID: " << SERIAL_ID << endl;
if (SERIAL_ID == -1)
{
if (VERBOSE)
cout << "Serial::Serial Port Failed to Open --> Serial ID" << endl;
return false;
}
if (!Configure())
return false;
if (!Drain())
return false;
if (Talk("G,0,0\n") == "")
{
if (VERBOSE)
cout << "Serial::Serial Port Failed to Open --> unable to talk" << endl;
return false;
}
if (VERBOSE)
cout << "Serial::Serial Port Opened" << endl;
return true;
}//end Open(apstring)
/**************************************************************************/
//Purpose: Send command to Khepera and return response.
//Preconditions: Commands are in the form of a capital letter,
// followed by any parameters needed, ending with
// a carriage return and line feed.
//Postconditions: Responses are in the form of a lowercase letter
// corresponding to the capital letter command, then
// any data it needs to transmit and a carriage return.
apstring Serial::Talk(apstring send, int size)
{
if (VERBOSE)
cout << "Serial::Entering Serial Talk" << endl;
apstring receive = "";
char cSend[send.length()+1];
int i;
for (i=0; i<send.length(); i++)
cSend[i] = send[i];
cSend[i] = '\0';
if (VERBOSE)
cout << "El Comando enviado al puerto es "<< cSend << endl;
int tries = 0;
do {
//if write succeeds, read response
if (write(SERIAL_ID,cSend,send.length()) == send.length())
{
receive = Readline(size);
if (VERBOSE){
cout << "Serial::receive="<< receive << endl;
cout << "Serial::Talk done" << endl;
}
return receive;
}
tries ++;
} while (tries < DEFAULT_RETRY);
//if reach this point, talk failed
if (VERBOSE)
cout << "Serial::Talk Timeout" << endl;
//perror("Serial::Readline()");
return receive;
}//end Talk(apstring)
/**************************************************************************/
//Purpose: Close the serial connection.
//Preconditions: Connection must be open.
//Postconditions: Connection is closed to port. Returns true if successful,
// false otherwise.
bool Serial::Close()
{
if (VERBOSE)
cout << "Serial::Entering Serial Close "<<SERIAL_ID<< endl;
close(SERIAL_ID);
if (VERBOSE)
cout << "Serial::Serial Line Closed" << endl;
return true;
}//end Close()
/**************************************************************************/
/*********Private Functions**************/
//Purpose: Configures the serial port to read and
// write at 9600 baud, and does a bunch of other
// stuff too. Taken from Olivier's khep_serial.c.
//Preconditions: None.
//Postconditions: Returns true is port is configured correctly,
// false otherwise.
bool Serial::Configure()
{
if (VERBOSE)
cout << "Serial::Entering Serial Configure..."<< endl;
struct termios term;
static int speedkey;
if (tcgetattr(SERIAL_ID, &term) != 0) {
cerr << "Serial::Configure(), getting attributes" << endl;
return false;
}
//speedkey = B9600;
speedkey = B57600;
cfsetispeed(&term, speedkey);
cfsetospeed(&term, speedkey);
clear(term.c_iflag,
IGNBRK|BRKINT|IGNPAR|ISTRIP|ICRNL|INLCR|IXON|IXOFF);
set(term.c_iflag, 0);
clear(term.c_oflag,OPOST);
set(term.c_oflag, 0);
clear(term.c_cflag,CSIZE|PARENB);
set(term.c_cflag,CS8|CSTOPB);
clear(term.c_lflag, ISIG|ICANON|XCASE|ECHO|IEXTEN);
set(term.c_lflag, 0);
term.c_cc[VMIN] = 0;
term.c_cc[VTIME] = DEFAULT_TIMEOUT;
if(tcsetattr(SERIAL_ID, TCSANOW, &term) != 0) {
cerr << "Serial::Configure(), setting attributes" << endl;
return false;
}
if (VERBOSE)
cout << "Serial::Configured" << endl;
return true;
}//end Configure()
/**************************************************************************/
//Purpose: Reads a line from the robot.
//Preconditions: None.
//Postconditions: Returns the size of the line it wrote.
apstring Serial::Readline(int size)
{
if (VERBOSE)
cout << "Serial::Entering Serial Readline..." << endl;
char buffer[size+1];
int nrd,rsize=0;
char c;
if (VERBOSE)
cout << "Serial::buffer=";
do {
nrd = read(SERIAL_ID, &c, 1);
if (nrd < 0)
break;
buffer[rsize] = c;
if (VERBOSE)
cout << c;
rsize += nrd;
} while (nrd ==1 && rsize < (size-1) && c != '\n');
if (VERBOSE)
cout << endl;
buffer[rsize] = '\0';
if (nrd<0)
{
if (VERBOSE)
cout << "ERROR IN Serial::Readline()" << endl;
return "";
//perror("Serial::Readline()");
}
else
{
if (VERBOSE)
cout << "Serial::Read line successfully" << endl;
//apstring response(buffer);
//return response;
return buffer;
}
}//end ReadLine()
/**************************************************************************/
//Purpose: Drains the input buffer.
//Preconditions: None.
//Postconditions: Serial line drained. Returns true if successful,
// false otherwise.
bool Serial::Drain()
{
if(VERBOSE)
cout << "Serial::Entering Serial Drain..." << endl;
char answer[MESSAGE_SIZE];
apstring temp;
int rsize;
do {
temp = Readline();
} while (temp.length()>0);
if (VERBOSE)
cout << "Serial::Serial line Drained" << endl;
return true;
}