@@ -14,7 +14,7 @@ NETSGPClient::InverterStatus NETSGPClient::getStatus(const uint32_t deviceID)
14
14
{
15
15
sendCommand (deviceID, Command::STATUS);
16
16
InverterStatus status;
17
- if (waitForMessage () && findAndReadReply ())
17
+ if (waitForMessage () && findAndReadReply (Command::STATUS ))
18
18
{
19
19
#ifdef DEBUG_SERIAL
20
20
for (uint8_t i = 0 ; i < 32 ; i++)
@@ -168,8 +168,8 @@ void NETSGPClient::sendCommand(const uint32_t deviceID, const Command command, c
168
168
169
169
bool NETSGPClient::sendCommandAndValidate (const uint32_t deviceID, const Command command, const uint8_t value)
170
170
{
171
- sendCommand (deviceID, Command::CONTROL , value);
172
- if (waitForMessage () && findAndReadReply ())
171
+ sendCommand (deviceID, command , value);
172
+ if (waitForMessage () && findAndReadReply (command ))
173
173
{
174
174
#ifdef DEBUG_SERIAL
175
175
for (uint8_t i = 0 ; i < 32 ; i++)
@@ -204,35 +204,28 @@ bool NETSGPClient::waitForMessage()
204
204
return false ;
205
205
}
206
206
207
- bool NETSGPClient::findAndReadReply ()
207
+ bool NETSGPClient::findAndReadReply (const Command command )
208
208
{
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 ))
211
212
{
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
+ }
218
216
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 ;
234
226
}
235
- DEBUGLN (" [findAndReadReply] Could not find command" );
227
+
228
+ DEBUGLN (" [findAndReadReply] Unknown command" );
236
229
return false ;
237
230
}
238
231
0 commit comments