Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 568bb38

Browse files
committed
Fix sendCommandAndValidate always sending CONTROL
- Search for magic byte and specific reply in findAndReadReply instead of only searching for magic byte
1 parent 8f02aa7 commit 568bb38

File tree

2 files changed

+23
-29
lines changed

2 files changed

+23
-29
lines changed

src/NETSGPClient.cpp

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ NETSGPClient::InverterStatus NETSGPClient::getStatus(const uint32_t deviceID)
1414
{
1515
sendCommand(deviceID, Command::STATUS);
1616
InverterStatus status;
17-
if (waitForMessage() && findAndReadReply())
17+
if (waitForMessage() && findAndReadReply(Command::STATUS))
1818
{
1919
#ifdef DEBUG_SERIAL
2020
for (uint8_t i = 0; i < 32; i++)
@@ -168,8 +168,8 @@ void NETSGPClient::sendCommand(const uint32_t deviceID, const Command command, c
168168

169169
bool NETSGPClient::sendCommandAndValidate(const uint32_t deviceID, const Command command, const uint8_t value)
170170
{
171-
sendCommand(deviceID, Command::CONTROL, value);
172-
if (waitForMessage() && findAndReadReply())
171+
sendCommand(deviceID, command, value);
172+
if (waitForMessage() && findAndReadReply(command))
173173
{
174174
#ifdef DEBUG_SERIAL
175175
for (uint8_t i = 0; i < 32; i++)
@@ -204,35 +204,28 @@ bool NETSGPClient::waitForMessage()
204204
return false;
205205
}
206206

207-
bool NETSGPClient::findAndReadReply()
207+
bool NETSGPClient::findAndReadReply(const Command command)
208208
{
209-
// as long as we have data for at least a control or power grade reply keep on looking
210-
while (mStream.available() >= 16)
209+
// Search for a reply header consisting of magic byte and one of the command bytes
210+
const char header[2] = {MAGIC_BYTE, command};
211+
if (!mStream.find(&header[0], 2))
211212
{
212-
// Search for a reply header consisting of magic byte and one of the command bytes
213-
if (!mStream.find(MAGIC_BYTE))
214-
{
215-
DEBUGLN("[findAndReadReply] Could not find magic byte");
216-
return false;
217-
}
213+
DEBUGLN("[findAndReadReply] Could not find header");
214+
return false;
215+
}
218216

219-
const uint8_t command = mStream.read();
220-
switch (command)
221-
{
222-
case Command::STATUS:
223-
// whole message is 27 bytes
224-
return mStream.readBytes(&mBuffer[2], 25) == 25;
225-
case Command::CONTROL:
226-
case Command::POWER_GRADE:
227-
// whole message is 16 bytes
228-
return mStream.readBytes(&mBuffer[2], 14) == 14;
229-
// default:
230-
// // technically we need to search for a magic byte again before we can say that no reply is found
231-
// DEBUGF("[findAndReadReply] Unexpected command 0x%02X", command);
232-
// return false;
233-
}
217+
switch (command)
218+
{
219+
case Command::STATUS:
220+
// whole message is 27 bytes
221+
return mStream.readBytes(&mBuffer[2], 25) == 25;
222+
case Command::CONTROL:
223+
case Command::POWER_GRADE:
224+
// whole message is 16 bytes
225+
return mStream.readBytes(&mBuffer[2], 14) == 14;
234226
}
235-
DEBUGLN("[findAndReadReply] Could not find command");
227+
228+
DEBUGLN("[findAndReadReply] Unknown command");
236229
return false;
237230
}
238231

src/NETSGPClient.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,9 +315,10 @@ class NETSGPClient
315315

316316
/// @brief Try to find a reply in the stream and if present read it into mBuffer
317317
///
318+
/// @param command Expected command of reply
318319
/// @return true If reply was found and read into mBuffer
319320
/// @return false If not
320-
bool findAndReadReply();
321+
bool findAndReadReply(const Command command);
321322

322323
/// @brief Calculate the checksum for a message inside the buffer.
323324
///

0 commit comments

Comments
 (0)