added option to read only a single field
This commit is contained in:
+14
-10
@@ -373,7 +373,8 @@ void SingleDataField::dump(ostream& output)
|
||||
result_t SingleDataField::read(const PartType partType,
|
||||
SymbolString& data, unsigned char offset,
|
||||
ostringstream& output, bool leadingSeparator,
|
||||
bool verbose, char separator)
|
||||
bool verbose, const char* filterName,
|
||||
char separator)
|
||||
{
|
||||
if (partType != m_partType)
|
||||
return RESULT_OK;
|
||||
@@ -389,11 +390,11 @@ result_t SingleDataField::read(const PartType partType,
|
||||
default:
|
||||
return RESULT_ERR_INVALID_PART;
|
||||
}
|
||||
if (isIgnored() == true) {
|
||||
if (isIgnored() == true || (filterName != NULL && m_name != filterName)) {
|
||||
if (offset + m_length > data.size()) {
|
||||
return RESULT_ERR_INVALID_POS;
|
||||
}
|
||||
return RESULT_OK;
|
||||
return RESULT_EMPTY;
|
||||
}
|
||||
|
||||
if (leadingSeparator == true)
|
||||
@@ -1111,9 +1112,10 @@ void DataFieldSet::dump(ostream& output)
|
||||
result_t DataFieldSet::read(const PartType partType,
|
||||
SymbolString& data, unsigned char offset,
|
||||
ostringstream& output, bool leadingSeparator,
|
||||
bool verbose, char separator)
|
||||
bool verbose, const char* filterName,
|
||||
char separator)
|
||||
{
|
||||
bool previousFullByteOffset = true;
|
||||
bool previousFullByteOffset = true, found = false;
|
||||
for (vector<SingleDataField*>::iterator it = m_fields.begin(); it < m_fields.end(); it++) {
|
||||
SingleDataField* field = *it;
|
||||
if (partType != pt_any && field->getPartType() != partType)
|
||||
@@ -1122,15 +1124,17 @@ result_t DataFieldSet::read(const PartType partType,
|
||||
if (previousFullByteOffset == false && field->hasFullByteOffset(false) == false)
|
||||
offset--;
|
||||
|
||||
//cout<<"read "<<field->getName().c_str()<<" in part "<<static_cast<unsigned>(field->getPartType())<<" offset "<<static_cast<unsigned>(offsets[field->getPartType()])<<endl;
|
||||
result_t result = field->read(partType, data, offset, output, leadingSeparator, verbose, separator);
|
||||
result_t result = field->read(partType, data, offset, output, leadingSeparator, verbose, filterName, separator);
|
||||
|
||||
if (result != RESULT_OK)
|
||||
if (result < RESULT_OK)
|
||||
return result;
|
||||
|
||||
offset += field->getLength(partType);
|
||||
previousFullByteOffset = field->hasFullByteOffset(true);
|
||||
leadingSeparator |= field->isIgnored() == false;
|
||||
if (result != RESULT_EMPTY) {
|
||||
found = true;
|
||||
leadingSeparator = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (verbose == true) {
|
||||
@@ -1138,7 +1142,7 @@ result_t DataFieldSet::read(const PartType partType,
|
||||
output << " [" << m_comment << "]";
|
||||
}
|
||||
|
||||
return RESULT_OK;
|
||||
return found == true ? RESULT_OK : RESULT_EMPTY;
|
||||
}
|
||||
|
||||
result_t DataFieldSet::write(istringstream& input,
|
||||
|
||||
+10
-4
@@ -183,13 +183,17 @@ public:
|
||||
* @param leadingSeparator whether to prepend a separator before the formatted value.
|
||||
* @param verbose whether to prepend the name, append the unit (if present), and append
|
||||
* the comment in square brackets (if present).
|
||||
* @param filterName the optional name of a field to limit the output to.
|
||||
* @param separator the separator character between multiple fields.
|
||||
* @return @a RESULT_OK on success (or if the partType does not match), or an error code.
|
||||
* @return @a RESULT_OK on success (or if the partType does not match),
|
||||
* or @a RESULT_EMPTY if the field was skipped (either ignored or due to @a filterName),
|
||||
* or an error code.
|
||||
*/
|
||||
virtual result_t read(const PartType partType,
|
||||
SymbolString& data, unsigned char offset,
|
||||
ostringstream& output, bool leadingSeparator=false,
|
||||
bool verbose=false, char separator=UI_FIELD_SEPARATOR) = 0;
|
||||
bool verbose=false, const char* filterName=NULL,
|
||||
char separator=UI_FIELD_SEPARATOR) = 0;
|
||||
/**
|
||||
* @brief Writes the value to the master or slave @a SymbolString.
|
||||
* @param input the @a istringstream to parse the formatted value from.
|
||||
@@ -270,7 +274,8 @@ public:
|
||||
virtual result_t read(const PartType partType,
|
||||
SymbolString& data, unsigned char offset,
|
||||
ostringstream& output, bool leadingSeparator=false,
|
||||
bool verbose=false, char separator=UI_FIELD_SEPARATOR);
|
||||
bool verbose=false, const char* filterName=NULL,
|
||||
char separator=UI_FIELD_SEPARATOR);
|
||||
// @copydoc
|
||||
virtual result_t write(istringstream& input,
|
||||
const PartType partType, SymbolString& data,
|
||||
@@ -569,7 +574,8 @@ public:
|
||||
virtual result_t read(const PartType partType,
|
||||
SymbolString& data, unsigned char offset,
|
||||
ostringstream& output, bool leadingSeparator=false,
|
||||
bool verbose=false, char separator=UI_FIELD_SEPARATOR);
|
||||
bool verbose=false, const char* filterName=NULL,
|
||||
char separator=UI_FIELD_SEPARATOR);
|
||||
// @copydoc
|
||||
virtual result_t write(istringstream& input,
|
||||
const PartType partType, SymbolString& data,
|
||||
|
||||
@@ -293,7 +293,9 @@ result_t Message::prepareSlave(SymbolString& slaveData)
|
||||
}
|
||||
|
||||
result_t Message::decode(const PartType partType, SymbolString& data,
|
||||
ostringstream& output, bool leadingSeparator, bool verbose, char separator)
|
||||
ostringstream& output, bool leadingSeparator,
|
||||
bool verbose, const char* filterName,
|
||||
char separator)
|
||||
{
|
||||
unsigned char offset;
|
||||
if (partType == pt_masterData)
|
||||
@@ -301,7 +303,7 @@ result_t Message::decode(const PartType partType, SymbolString& data,
|
||||
else
|
||||
offset = 0;
|
||||
int startPos = output.str().length();
|
||||
result_t result = m_data->read(partType, data, offset, output, leadingSeparator, verbose, separator);
|
||||
result_t result = m_data->read(partType, data, offset, output, leadingSeparator, verbose, filterName, separator);
|
||||
time(&m_lastUpdateTime);
|
||||
if (result != RESULT_OK) {
|
||||
m_lastValue.clear();
|
||||
|
||||
@@ -167,11 +167,14 @@ public:
|
||||
* @param leadingSeparator whether to prepend a separator before the formatted value.
|
||||
* @param verbose whether to prepend the name, append the unit (if present), and append
|
||||
* the comment in square brackets (if present).
|
||||
* @param filterName the optional name of a field to limit the output to.
|
||||
* @param separator the separator character between multiple fields.
|
||||
* @return @a RESULT_OK on success, or an error code.
|
||||
*/
|
||||
result_t decode(const PartType partType, SymbolString& data,
|
||||
ostringstream& output, bool leadingSeparator=false, bool verbose=false, char separator=UI_FIELD_SEPARATOR);
|
||||
ostringstream& output, bool leadingSeparator=false,
|
||||
bool verbose=false, const char* filterName=NULL,
|
||||
char separator=UI_FIELD_SEPARATOR);
|
||||
|
||||
/**
|
||||
* @brief Get the last decoded value.
|
||||
|
||||
@@ -27,6 +27,7 @@ const char* getResultCode(result_t resultCode) {
|
||||
case RESULT_OK: return "success";
|
||||
case RESULT_IN_ESC: return "success: escape sequence received";
|
||||
case RESULT_SYN: return "success: SYN received";
|
||||
case RESULT_EMPTY: return "success: empty";
|
||||
case RESULT_ERR_GENERIC_IO: return "ERR: generic I/O error";
|
||||
case RESULT_ERR_DEVICE: return "ERR: generic device error";
|
||||
case RESULT_ERR_SEND: return "ERR: send error";
|
||||
|
||||
@@ -24,6 +24,7 @@ static const int RESULT_OK = 0; // success
|
||||
|
||||
static const int RESULT_IN_ESC = 1; // start of escape sequence received
|
||||
static const int RESULT_SYN = 2; // regular SYN after message received
|
||||
static const int RESULT_EMPTY = 3; // empty result
|
||||
|
||||
static const int RESULT_ERR_GENERIC_IO = -1; // generic I/O error (usually fatal)
|
||||
static const int RESULT_ERR_DEVICE = -2; // generic device error (usually fatal)
|
||||
|
||||
Reference in New Issue
Block a user