|
From: <to...@us...> - 2008-07-01 14:14:27
|
Revision: 241
http://gearbox.svn.sourceforge.net/gearbox/?rev=241&view=rev
Author: tobasco
Date: 2008-07-01 07:14:36 -0700 (Tue, 01 Jul 2008)
Log Message:
-----------
Done most of the changes after Alex's comments except for the tests
Modified Paths:
--------------
gearbox/trunk/submitted/gbxsmartbatteryacfr/oceanserverparser.cpp
gearbox/trunk/submitted/gbxsmartbatteryacfr/oceanserverparser.h
gearbox/trunk/submitted/gbxsmartbatteryacfr/oceanserverreader.cpp
gearbox/trunk/submitted/gbxsmartbatteryacfr/oceanserverreader.h
gearbox/trunk/submitted/gbxsmartbatteryacfr/oceanserversystem.cpp
gearbox/trunk/submitted/gbxsmartbatteryacfr/smartbattery.cpp
gearbox/trunk/submitted/gbxsmartbatteryacfr/smartbattery.h
gearbox/trunk/submitted/gbxsmartbatteryacfr/smartbatteryacfr.dox
gearbox/trunk/submitted/gbxsmartbatteryacfr/smartbatteryparsing.cpp
gearbox/trunk/submitted/gbxsmartbatteryacfr/smartbatteryparsing.h
gearbox/trunk/submitted/gbxsmartbatteryacfr/test/simpletest.cpp
Removed Paths:
-------------
gearbox/trunk/submitted/gbxsmartbatteryacfr/test/serialtest.cpp
Modified: gearbox/trunk/submitted/gbxsmartbatteryacfr/oceanserverparser.cpp
===================================================================
--- gearbox/trunk/submitted/gbxsmartbatteryacfr/oceanserverparser.cpp 2008-07-01 13:35:32 UTC (rev 240)
+++ gearbox/trunk/submitted/gbxsmartbatteryacfr/oceanserverparser.cpp 2008-07-01 14:14:36 UTC (rev 241)
@@ -9,6 +9,7 @@
*/
#include <sstream>
+#include <gbxutilacfr/tokenise.h>
#include <gbxsmartbatteryacfr/exceptions.h>
#include <gbxsmartbatteryacfr/smartbatteryparsing.h>
@@ -63,30 +64,30 @@
vector<bool> states;
if (it->first=="01") {
- readSingleByte(it->second, states);
+ readFlags(it->second, states);
batterySystem.availableBatteries = states;
}
else if (it->first=="02") {
- readSingleByte(it->second, states);
+ readFlags(it->second, states);
batterySystem.chargingStates = states;
}
else if (it->first=="03") {
- readSingleByte(it->second, states);
+ readFlags(it->second, states);
batterySystem.supplyingPowerStates = states;
}
else if (it->first=="04") {
// reserved, do nothing
}
else if (it->first=="05") {
- readSingleByte(it->second, states);
+ readFlags(it->second, states);
batterySystem.chargePowerPresentStates = states;
}
else if (it->first=="06") {
- readSingleByte(it->second, states);
+ readFlags(it->second, states);
batterySystem.powerNoGoodStates = states;
}
else if (it->first=="07") {
- readSingleByte(it->second, states);
+ readFlags(it->second, states);
batterySystem.chargeInhibitedStates = states;
}
else
@@ -112,7 +113,7 @@
for (it=keyValuePairs.begin(); it!=keyValuePairs.end(); it++)
{
- SmartBatteryDataField smartField = stringToSmartField( it->first );
+ SmartBatteryDataField smartField = keyToSmartField( it->first );
switch( smartField )
{
@@ -131,7 +132,7 @@
case AtRateTimeToEmpty:
bat.setAtRateTimeToEmpty( readMinutes( it->second ) ); break;
case AtRateOk:
- bat.setAtRateOk( readBool( it->second ) ); break;
+ bat.setAtRateOk( readRate( it->second ) ); break;
case Temperature:
bat.setTemperature( readTemperature( it->second ) ); break;
case Voltage:
@@ -233,10 +234,9 @@
}
bool
-OceanServerParser::atBeginningOfRecord( const char* line )
+OceanServerParser::atBeginningOfRecord( const std::string &line )
{
- vector<string> tokens;
- splitIntoFields( line, tokens, ",");
+ vector<string> tokens = gbxutilacfr::tokenise( line, ",");
if (tokens.size()>0) {
if (tokens[0]!="$S") {
@@ -246,19 +246,7 @@
return true;
}
-bool
-OceanServerParser::atEndOfRecord( const char* line )
-{
- vector<string> tokens;
- splitIntoFields( line, tokens, ",");
-
- if (tokens.size()>0) {
- if (tokens[0]=="$S") return true;
- }
- return false;
-}
-
void
OceanServerParser::parse( vector<string> &stringList,
OceanServerSystem &batterySystem )
@@ -308,8 +296,7 @@
}
// divide the line into 2 parts: data and checksum (if present)
- vector<string> checksumList;
- splitIntoFields(line, checksumList, "%" );
+ vector<string> checksumList = gbxutilacfr::tokenise( line, "%" );
if (checksumList.size()==2)
{
// we have a checksum, is it correct?
@@ -320,8 +307,7 @@
// divide the data into individual fields and parse
if (checksumList.size()==0)
throw ParsingException( ERROR_INFO, "String length is 0" );
- vector<string> fields;
- splitIntoFields( checksumList[0], fields, ",");
+ vector<string> fields = gbxutilacfr::tokenise( checksumList[0], "," );
parseFields( fields, batterySystem );
}
}
Modified: gearbox/trunk/submitted/gbxsmartbatteryacfr/oceanserverparser.h
===================================================================
--- gearbox/trunk/submitted/gbxsmartbatteryacfr/oceanserverparser.h 2008-07-01 13:35:32 UTC (rev 240)
+++ gearbox/trunk/submitted/gbxsmartbatteryacfr/oceanserverparser.h 2008-07-01 14:14:36 UTC (rev 241)
@@ -27,8 +27,7 @@
{
public:
- OceanServerParser( gbxutilacfr::Tracer &tracer );
- ~OceanServerParser() {};
+ OceanServerParser( gbxutilacfr::Tracer &tracer );
//! Expects a full record of batterydata as a stringList (one line per string) produced by the oceanserver controller.
//! Parses each line and sets corresponding fields in batterySystem
@@ -36,10 +35,7 @@
OceanServerSystem &batterySystem );
//! Checks whether the passed string (one line) is the first line of the record
- bool atBeginningOfRecord( const char *string );
-
- //! Checks whether the passed string (one line) is the end of the record
- bool atEndOfRecord( const char *string );
+ bool atBeginningOfRecord( const std::string &line );
private:
Modified: gearbox/trunk/submitted/gbxsmartbatteryacfr/oceanserverreader.cpp
===================================================================
--- gearbox/trunk/submitted/gbxsmartbatteryacfr/oceanserverreader.cpp 2008-07-01 13:35:32 UTC (rev 240)
+++ gearbox/trunk/submitted/gbxsmartbatteryacfr/oceanserverreader.cpp 2008-07-01 14:14:36 UTC (rev 241)
@@ -18,12 +18,20 @@
namespace gbxsmartbatteryacfr {
+// baudrate is fixed
static const int BAUDRATE = 19200;
+
+// timeout for reading from the serial port
static const int TIMEOUT_SEC = 2;
+
+// the maximum number of lines to read until we are confident that we are not
+// connected to an OceanServer system
+static const int MAX_TRIES = 50;
+
-OceanServerReader::OceanServerReader( const string &device,
+OceanServerReader::OceanServerReader( const string &serialPort,
gbxutilacfr::Tracer &tracer )
- : serial_( device, BAUDRATE, gbxserialacfr::Serial::Timeout(TIMEOUT_SEC,0) ),
+ : serial_( serialPort, BAUDRATE, gbxserialacfr::Serial::Timeout(TIMEOUT_SEC,0) ),
tracer_(tracer),
parser_(tracer),
firstTime_(true)
@@ -45,12 +53,18 @@
}
bool
-OceanServerReader::isOceanServerSystem( const char* oceanServerString )
-{
- for (unsigned int i=0; i<oceanServerStrings_.size(); i++)
+OceanServerReader::isOceanServerSystem( string &oceanServerString )
+{
+ // number of characters which we require to match
+ // if they match, we are pretty sure we have an OceanServerSystem
+ unsigned int numCharRequired = 8;
+
+ if ( oceanServerString.size()<numCharRequired ) return false;
+
+ for (unsigned int i=0; i<oceanServerStrings_.size(); i++)
{
- // if the first 8 characters agree we are pretty sure we have an OceanServerSystem
- if (strncmp(oceanServerStrings_[i].c_str(),oceanServerString,8)==0) return true;
+ if ( strncmp(oceanServerStrings_[i].c_str(),oceanServerString.c_str(),numCharRequired)==0 )
+ return true;
}
return false;
}
@@ -64,42 +78,44 @@
const char menuMode = ' ';
serial_.write(&menuMode, 1);
- // if we were in battery reading mode before, we might have to skip quite
- // a few lines until we get something from the menu
- const int maxTries = 40;
int numTries=0;
-
- // for tracer output
stringstream ss;
while(true)
{
- ss.str(""); ss << "OceanServerReader: Trying to read from serial port with timeout of " << TIMEOUT_SEC << "s" << endl;
+ ss.str(""); ss << "OceanServerReader: " << __func__ << ": Trying to read from serial port with timeout of " << TIMEOUT_SEC << "s" << endl;
tracer_.info( ss.str() );
+ // reading from serial port
string serialData;
int ret = serial_.readLine( serialData );
- if (ret<0) {
- throw HardwareReadingException( ERROR_INFO, "Connected to the wrong serial port. Timed out while trying to read a line.");
+ if ( ret<0 ) {
+ throw HardwareReadingException( ERROR_INFO, "Timed out while trying to read a line from serial port.");
}
- if ( isOceanServerSystem(serialData.c_str()) ) {
- tracer_.info( "Oceanserverreader.cpp: We are connected to an Oceanserver system. Good." );
+
+ // checking whether we are connected to an oceanserver system
+ if ( isOceanServerSystem(serialData) ) {
+ tracer_.info( "OceanServerReader: We are connected to an Oceanserver system. Good." );
break;
}
+
+ // count the number of tries
numTries++;
- ss.str(""); ss << "OceanServerReader: Trying to find out whether this is an oceanserver system. Attempt number " << numTries << "/" << maxTries << ".";
+ ss.str(""); ss << "OceanServerReader: Trying to find out whether this is an oceanserver system. Attempt number " << numTries << "/" << MAX_TRIES << ".";
tracer_.info( ss.str() );
- if (numTries>=maxTries) {
- throw HardwareReadingException( ERROR_INFO, "Connected to the wrong serial port. Didn't recognize any of the strings.");
+
+ if ( numTries >= MAX_TRIES ) {
+ throw HardwareReadingException( ERROR_INFO, "Didn't recognize any of the strings. We may be connected to the wrong serial port.");
}
}
}
-void
-OceanServerReader::tryToReadLineFromSerialPort( std::string &serialData )
+std::string
+OceanServerReader::tryToReadLineFromSerialPort()
{
const int maxTries=5;
int numTries=0;
+ string serialData;
while(true)
{
@@ -114,6 +130,8 @@
throw HardwareReadingException( ERROR_INFO, ss.str().c_str() );
}
}
+
+ return serialData;
}
void
@@ -125,10 +143,14 @@
if (firstTime_)
{
// (1) Wait until we got the beginning of the record
+ int numTries=0;
while(true)
{
- tryToReadLineFromSerialPort( serialData );
- if (parser_.atBeginningOfRecord( serialData.c_str() )) break;
+ if ( numTries > MAX_TRIES )
+ throw gbxutilacfr::Exception( ERROR_INFO, "Couldn't find the beginning of a valid OceanServer record" );
+ numTries++;
+ serialData = tryToReadLineFromSerialPort();
+ if (parser_.atBeginningOfRecord( serialData )) break;
}
tracer_.debug( "OceanServerReader: Beginning of a new record", 5 );
@@ -145,19 +167,23 @@
try {
// (3) Read the rest of the record line-by-line
+ int numTries=0;
while(true)
{
- tryToReadLineFromSerialPort( serialData );
- if ( parser_.atEndOfRecord( serialData.c_str() ) )
+ if ( numTries > MAX_TRIES )
+ throw gbxutilacfr::Exception( ERROR_INFO, "Couldn't find the beginning of a valid OceanServer record" );
+ numTries++;
+ serialData = tryToReadLineFromSerialPort();
+ if ( parser_.atBeginningOfRecord( serialData ) )
{
- tracer_.debug( "OceanServerReader: End of record", 5 );
+ tracer_.debug( "OceanServerReader: End of the record (beginning of the NEXT record)", 5 );
parser_.parse( stringList, system );
break;
}
stringList.push_back(serialData);
}
- // (4) Save the last line: it is the beginning of the next record
+ // (4) Save the beginning of the next record
beginningRecordLine_ = serialData;
if (firstTime_) {
firstTime_ = false;
@@ -167,7 +193,7 @@
{
stringstream ss;
ss << "OceanServerReader: Caught ParsingException: " << e.what() << ". ";
- ss << "It's not critical, we are trying to find the beginning of a new record.";
+ ss << "It's not critical, we will try to find the beginning of a new record.";
tracer_.warning( ss.str() );
firstTime_ = true;
Modified: gearbox/trunk/submitted/gbxsmartbatteryacfr/oceanserverreader.h
===================================================================
--- gearbox/trunk/submitted/gbxsmartbatteryacfr/oceanserverreader.h 2008-07-01 13:35:32 UTC (rev 240)
+++ gearbox/trunk/submitted/gbxsmartbatteryacfr/oceanserverreader.h 2008-07-01 14:14:36 UTC (rev 241)
@@ -31,18 +31,20 @@
{
public:
- //! May throw HardwareReadingException
- OceanServerReader( const std::string &device,
+ //! Connects to the serial port (e.g. /dev/ttyS0) and checks
+ //! whether we are connected to an OceanServer system.
+ //! Throws a HardwareReadingException if any of the above goes wrong.
+ OceanServerReader( const std::string &serialPort,
gbxutilacfr::Tracer &tracer );
-
- ~OceanServerReader() {};
- //! May throw HardwareReadingException
+ //! May throw HardwareReadingExceptions and ParsingExceptions
void read( OceanServerSystem &system );
private:
- bool isOceanServerSystem( const char* oceanServerString );
+ // Returns true if 100% sure that we are connected to an OceanServer system, otherwise false
+ bool isOceanServerSystem( std::string &oceanServerString );
+
std::vector<std::string> oceanServerStrings_;
gbxserialacfr::Serial serial_;
@@ -50,7 +52,7 @@
gbxsmartbatteryacfr::OceanServerParser parser_;
void checkConnection();
- void tryToReadLineFromSerialPort( std::string &serialData );
+ std::string tryToReadLineFromSerialPort();
std::string beginningRecordLine_;
bool firstTime_;
Modified: gearbox/trunk/submitted/gbxsmartbatteryacfr/oceanserversystem.cpp
===================================================================
--- gearbox/trunk/submitted/gbxsmartbatteryacfr/oceanserversystem.cpp 2008-07-01 13:35:32 UTC (rev 240)
+++ gearbox/trunk/submitted/gbxsmartbatteryacfr/oceanserversystem.cpp 2008-07-01 14:14:36 UTC (rev 241)
@@ -21,6 +21,7 @@
//
// Helper functions
//
+namespace {
string toString( const vector<bool> &flags )
{
@@ -41,12 +42,13 @@
}
return ss.str();
}
+
+}
//
// Non-member functions
//
-
string toString( const OceanServerSystem &system )
{
stringstream ss;
@@ -177,7 +179,6 @@
//
// Member functions
//
-
OceanServerSystem::OceanServerSystem()
: percentCharge(0),
minToEmpty(0),
Modified: gearbox/trunk/submitted/gbxsmartbatteryacfr/smartbattery.cpp
===================================================================
--- gearbox/trunk/submitted/gbxsmartbatteryacfr/smartbattery.cpp 2008-07-01 13:35:32 UTC (rev 240)
+++ gearbox/trunk/submitted/gbxsmartbatteryacfr/smartbattery.cpp 2008-07-01 14:14:36 UTC (rev 241)
@@ -18,48 +18,47 @@
namespace gbxsmartbatteryacfr {
-SmartBatteryDataField stringToSmartField( const string &fieldStr )
+SmartBatteryDataField keyToSmartField( const string &key )
{
- if (fieldStr=="00") return ManufacturerAccess;
- else if (fieldStr=="01") return RemainingCapacityAlarm;
- else if (fieldStr=="02") return RemainingTimeAlarm;
- else if (fieldStr=="03") return BatteryMode;
- else if (fieldStr=="04") return AtRate;
- else if (fieldStr=="05") return AtRateTimeToFull;
- else if (fieldStr=="06") return AtRateTimeToEmpty;
- else if (fieldStr=="07") return AtRateOk;
- else if (fieldStr=="08") return Temperature;
- else if (fieldStr=="09") return Voltage;
- else if (fieldStr=="0A") return Current;
- else if (fieldStr=="0B") return AverageCurrent;
- else if (fieldStr=="0C") return MaxError;
- else if (fieldStr=="0D") return RelativeStateOfCharge;
- else if (fieldStr=="0E") return AbsoluteStateOfCharge;
- else if (fieldStr=="0F") return RemainingCapacity;
- else if (fieldStr=="10") return FullChargeCapacity;
- else if (fieldStr=="11") return RunTimeToEmpty;
- else if (fieldStr=="12") return AverageTimeToEmpty;
- else if (fieldStr=="13") return AverageTimeToFull;
- else if (fieldStr=="14") return ChargingCurrent;
- else if (fieldStr=="15") return ChargingVoltage;
- else if (fieldStr=="16") return BatteryStatus;
- else if (fieldStr=="17") return CycleCount;
- else if (fieldStr=="18") return DesignCapacity;
- else if (fieldStr=="19") return DesignVoltage;
- else if (fieldStr=="1A") return SpecificationInfo;
- else if (fieldStr=="1B") return ManufactureDate;
- else if (fieldStr=="1C") return SerialNumber;
- else if (fieldStr=="20") return ManufacturerName;
- else if (fieldStr=="21") return DeviceName;
- else if (fieldStr=="22") return DeviceChemistry;
- else if (fieldStr=="23") return ManufacturerData;
+ if (key=="00") return ManufacturerAccess;
+ else if (key=="01") return RemainingCapacityAlarm;
+ else if (key=="02") return RemainingTimeAlarm;
+ else if (key=="03") return BatteryMode;
+ else if (key=="04") return AtRate;
+ else if (key=="05") return AtRateTimeToFull;
+ else if (key=="06") return AtRateTimeToEmpty;
+ else if (key=="07") return AtRateOk;
+ else if (key=="08") return Temperature;
+ else if (key=="09") return Voltage;
+ else if (key=="0A") return Current;
+ else if (key=="0B") return AverageCurrent;
+ else if (key=="0C") return MaxError;
+ else if (key=="0D") return RelativeStateOfCharge;
+ else if (key=="0E") return AbsoluteStateOfCharge;
+ else if (key=="0F") return RemainingCapacity;
+ else if (key=="10") return FullChargeCapacity;
+ else if (key=="11") return RunTimeToEmpty;
+ else if (key=="12") return AverageTimeToEmpty;
+ else if (key=="13") return AverageTimeToFull;
+ else if (key=="14") return ChargingCurrent;
+ else if (key=="15") return ChargingVoltage;
+ else if (key=="16") return BatteryStatus;
+ else if (key=="17") return CycleCount;
+ else if (key=="18") return DesignCapacity;
+ else if (key=="19") return DesignVoltage;
+ else if (key=="1A") return SpecificationInfo;
+ else if (key=="1B") return ManufactureDate;
+ else if (key=="1C") return SerialNumber;
+ else if (key=="20") return ManufacturerName;
+ else if (key=="21") return DeviceName;
+ else if (key=="22") return DeviceChemistry;
+ else if (key=="23") return ManufacturerData;
else
{
stringstream ss;
- ss << "Unknown field: " << fieldStr;
+ ss << "Unknown field: " << key;
throw ParsingException( ERROR_INFO, ss.str().c_str() );
- }
- return NUM_SMARTBATTERY_FIELDS;
+ }
}
string toString( const SmartBattery &b )
Modified: gearbox/trunk/submitted/gbxsmartbatteryacfr/smartbattery.h
===================================================================
--- gearbox/trunk/submitted/gbxsmartbatteryacfr/smartbattery.h 2008-07-01 13:35:32 UTC (rev 240)
+++ gearbox/trunk/submitted/gbxsmartbatteryacfr/smartbattery.h 2008-07-01 14:14:36 UTC (rev 241)
@@ -57,8 +57,9 @@
NUM_SMARTBATTERY_FIELDS
};
-//! Converts a string to a SmartBatteryDataField, returns NUM_SMARTBATTERY_FIELDS if string is not valid
-SmartBatteryDataField stringToSmartField( const std::string &fieldStr );
+//! Converts a key (string) to a SmartBatteryDataField.
+//! Throws a ParsingException if key is unknown.
+SmartBatteryDataField keyToSmartField( const std::string &key );
//! SmartBattery class holds all the data of a single smart battery.
//! Since not all data is always present, access to data needs to be done as follows:
Modified: gearbox/trunk/submitted/gbxsmartbatteryacfr/smartbatteryacfr.dox
===================================================================
--- gearbox/trunk/submitted/gbxsmartbatteryacfr/smartbatteryacfr.dox 2008-07-01 13:35:32 UTC (rev 240)
+++ gearbox/trunk/submitted/gbxsmartbatteryacfr/smartbatteryacfr.dox 2008-07-01 14:14:36 UTC (rev 241)
@@ -52,11 +52,12 @@
- This is a Linux-only implementation (due to the Linux-only serial library).
- It works with those serial devices which @ref gbx_library_gbxserialacfr supports.
- The library is tailored to the OceanServer battery system.
+- Has been tested with the following hardware configuration: battery management module MP08SR, DC/DC converter module DC123SR, and a variable number of battery modules of type BA95HC-FL (can be added and removed at runtime)
- Has been tested with the following firmware: V2.10 (2007)
-@par OceanServer Layout
+@par MP08SR Layout
-The battery number reported by the driver corresponds to the following connectors on the Battery Management Module board:
+The battery number reported by the driver corresponds to the following connectors on the Battery Management Module board MP08SR:
@verbatim
Level 1 (lower level):
Modified: gearbox/trunk/submitted/gbxsmartbatteryacfr/smartbatteryparsing.cpp
===================================================================
--- gearbox/trunk/submitted/gbxsmartbatteryacfr/smartbatteryparsing.cpp 2008-07-01 13:35:32 UTC (rev 240)
+++ gearbox/trunk/submitted/gbxsmartbatteryacfr/smartbatteryparsing.cpp 2008-07-01 14:14:36 UTC (rev 241)
@@ -17,14 +17,12 @@
namespace gbxsmartbatteryacfr {
-//
-// Not part of the public interface
-//
-
+namespace {
+
int readNumPositiveFlags(const string &str)
{
vector<bool> flags;
- readSingleByte( str, flags );
+ readFlags( str, flags );
int numPositiveFlags=0;
@@ -74,10 +72,8 @@
return value16;
}
+} // end of namespace
-//
-// Public interface functions
-//
double readTemperature( const string &str )
{
@@ -137,12 +133,6 @@
return readUnsignedInt16( str );
}
-bool readBool( const string &str )
-{
- return readUnsignedInt16( str );
-}
-
-
int readRate( const string &str )
{
return readUnsignedInt16( str );
@@ -199,35 +189,11 @@
}
-void splitIntoFields( const string &str,
- vector<string> &fields,
- const string &delimiter)
+void readFlags( const string &str,
+ vector<bool> &flags )
{
- // Skip delimiters at beginning.
- string::size_type lastPos = str.find_first_not_of(delimiter, 0);
-
- // Find first "non-delimiter".
- string::size_type pos = str.find_first_of(delimiter, lastPos);
-
- while (string::npos != pos || string::npos != lastPos)
- {
- // Found a token, add it to the vector.
- fields.push_back(str.substr(lastPos, pos - lastPos));
-
- // Skip delimiters. Note the "not_of"
- lastPos = str.find_first_not_of(delimiter, pos);
-
- // Find next "non-delimiter"
- pos = str.find_first_of(delimiter, lastPos);
- }
-}
-
-
-void readSingleByte( const string &str,
- vector<bool> &flags )
-{
if (str.size()!=2)
- throw ParsingException( ERROR_INFO, "readSingleByte called with string size != 2" );
+ throw ParsingException( ERROR_INFO, "readFlags called with string size != 2" );
stringstream ss(str);
int allFlags;
Modified: gearbox/trunk/submitted/gbxsmartbatteryacfr/smartbatteryparsing.h
===================================================================
--- gearbox/trunk/submitted/gbxsmartbatteryacfr/smartbatteryparsing.h 2008-07-01 13:35:32 UTC (rev 240)
+++ gearbox/trunk/submitted/gbxsmartbatteryacfr/smartbatteryparsing.h 2008-07-01 14:14:36 UTC (rev 241)
@@ -19,38 +19,66 @@
namespace gbxsmartbatteryacfr
{
-//!
-//! Functions to parse SmartBattery data
-//! and some helper functions
-//!
-//! @author Tobias Kaupp
-//!
-
-// Reading data
-void readSingleByte( const std::string &str,
- std::vector<bool> &flags );
+//! Expects 2 hex characters and translates them into a vector of boolean flags
+//! May throw ParsingException
+void readFlags( const std::string &str,
+ std::vector<bool> &flags );
+
+//! Expects 4 hex characters, returns temperature [degC]
+//! May throw ParsingException
double readTemperature( const std::string &str );
+
+//! Expects 4 hex characters, returns current [A]
+//! May throw ParsingException
double readCurrent( const std::string &str );
+
+//! Expects 4 hex characters, returns voltage [V]
+//! May throw ParsingException
double readVoltage( const std::string &str );
+
+//! Expects 2 hex characters, returns number of batteries
+//! May throw ParsingException
int readNumBatteries( const std::string &str );
+
+//! Expects 4 hex characters, returns percentage
+//! May throw ParsingException
int readPercentWord( const std::string &str );
+
+//! Expects 2 hex characters, returns percentage
+//! May throw ParsingException
int readPercentByte( const std::string &str );
+
+//! Expects 4 hex characters, returns minutes
+//! May throw ParsingException
int readMinutes( const std::string &str );
+
+//! Expects 4 hex characters, returns capacity [%]
+//! May throw ParsingException
int readCapacity( const std::string &str );
+
+//! Expects 4 hex characters, translates them into uint16_t
+//! May throw ParsingException
uint16_t read16Flags( const std::string &str );
+
+//! Expects 4 hex characters, returns a count
+//! May throw ParsingException
int readCount( const std::string &str );
+
+//! Expects 4 hex characters, returns a number
+//! May throw ParsingException
int readNumber( const std::string &str );
-bool readBool( const std::string &str );
+
+//! Expects 4 hex characters, returns a rate
+//! May throw ParsingException
int readRate( const std::string &str );
-// Other helper functions
-bool isChecksumValid( const std::string &data,
- const std::string &checksumStr );
-
-void splitIntoFields( const std::string &str,
- std::vector<std::string> &fields,
- const std::string &delimiter);
+//! Computes an XOR checksum from 'input' (skips the first character).
+//! Returns true if it matches with 'expected', otherwise false
+bool isChecksumValid( const std::string &input,
+ const std::string &expected );
+//! Decomposes 'fields' (a flat list of keys and values) into 'pairs'
+//! (a map of keys and values). May throw ParsingException.
void toKeyValuePairs( const std::vector<std::string> &fields,
std::map<std::string,std::string> &pairs,
gbxutilacfr::Tracer &tracer );
Deleted: gearbox/trunk/submitted/gbxsmartbatteryacfr/test/serialtest.cpp
===================================================================
--- gearbox/trunk/submitted/gbxsmartbatteryacfr/test/serialtest.cpp 2008-07-01 13:35:32 UTC (rev 240)
+++ gearbox/trunk/submitted/gbxsmartbatteryacfr/test/serialtest.cpp 2008-07-01 14:14:36 UTC (rev 241)
@@ -1,96 +0,0 @@
-#include <iostream>
-#include <fstream>
-#include <sstream>
-#include <sys/time.h>
-#include <vector>
-#include <string>
-
-#include <gbxserialacfr/serial.h>
-
-using namespace std;
-
-static const unsigned int BUFFER_SIZE = 1024;
-static const int BAUDRATE = 19200;
-static const int TIMEOUT_SEC = 30;
-static string DEVICE="/dev/ttyS3";
-
-// ofstream* createFile()
-// {
-// string filename="/home/mrsys/tmp/ocean/serialtest.txt";
-// ofstream *file = new ofstream( filename.c_str(), ios::app );
-// if ( !file->is_open() ) {
-// cout << "Could not create file " << filename << endl;
-// exit(1);
-// }
-// struct timeval now;
-// gettimeofday( &now, 0 );
-// (*file) << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << endl;
-// (*file) << "Start time (sec): " << now.tv_sec << endl << endl;
-
-// return file;
-// }
-
-bool isOceanServerSystem( const char* oceanServerString, gbxserialacfr::Serial &serial_ )
-{
- vector<string> oceanServerStrings_;
- oceanServerStrings_.push_back(" S - Setup Controller");
- oceanServerStrings_.push_back(" B - Battery Status");
- oceanServerStrings_.push_back(" X - Host HEX");
- oceanServerStrings_.push_back(" H - Help");
- oceanServerStrings_.push_back(" www.ocean-server.com");
-
- for (unsigned int i=0; i<oceanServerStrings_.size(); i++)
- {
- // if the first 8 characters agree we are pretty sure we have an OceanServerSystem
- if (strncmp(oceanServerStrings_[i].c_str(),oceanServerString,8)==0) return true;
- }
- return false;
-}
-
-int main( int argc, char **argv )
-{
- gbxserialacfr::Serial serial_( DEVICE, BAUDRATE, gbxserialacfr::Serial::Timeout(TIMEOUT_SEC,0) );
-
- const char menuMode = ' ';
- serial_.write(&menuMode, 1);
-
- const int maxTries = 40;
- int numTries=0;
-
- while(true)
- {
- cout << "INFO(oceanserverreader.cpp): Trying to read from serial port with timeout of " << TIMEOUT_SEC << "s" << endl;
- string serialData;
- int ret = serial_.readLine( serialData );
- if (ret<0) {
- cout << "Connected to the wrong serial port. Timed out while trying to read a line." << endl;
- }
- if ( isOceanServerSystem( serialData.c_str(), serial_ ) ) {
- cout << "INFO(oceanserverreader.cpp): We are connected to an Oceanserver system. Good." << endl;
- break;
- }
- numTries++;
- cout << "Trying to find out whether this is an oceanserver system. Attempt number " << numTries << "/" << maxTries << "." << endl;
- if (numTries>=maxTries) {
- cout << "Connected to the wrong serial port. Didn't recognize any of the strings." << endl;
- }
- }
-
- // send an X
- serial_.flush();
- const char startReading = 'X';
- serial_.write(&startReading, 1);
-
- // ofstream* file = createFile();
-
- while(true)
- {
- std::string serialData;
-
- int ret = serial_.readLine( serialData );
-
- cout << "Ret: " << ret << " " << serialData << flush;
- }
-
- return 0;
-}
Modified: gearbox/trunk/submitted/gbxsmartbatteryacfr/test/simpletest.cpp
===================================================================
--- gearbox/trunk/submitted/gbxsmartbatteryacfr/test/simpletest.cpp 2008-07-01 13:35:32 UTC (rev 240)
+++ gearbox/trunk/submitted/gbxsmartbatteryacfr/test/simpletest.cpp 2008-07-01 14:14:36 UTC (rev 241)
@@ -72,6 +72,7 @@
cout << "ERROR(simple_test): Caught a hardware reading exception: "
<< e.what() << endl
<< "This shouldn't happen!" << endl;
+ delete reader;
return 1;
}
catch(std::exception &e)
@@ -79,6 +80,7 @@
cout << "ERROR(simple_test): Caught an unknown exception: "
<< e.what() << endl
<< "This shouldn't happen!" << endl;
+ delete reader;
return 1;
}
} //end of for loop
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|