Skip to content

Commit 0e476db

Browse files
committed
VE.Direct: collect serial input into buffer and print
should help debug issues for users.
1 parent a571a07 commit 0e476db

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

lib/VeDirectFrameHandler/VeDirectFrameHandler.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ VeDirectFrameHandler::VeDirectFrameHandler() :
7070
_name(""),
7171
_value(""),
7272
_tmpFrame(),
73+
_debugIn(0),
7374
_lastByteMillis(0),
7475
_lastUpdate(0)
7576
{
@@ -82,6 +83,18 @@ void VeDirectFrameHandler::init(int8_t rx, int8_t tx, Print* msgOut)
8283
_msgOut = msgOut;
8384
}
8485

86+
void VeDirectFrameHandler::dumpDebugBuffer() {
87+
_msgOut->printf("[VE.Direct] serial input:");
88+
for (int i = 0; i < _debugIn; ++i) {
89+
if (i % 16 == 0) {
90+
_msgOut->printf("\r\n[VE.Direct] ");
91+
}
92+
_msgOut->printf("%02x ", _debugBuffer[i]);
93+
}
94+
_msgOut->println("");
95+
_debugIn = 0;
96+
}
97+
8598
void VeDirectFrameHandler::loop()
8699
{
87100
while ( VedirectSerial.available()) {
@@ -94,6 +107,7 @@ void VeDirectFrameHandler::loop()
94107
// to decode a new frame once more data arrives.
95108
if (IDLE != _state && _lastByteMillis + 500 < millis()) {
96109
_msgOut->printf("[VE.Direct] Resetting state machine (was %d) after timeout\r\n", _state);
110+
dumpDebugBuffer();
97111
_checksum = 0;
98112
_state = IDLE;
99113
_tmpFrame = { };
@@ -107,6 +121,12 @@ void VeDirectFrameHandler::loop()
107121
*/
108122
void VeDirectFrameHandler::rxData(uint8_t inbyte)
109123
{
124+
_debugBuffer[_debugIn] = inbyte;
125+
_debugIn = (_debugIn + 1) % _debugBuffer.size();
126+
if (0 == _debugIn) {
127+
_msgOut->println("[VE.Direct] ERROR: debug buffer overrun!");
128+
}
129+
110130
//if (mStop) return;
111131
if ( (inbyte == ':') && (_state != CHECKSUM) ) {
112132
_prevState = _state; //hex frame can interrupt TEXT
@@ -268,6 +288,9 @@ void VeDirectFrameHandler::textRxEvent(char * name, char * value) {
268288
* is created in the public buffer.
269289
*/
270290
void VeDirectFrameHandler::frameEndEvent(bool valid) {
291+
// TODO this should be toggled by a "verbose logging" switch
292+
dumpDebugBuffer();
293+
271294
if ( valid ) {
272295
_tmpFrame.P = _tmpFrame.V * _tmpFrame.I;
273296

lib/VeDirectFrameHandler/VeDirectFrameHandler.h

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#pragma once
1313

1414
#include <Arduino.h>
15+
#include <array>
1516

1617
#ifndef VICTRON_PIN_TX
1718
#define VICTRON_PIN_TX 21 // HardwareSerial TX Pin
@@ -101,6 +102,7 @@ class VeDirectFrameHandler {
101102

102103
private:
103104
void setLastUpdate(); // set timestampt after successful frame read
105+
void dumpDebugBuffer();
104106
void rxData(uint8_t inbyte); // byte of serial data
105107
void textRxEvent(char *, char *);
106108
void frameEndEvent(bool); // copy temp struct to public struct
@@ -117,6 +119,8 @@ class VeDirectFrameHandler {
117119
char _value[VE_MAX_VALUE_LEN]; // buffer for the field value
118120
veStruct _tmpFrame{}; // private struct for received name and value pairs
119121
MovingAverage<double, 5> _efficiency;
122+
std::array<uint8_t, 512> _debugBuffer;
123+
unsigned _debugIn;
120124
uint32_t _lastByteMillis;
121125
uint32_t _lastUpdate;
122126
};

0 commit comments

Comments
 (0)