Feature: add unique prefix to VE.Direct messages

This commit is contained in:
Bernhard Kirchen 2024-03-29 16:05:09 +01:00 committed by Bernhard Kirchen
parent 8abf614047
commit 187f197e32
4 changed files with 21 additions and 21 deletions

View File

@ -72,7 +72,7 @@ VeDirectFrameHandler::VeDirectFrameHandler() :
{ {
} }
void VeDirectFrameHandler::init(int8_t rx, int8_t tx, Print* msgOut, bool verboseLogging, uint16_t hwSerialPort) void VeDirectFrameHandler::init(char const* who, int8_t rx, int8_t tx, Print* msgOut, bool verboseLogging, uint16_t hwSerialPort)
{ {
_vedirectSerial = std::make_unique<HardwareSerial>(hwSerialPort); _vedirectSerial = std::make_unique<HardwareSerial>(hwSerialPort);
_vedirectSerial->begin(19200, SERIAL_8N1, rx, tx); _vedirectSerial->begin(19200, SERIAL_8N1, rx, tx);
@ -80,13 +80,15 @@ void VeDirectFrameHandler::init(int8_t rx, int8_t tx, Print* msgOut, bool verbos
_msgOut = msgOut; _msgOut = msgOut;
_verboseLogging = verboseLogging; _verboseLogging = verboseLogging;
_debugIn = 0; _debugIn = 0;
snprintf(_logId, sizeof(_logId), "[VE.Direct %s %d/%d]", who, rx, tx);
if (_verboseLogging) { _msgOut->printf("%s init complete\r\n", _logId); }
} }
void VeDirectFrameHandler::dumpDebugBuffer() { void VeDirectFrameHandler::dumpDebugBuffer() {
_msgOut->printf("[VE.Direct] serial input (%d Bytes):", _debugIn); _msgOut->printf("%s serial input (%d Bytes):", _logId, _debugIn);
for (int i = 0; i < _debugIn; ++i) { for (int i = 0; i < _debugIn; ++i) {
if (i % 16 == 0) { if (i % 16 == 0) {
_msgOut->printf("\r\n[VE.Direct]"); _msgOut->printf("\r\n%s", _logId);
} }
_msgOut->printf(" %02x", _debugBuffer[i]); _msgOut->printf(" %02x", _debugBuffer[i]);
} }
@ -105,7 +107,7 @@ void VeDirectFrameHandler::loop()
// if such a large gap is observed, reset the state machine so it tries // if such a large gap is observed, reset the state machine so it tries
// to decode a new frame once more data arrives. // to decode a new frame once more data arrives.
if (IDLE != _state && _lastByteMillis + 500 < millis()) { if (IDLE != _state && _lastByteMillis + 500 < millis()) {
_msgOut->printf("[VE.Direct] Resetting state machine (was %d) after timeout\r\n", _state); _msgOut->printf("%s Resetting state machine (was %d) after timeout\r\n", _logId, _state);
if (_verboseLogging) { dumpDebugBuffer(); } if (_verboseLogging) { dumpDebugBuffer(); }
_checksum = 0; _checksum = 0;
_state = IDLE; _state = IDLE;
@ -123,7 +125,7 @@ void VeDirectFrameHandler::rxData(uint8_t inbyte)
_debugBuffer[_debugIn] = inbyte; _debugBuffer[_debugIn] = inbyte;
_debugIn = (_debugIn + 1) % _debugBuffer.size(); _debugIn = (_debugIn + 1) % _debugBuffer.size();
if (0 == _debugIn) { if (0 == _debugIn) {
_msgOut->println("[VE.Direct] ERROR: debug buffer overrun!"); _msgOut->printf("%s ERROR: debug buffer overrun!\r\n", _logId);
} }
} }
@ -201,7 +203,7 @@ void VeDirectFrameHandler::rxData(uint8_t inbyte)
{ {
bool valid = _checksum == 0; bool valid = _checksum == 0;
if (!valid) { if (!valid) {
_msgOut->printf("[VE.Direct] checksum 0x%02x != 0, invalid frame\r\n", _checksum); _msgOut->printf("%s checksum 0x%02x != 0, invalid frame\r\n", _logId, _checksum);
} }
if (_verboseLogging) { dumpDebugBuffer(); } if (_verboseLogging) { dumpDebugBuffer(); }
_checksum = 0; _checksum = 0;
@ -219,10 +221,10 @@ void VeDirectFrameHandler::rxData(uint8_t inbyte)
* textRxEvent * textRxEvent
* This function is called every time a new name/value is successfully parsed. It writes the values to the temporary buffer. * This function is called every time a new name/value is successfully parsed. It writes the values to the temporary buffer.
*/ */
bool VeDirectFrameHandler::textRxEvent(std::string const& who, char* name, char* value, veStruct& frame) { bool VeDirectFrameHandler::textRxEvent(char* name, char* value, veStruct& frame) {
if (_verboseLogging) { if (_verboseLogging) {
_msgOut->printf("[Victron %s] Text Event %s: Value: %s\r\n", _msgOut->printf("%s Text Event %s: Value: %s\r\n",
who.c_str(), name, value ); _logId, name, value );
} }
if (strcmp(name, "PID") == 0) { if (strcmp(name, "PID") == 0) {
@ -271,7 +273,7 @@ int VeDirectFrameHandler::hexRxEvent(uint8_t inbyte) {
default: default:
_hexSize++; _hexSize++;
if (_hexSize>=VE_MAX_HEX_LEN) { // oops -buffer overflow - something went wrong, we abort if (_hexSize>=VE_MAX_HEX_LEN) { // oops -buffer overflow - something went wrong, we abort
_msgOut->println("[VE.Direct] hexRx buffer overflow - aborting read"); _msgOut->printf("%s hexRx buffer overflow - aborting read\r\n", _logId);
_hexSize=0; _hexSize=0;
ret=IDLE; ret=IDLE;
} }

View File

@ -22,12 +22,13 @@
class VeDirectFrameHandler { class VeDirectFrameHandler {
public: public:
VeDirectFrameHandler();
virtual void init(int8_t rx, int8_t tx, Print* msgOut, bool verboseLogging, uint16_t hwSerialPort);
void loop(); // main loop to read ve.direct data void loop(); // main loop to read ve.direct data
uint32_t getLastUpdate() const; // timestamp of last successful frame read uint32_t getLastUpdate() const; // timestamp of last successful frame read
protected: protected:
VeDirectFrameHandler();
void init(char const* who, int8_t rx, int8_t tx, Print* msgOut, bool verboseLogging, uint16_t hwSerialPort);
bool _verboseLogging; bool _verboseLogging;
Print* _msgOut; Print* _msgOut;
uint32_t _lastUpdate; uint32_t _lastUpdate;
@ -43,7 +44,7 @@ protected:
frozen::string const& getPidAsString() const; // product ID as string frozen::string const& getPidAsString() const; // product ID as string
} veStruct; } veStruct;
bool textRxEvent(std::string const& who, char* name, char* value, veStruct& frame); bool textRxEvent(char* name, char* value, veStruct& frame);
bool isDataValid(veStruct const& frame) const; // return true if data valid and not outdated bool isDataValid(veStruct const& frame) const; // return true if data valid and not outdated
template<typename T, size_t L> template<typename T, size_t L>
@ -76,4 +77,5 @@ private:
std::array<uint8_t, 512> _debugBuffer; std::array<uint8_t, 512> _debugBuffer;
unsigned _debugIn; unsigned _debugIn;
uint32_t _lastByteMillis; uint32_t _lastByteMillis;
char _logId[32];
}; };

View File

@ -3,9 +3,8 @@
void VeDirectMpptController::init(int8_t rx, int8_t tx, Print* msgOut, bool verboseLogging, uint16_t hwSerialPort) void VeDirectMpptController::init(int8_t rx, int8_t tx, Print* msgOut, bool verboseLogging, uint16_t hwSerialPort)
{ {
VeDirectFrameHandler::init(rx, tx, msgOut, verboseLogging, hwSerialPort); VeDirectFrameHandler::init("MPPT", rx, tx, msgOut, verboseLogging, hwSerialPort);
_spData = std::make_shared<veMpptStruct>(); _spData = std::make_shared<veMpptStruct>();
if (_verboseLogging) { _msgOut->println("Finished init MPPTController"); }
} }
bool VeDirectMpptController::isDataValid() const { bool VeDirectMpptController::isDataValid() const {
@ -14,7 +13,7 @@ bool VeDirectMpptController::isDataValid() const {
void VeDirectMpptController::textRxEvent(char* name, char* value) void VeDirectMpptController::textRxEvent(char* name, char* value)
{ {
if (VeDirectFrameHandler::textRxEvent("MPPT", name, value, _tmpFrame)) { if (VeDirectFrameHandler::textRxEvent(name, value, _tmpFrame)) {
return; return;
} }

View File

@ -9,15 +9,12 @@ VeDirectShuntController::VeDirectShuntController()
void VeDirectShuntController::init(int8_t rx, int8_t tx, Print* msgOut, bool verboseLogging) void VeDirectShuntController::init(int8_t rx, int8_t tx, Print* msgOut, bool verboseLogging)
{ {
VeDirectFrameHandler::init(rx, tx, msgOut, verboseLogging, 2); VeDirectFrameHandler::init("SmartShunt", rx, tx, msgOut, verboseLogging, 2);
if (_verboseLogging) {
_msgOut->println("Finished init ShuntController");
}
} }
void VeDirectShuntController::textRxEvent(char* name, char* value) void VeDirectShuntController::textRxEvent(char* name, char* value)
{ {
if (VeDirectFrameHandler::textRxEvent("SmartShunt", name, value, _tmpFrame)) { if (VeDirectFrameHandler::textRxEvent(name, value, _tmpFrame)) {
return; return;
} }