|
From: <rum...@us...> - 2008-07-04 13:08:08
|
Revision: 260
http://gearbox.svn.sourceforge.net/gearbox/?rev=260&view=rev
Author: rumataxyz
Date: 2008-07-04 06:08:05 -0700 (Fri, 04 Jul 2008)
Log Message:
-----------
added another utility function, to send commands and test for success
Modified Paths:
--------------
gearbox/trunk/submitted/gbxnovatelacfr/gbxnovatelutilacfr/serialconnectivity.cpp
gearbox/trunk/submitted/gbxnovatelacfr/gbxnovatelutilacfr/serialconnectivity.h
Modified: gearbox/trunk/submitted/gbxnovatelacfr/gbxnovatelutilacfr/serialconnectivity.cpp
===================================================================
--- gearbox/trunk/submitted/gbxnovatelacfr/gbxnovatelutilacfr/serialconnectivity.cpp 2008-07-03 08:15:02 UTC (rev 259)
+++ gearbox/trunk/submitted/gbxnovatelacfr/gbxnovatelutilacfr/serialconnectivity.cpp 2008-07-04 13:08:05 UTC (rev 260)
@@ -44,41 +44,23 @@
serial.flush();
}
catch( SerialException &e ){
- std::cout <<"Caught SerialException: " << e.what()<<"\n";
+ std::cerr <<"Caught SerialException: " << e.what()<<"\n";
return false;
}
catch( std::exception &e ){
- std::cout <<"Caught std::exception: " << e.what()<<"\n";
+ std::cerr <<"Caught std::exception: " << e.what()<<"\n";
return false;
}
catch(...){
- std::cout <<"Caught unknown Exception :-(\n";
+ std::cerr <<"Caught unknown Exception :-(\n";
return false;
}
int successCnt = 0;
+ std::string errorResponse;
for(int i=0; i<numTry; i++){
- // send challenge
- serial.writeString(challenge.c_str());
- usleep(timeOutMsec*1000);
-
- //read response
- int available;
- available = serial.bytesAvailable();
- if(0<available){
- char *buf;
- buf = new char[available+1];
- int read = serial.read(buf, available);
- assert( read != -1 );
-
- //and look for an ACK
- buf[read] = '\0';
- std::string response(buf);
- size_t found = response.find(ack);
- if(std::string::npos != found){
- successCnt++;
- }
- delete []buf;
+ if(true == sendCmdWaitForResponse( challenge, ack, errorResponse, serial, timeOutMsec)){
+ successCnt++;
}
}
std::cout << successCnt << "/"<< numTry << " ";
@@ -90,4 +72,38 @@
return false;
}
}
+
+bool sendCmdWaitForResponse(
+ std::string &challenge,
+ std::string &ack,
+ std::string &errorResponse,
+ gbxserialacfr::Serial& serial,
+ int timeOutMsec){
+ // send challenge
+ serial.writeString(challenge.c_str());
+ usleep(timeOutMsec*1000);
+
+ //read response
+ int available;
+ bool success = false;
+ available = serial.bytesAvailable();
+ if(0<available){
+ char *buf;
+ buf = new char[available+1];
+ int read = serial.read(buf, available);
+ assert( read != -1 );
+
+ //and look for an ACK
+ buf[read] = '\0';
+ std::string response(buf);
+ size_t found = response.find(ack);
+ if(std::string::npos != found){
+ success = true;
+ }else{
+ errorResponse = response;
+ }
+ delete []buf;
+ }
+ return success;
+}
}//namespace
Modified: gearbox/trunk/submitted/gbxnovatelacfr/gbxnovatelutilacfr/serialconnectivity.h
===================================================================
--- gearbox/trunk/submitted/gbxnovatelacfr/gbxnovatelutilacfr/serialconnectivity.h 2008-07-03 08:15:02 UTC (rev 259)
+++ gearbox/trunk/submitted/gbxnovatelacfr/gbxnovatelutilacfr/serialconnectivity.h 2008-07-04 13:08:05 UTC (rev 260)
@@ -19,7 +19,7 @@
// Limitations: amount of data expected before timeOutMsec should be
// reasonably small
//
-// returns 0 for Success; -1 for failure
+// returns true for Success; false for failure
namespace gbxnovatelutilacfr{
bool testConnectivity(
std::string &challenge,
@@ -29,4 +29,19 @@
int numTry,
int successThresh,
int baudrate);
+
+// send a [challenge] (command ...) to
+// which the device will answer with a unique [ack] in [timeOutMsec] milliseconds.
+//
+// Limitations: amount of data expected before timeOutMsec should be
+// reasonably small
+//
+// returns true for Success (i.e. ack received in time),
+// false for failure, in case of failure errorResponse contains the reply from the receiver
+bool sendCmdWaitForResponse(
+ std::string &challenge,
+ std::string &ack,
+ std::string &errorResponse,
+ gbxserialacfr::Serial& serial,
+ int timeOutMsec);
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|