|
From: <bo...@us...> - 2008-02-22 02:58:15
|
Revision: 77
http://gearbox.svn.sourceforge.net/gearbox/?rev=77&view=rev
Author: borax00
Date: 2008-02-21 18:58:17 -0800 (Thu, 21 Feb 2008)
Log Message:
-----------
Applied changes from Michael's review.
Modified Paths:
--------------
gearbox/trunk/submitted/gbxserialacfr/doc.dox
gearbox/trunk/submitted/gbxserialacfr/lockfile/lockfile.cpp
gearbox/trunk/submitted/gbxserialacfr/serial.cpp
gearbox/trunk/submitted/gbxserialacfr/serial.h
gearbox/trunk/submitted/gbxserialacfr/test/serialechotest.cpp
gearbox/trunk/submitted/gbxsickacfr/doc.dox
Modified: gearbox/trunk/submitted/gbxserialacfr/doc.dox
===================================================================
--- gearbox/trunk/submitted/gbxserialacfr/doc.dox 2008-02-20 00:45:24 UTC (rev 76)
+++ gearbox/trunk/submitted/gbxserialacfr/doc.dox 2008-02-22 02:58:17 UTC (rev 77)
@@ -25,6 +25,11 @@
#include <gbxserialacfr/serial.h>
@endverbatim
+@par Example
+ See:
+ - test/serialechotest.cpp
+ - test/serialloopbacktest.cpp
+
@par Other serial libraries
- http://libserial.sourceforge.net (GPL)
@@ -33,14 +38,18 @@
@par Tested On
- rs232 only (ie not rs422)
+ - Not tested at 500k baud
- For USB-to-serial devices, tested on FTDI-based devices only
+@par Style
+ See http://orca-robotics.sourceforge.net/orca/orca_doc_style.html
+
@par Copyright
Alex Brooks, Mathew Ridley
@par Responsible Developer
Alex Brooks
-
+
*/
/*!
Modified: gearbox/trunk/submitted/gbxserialacfr/lockfile/lockfile.cpp
===================================================================
--- gearbox/trunk/submitted/gbxserialacfr/lockfile/lockfile.cpp 2008-02-20 00:45:24 UTC (rev 76)
+++ gearbox/trunk/submitted/gbxserialacfr/lockfile/lockfile.cpp 2008-02-22 02:58:17 UTC (rev 77)
@@ -110,7 +110,7 @@
stringstream ss; ss << "device " << dev << " is already locked by process PID " << pidOfLocker;
throw LockFileException( ss.str() );
}
- else
+ else if ( errno == ESRCH )
{
// The process which owns the lock no longer exists. Clean up.
ret = unlink( lbuf );
@@ -130,7 +130,13 @@
throw LockFileException( ss.str() );
}
}
- }
+ else
+ {
+ stringstream ss;
+ ss << "lock_dev: Don't expect to see this errno after kill: " << strerror(errno);
+ throw LockFileException( ss.str() );
+ }
+ }
ret = unlink(pbuf);
if ( ret < 0 )
@@ -176,7 +182,11 @@
LockFile::~LockFile()
{
- unlock_dev( dev_.c_str(), lockPid_ );
+ // Don't allow exceptions from destructor
+ try {
+ unlock_dev( dev_.c_str(), lockPid_ );
+ }
+ catch ( ... ) {}
}
}
Modified: gearbox/trunk/submitted/gbxserialacfr/serial.cpp
===================================================================
--- gearbox/trunk/submitted/gbxserialacfr/serial.cpp 2008-02-20 00:45:24 UTC (rev 76)
+++ gearbox/trunk/submitted/gbxserialacfr/serial.cpp 2008-02-22 02:58:17 UTC (rev 77)
@@ -8,6 +8,7 @@
*
*/
+#include <termios.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/ioctl.h>
@@ -142,7 +143,7 @@
#endif
default:
stringstream ss;
- ss << "Serial::baud() Invalid baud rate: " << baudRate;
+ ss << "Serial::"<<__func__<<"() Invalid baud rate: " << baudRate;
throw SerialException( ss.str() );
}
}
@@ -215,7 +216,7 @@
return 4000000;
default:
stringstream ss;
- ss << "Serial::baud() Invalid baud rate: " << baudRate;
+ ss << "Serial::"<<__func__<<"() Invalid baud rate: " << baudRate;
throw SerialException( ss.str() );
}
}
@@ -371,7 +372,7 @@
setBaudRate( baudRate );
if(debugLevel_ > 2){
- cout << "At end of Serial::Serial: " << getStatusString();
+ cout << "At end of Serial::Serial(): " << getStatusString();
}
}
catch ( const SerialException &e )
@@ -404,17 +405,27 @@
Serial::close()
{
if ( debugLevel_ > 0 )
- cout<<"TRACE(serial.cpp): close()" << endl;
+ {
+ // AlexB: Not sure if cout::operator<< can throw, be careful just in case.
+ try {
+ cout<<"TRACE(serial.cpp): "<<__func__<<"()" << endl;
+ }
+ catch ( ... ) {}
+ }
assert( portFd_ != -1 );
if(tcdrain(portFd_))
{
- perror("Serial::close():tcdrain()");
+ stringstream ss;
+ ss << "Serial::"<<__func__<<"():tcdrain()";
+ perror(ss.str().c_str());
}
if (::close(portFd_))
{
- perror("Serial::close():close()");
+ stringstream ss;
+ ss << "Serial::"<<__func__<<"():close()";
+ perror(ss.str().c_str());
}
// Make sure that we force this back to an invalid state
@@ -427,16 +438,18 @@
struct termios localOptions;
if ( debugLevel_ > 0 )
- cout<<"TRACE(serial.cpp): setBaudRate("<<baud<<")" << endl;
+ cout<<"TRACE(serial.cpp): "<<__func__<<"("<<baud<<")" << endl;
if(portFd_==-1)
{
- throw SerialException( "Serial:baud() no valid device open" );
+ stringstream ss;
+ ss << "Serial::"<<__func__<<"(): no valid device open";
+ throw SerialException( ss.str() );
}
if(tcgetattr(portFd_, &localOptions) == -1)
{
stringstream ss;
- ss << "Serial::baud():tcgetattr() Error reading attr: " << strerror(errno);
+ ss << "Serial::"<<__func__<<"():tcgetattr() Error reading attr: " << strerror(errno);
throw SerialException( ss.str() );
}
@@ -458,7 +471,7 @@
if (ioctl(portFd_, TIOCGSERIAL, &serinfo) < 0)
{
stringstream ss;
- ss << "Serial::setBaudRate("<<baud<<"): error calling 'ioctl(portFd_, TIOCGSERIAL, &serinfo)': "<<strerror(errno);
+ ss << "Serial::"<<__func__<<"("<<baud<<"): error calling 'ioctl(portFd_, TIOCGSERIAL, &serinfo)': "<<strerror(errno);
throw SerialException( ss.str() );
}
@@ -468,7 +481,7 @@
if (ioctl(portFd_, TIOCSSERIAL, &serinfo) < 0)
{
stringstream ss;
- ss << "Serial::setBaudRate("<<baud<<"): error calling 'ioctl(portFd_, TIOCSSERIAL, &serinfo)': "<<strerror(errno);
+ ss << "Serial::"<<__func__<<"("<<baud<<"): error calling 'ioctl(portFd_, TIOCSSERIAL, &serinfo)': "<<strerror(errno);
throw SerialException( ss.str() );
}
}
@@ -476,7 +489,7 @@
if ( tcsetattr(portFd_, TCSAFLUSH, &localOptions) == -1 )
{
stringstream ss;
- ss << "Serial::baud():tcsetattr() Error setting attr: " << strerror(errno);
+ ss << "Serial::"<<__func__<<"():tcsetattr() Error setting attr: " << strerror(errno);
throw SerialException( ss.str() );
}
}
@@ -493,7 +506,7 @@
if ( portFd_ == -1 )
{
stringstream ss;
- ss << "Serial::open(): failed to open '"<<dev_<<"': "<<strerror(errno);
+ ss << "Serial::"<<__func__<<"(): failed to open '"<<dev_<<"': "<<strerror(errno);
throw SerialException( ss.str() );
}
@@ -502,16 +515,14 @@
{
close();
stringstream ss;
- ss << "Serial::open(): tcgetattr() failed for '"<<dev_<<"': "<<strerror(errno);
+ ss << "Serial::"<<__func__<<"(): tcgetattr() failed for '"<<dev_<<"': "<<strerror(errno);
throw SerialException( ss.str() );
}
if(debugLevel_ > 2){
- cout << "At beginning of open(): "<<getStatusString()<<endl;
+ cout << "At beginning of "<<__func__<<"(): "<<getStatusString()<<endl;
}
-
-
// enable receiver & ignore control lines
localOptions.c_cflag |= (CLOCAL | CREAD) ;
@@ -541,31 +552,32 @@
{
close();
stringstream ss;
- ss << "Serial::open(): tcsetattr() failed for '"<<dev_<<"': "<<strerror(errno);
+ ss << "Serial::"<<__func__<<"(): tcsetattr() failed for '"<<dev_<<"': "<<strerror(errno);
throw SerialException( ss.str() );
}
if ( debugLevel_ > 2 ){
- cout << "At end of open():" << getStatusString();
+ cout << "At end of "<<__func__<<"():" << getStatusString();
}
-
}
int
Serial::read(void *buf, int count)
{
if ( debugLevel_ > 0 )
- cout<<"TRACE(serial.cpp): read()" << endl;
+ cout<<"TRACE(serial.cpp): "<<__func__<<"()" << endl;
int got = ::read(portFd_, buf, count);
if ( got < 0 )
{
- throw SerialException( string("Serial::read(): ") + strerror(errno) );
+ stringstream ss;
+ ss << "Serial::"<<__func__<<"(): " << strerror(errno);
+ throw SerialException( ss.str() );
}
if ( debugLevel_ > 1 )
{
- cout<<"TRACE(serial.cpp): read: '";
+ cout<<"TRACE(serial.cpp): just read: '";
for ( int i=0; i < count; i++ )
cout << ((char*)(buf))[i];
cout << "'" << endl;
@@ -580,7 +592,7 @@
Serial::readFull(void *buf, int count)
{
if ( debugLevel_ > 0 )
- cout<<"TRACE(serial.cpp): readFull(): count=" << count << endl;
+ cout<<"TRACE(serial.cpp): "<<__func__<<"(): count=" << count << endl;
char* bufPtr = static_cast<char*>(buf);
@@ -605,7 +617,9 @@
else
{
- throw SerialException( std::string("Serial::readFullWithTimeout: read(): ")+strerror(errno) );
+ stringstream ss;
+ ss << "Serial::"<<__func__<<": read(): "<<strerror(errno);
+ throw SerialException( ss.str() );
}
}
@@ -620,7 +634,7 @@
Serial::readLine(void *buf, int count, char termchar)
{
if ( debugLevel_ > 0 ){
- cout<<"TRACE(serial.cpp): readLine ";
+ cout<<"TRACE(serial.cpp): "<<__func__<<"(): ";
if(timeoutsEnabled_){
cout << "timeouts enabled"<<endl;
}else{
@@ -639,7 +653,9 @@
//Check for buf overrun Must leave room for NULL terminator
if ( dataPtr >= bufPtr + (count - 1) )
{
- throw SerialException( "Serial::readLine: Not enough room in buffer" );
+ stringstream ss;
+ ss << "Serial::"<<__func__<<": Not enough room in buffer";
+ throw SerialException( ss.str() );
}
int ret = ::read( portFd_, &nextChar, 1 );
@@ -662,7 +678,9 @@
}
//If we get here then it was a more serious error
- throw SerialException( std::string( "Serial::readLine(): ")+strerror(errno) );
+ stringstream ss;
+ ss << "Serial::"<<__func__<<"(): "<<strerror(errno);
+ throw SerialException( ss.str() );
}
} while (nextChar != termchar);
@@ -685,7 +703,9 @@
if(ret==-1)
{
- throw SerialException( std::string("Serial::bytesAvailable(): ")+strerror(errno) );
+ stringstream ss;
+ ss << "Serial::"<<__func__<<": "<< strerror(errno);
+ throw SerialException( ss.str() );
}
return n_read;
}
@@ -718,7 +738,9 @@
}
if(selval<0)
{
- throw SerialException( std::string("Serial::waitForTimeout: select(): ")+strerror(errno) );
+ stringstream ss;
+ ss << "Serial::"<<__func__<<": select(): "<<strerror(errno);
+ throw SerialException( ss.str() );
}
return GOT_DATA;
@@ -729,24 +751,28 @@
Serial::writeString(const char *str)
{
if ( debugLevel_ > 0 )
- cout<<"TRACE(serial.cpp): writeString(): writing '"<<str<<"'" << endl;
+ cout<<"TRACE(serial.cpp): "<<__func__<<"(): writing '"<<str<<"'" << endl;
int put;
put = ::write(portFd_, str, strlen(str) );
if ( put < 0 )
{
- throw SerialException( string("Serial::write(): ")+strerror(errno) );
+ stringstream ss;
+ ss << "Serial::"<<__func__<<"(): " << strerror(errno);
+ throw SerialException( ss.str() );
}
else if ( put == 0 )
{
- throw SerialException( "Serial::writeString(): ::write() returned 0" );
+ stringstream ss;
+ ss << "Serial::"<<__func__<<"(): ::write() returned 0";
+ throw SerialException( ss.str() );
}
else if ( put < (int)(strlen(str)) )
{
// AlexB: Not sure what to do here... This can happen eg if the buffer is full.
// I'm not convinced that we want to throw an exception, but chances are
// lots of users won't check the return code.
- cout << "WARNING: Serial::writeString: only wrote " << put << " of " << strlen(str) << " bytes." << endl;
+ cout << "WARNING: Serial::"<<__func__<<": only wrote " << put << " of " << strlen(str) << " bytes." << endl;
}
if ( debugLevel_ > 1 )
@@ -765,7 +791,9 @@
if(tcgetattr(portFd_, &status) == -1)
{
close();
- throw SerialException( std::string("Serial::getStatusString(): tcgetattr():")+strerror(errno) );
+ stringstream ss;
+ ss << "Serial::"<<__func__<<"(): tcgetattr():"<<strerror(errno);
+ throw SerialException( ss.str() );
}
stringstream ss;
@@ -778,7 +806,7 @@
if (ioctl(portFd_, TIOCGSERIAL, &serinfo) < 0)
{
stringstream ss;
- ss << "Serial::getStatusString(): error calling 'ioctl(portFd_, TIOCGSERIAL, &serinfo)': "<<strerror(errno);
+ ss << "Serial::"<<__func__<<"(): error calling 'ioctl(portFd_, TIOCGSERIAL, &serinfo)': "<<strerror(errno);
throw SerialException( ss.str() );
}
ss << serinfo;
@@ -793,7 +821,9 @@
int ret = tcflush(portFd_,TCIOFLUSH);
if ( ret < 0 )
{
- throw SerialException( std::string("Serial::flush(): ")+strerror(errno) );
+ stringstream ss;
+ ss << "Serial::"<<__func__<<"(): "<<strerror(errno);
+ throw SerialException( ss.str() );
}
}
@@ -803,7 +833,9 @@
// wait till all output sent
if(tcdrain(portFd_))
{
- throw SerialException( std::string("Serial::drain(): tcdrain: ")+strerror(errno) );
+ stringstream ss;
+ ss << "Serial::"<<__func__<<"(): tcdrain: "<<strerror(errno);
+ throw SerialException( ss.str() );
}
}
@@ -811,19 +843,27 @@
Serial::write(const void *buf, int count)
{
if ( debugLevel_ > 0 )
- cout<<"TRACE(serial.cpp): write()" << endl;
+ cout<<"TRACE(serial.cpp): "<<__func__<<"()" << endl;
if ( count == 0 )
- throw SerialException( "Serial::write() was called with zero bytes" );
+ {
+ stringstream ss;
+ ss << "Serial:: "<<__func__<<"() was called with zero bytes";
+ throw SerialException( ss.str() );
+ }
int put = ::write(portFd_, buf, count);
if ( put < 0 )
{
- throw SerialException( string("Serial::write(): ")+strerror(errno) );
+ stringstream ss;
+ ss << "Serial:: "<<__func__<<"(): "<<strerror(errno);
+ throw SerialException( ss.str() );
}
else if ( put == 0 )
{
- throw SerialException( "Serial::write(): ::write() returned 0" );
+ stringstream ss;
+ ss << "Serial::"<<__func__<<"(): ::write() returned 0";
+ throw SerialException( ss.str() );
}
if ( debugLevel_ > 1 )
{
Modified: gearbox/trunk/submitted/gbxserialacfr/serial.h
===================================================================
--- gearbox/trunk/submitted/gbxserialacfr/serial.h 2008-02-20 00:45:24 UTC (rev 76)
+++ gearbox/trunk/submitted/gbxserialacfr/serial.h 2008-02-22 02:58:17 UTC (rev 77)
@@ -10,9 +10,6 @@
#ifndef GBXSERIALACFR_SERIAL_H
#define GBXSERIALACFR_SERIAL_H
-#include <termios.h>
-#include <sys/types.h>
-#include <fcntl.h>
#include <string>
#include <gbxserialacfr/uncopyable.h>
#include <gbxserialacfr/lockfile/lockfile.h>
@@ -63,7 +60,7 @@
//! Destructor closes serial port
~Serial();
- //! turn on/off debug messages
+ //! Debug messages are printed to stdout. debugLevel should be in the range [0,3].
void setDebugLevel( int debugLevel ) { debugLevel_ = debugLevel; }
//! Sets the baud rate. Flushes any data.
@@ -73,10 +70,8 @@
void setTimeout(int sec, int usec);
//! Reads up to @ref count bytes into buffer @ref buf.
- //! Returns the number of bytes read.
- //! Will never return <0 -- throws exceptions instead.
+ //! Returns the number of bytes read, or '-1' on timeout (if timeouts are enabled).
//! If timeouts are not enabled, blocks till it gets something.
- //! If timeouts are enabled, throws an exception if data isn't available.
int read(void *buf, int count);
//! Tries to read exactly @ref count bytes into @ref buf.
@@ -99,7 +94,7 @@
//! char buf[6];
//! serial.readLine( buf, 6 );
//!
- //! where the two extra characters are for the '\n' and the terminating '\0'.
+ //! where the two extra characters are for the "\n" and the terminating "\0".
//!
//! If timeouts are not enabled we might block forever, waiting for the number of bytes we want or an error.
//!
@@ -122,7 +117,7 @@
//! Writes some data. Returns the number of bytes written.
int write(const void *buf, int count);
- //! Writes a ('\0'-terminated) string.
+ //! Writes a ("\0"-terminated) string.
//! Returns the number of bytes written.
int writeString(const char *buf);
inline int writeString(const std::string &s) {
Modified: gearbox/trunk/submitted/gbxserialacfr/test/serialechotest.cpp
===================================================================
--- gearbox/trunk/submitted/gbxserialacfr/test/serialechotest.cpp 2008-02-20 00:45:24 UTC (rev 76)
+++ gearbox/trunk/submitted/gbxserialacfr/test/serialechotest.cpp 2008-02-22 02:58:17 UTC (rev 77)
@@ -5,6 +5,7 @@
#include <sstream>
#include <vector>
#include <iomanip>
+#include <assert.h>
using namespace std;
@@ -57,7 +58,9 @@
// cout<<"TRACE(serialechotest.cpp): &(data[0]): " << (int)(&(data[0])) << endl;
// cout<<"TRACE(serialechotest.cpp): data[0]: " << (int)(data[0]) << endl;
- serial.read( &(data[0]), nBytes );
+ int numRead = serial.read( &(data[0]), nBytes );
+ // There were nBytes available, we should be able to read them all
+ assert( numRead == (int)nBytes );
cout << "got data: " << toHexString( data ) << endl;
}
Modified: gearbox/trunk/submitted/gbxsickacfr/doc.dox
===================================================================
--- gearbox/trunk/submitted/gbxsickacfr/doc.dox 2008-02-20 00:45:24 UTC (rev 76)
+++ gearbox/trunk/submitted/gbxsickacfr/doc.dox 2008-02-22 02:58:17 UTC (rev 77)
@@ -26,6 +26,9 @@
@par Example
See test/test.cpp
+@par Style
+ See http://orca-robotics.sourceforge.net/orca/orca_doc_style.html
+
@par Copyright
Alex Brooks, Alexei Makarenko, Tobias Kaupp
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|