Skip to content

Commit 5d0a761

Browse files
authored
Refactor getResponse method in ModuleComm class
fix missing messages.
1 parent 0e62b27 commit 5d0a761

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

src/utils/comm.cpp

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,29 +37,33 @@ ModuleComm::Respond_t ModuleComm::getResponse(uint32_t timeout)
3737
{
3838
Respond_t ret;
3939
ret.time_out = false;
40-
String buffer;
40+
static String buffer;
4141
uint32_t startTime = millis();
42-
int openBraces = 0;
42+
static int openBraces = 0;
4343
bool started = false;
4444

4545
while (millis() - startTime < timeout) {
4646
while (_serial->available()) {
4747
char c = (char)_serial->read();
48-
buffer += c;
49-
if (c == '{') {
50-
started = true;
51-
openBraces++;
52-
} else if (c == '}') {
53-
openBraces--;
54-
if (started && openBraces == 0) {
55-
ret.msg = buffer;
56-
return ret;
48+
if(c != '\n'){
49+
buffer += c;
50+
if(c == '{'){
51+
openBraces ++;
52+
}else if (c == '}') {
53+
openBraces --;
54+
if(openBraces == 0){
55+
ret.msg = buffer;
56+
buffer.clear();
57+
return ret;
58+
}else if (openBraces < 0) {
59+
openBraces = 0;
60+
buffer.clear();
61+
}
5762
}
5863
}
59-
60-
if (c == '\n' && !started) {
61-
ret.msg = buffer;
62-
return ret;
64+
else{
65+
openBraces = 0;
66+
buffer.clear();
6367
}
6468
}
6569
delay(1);
@@ -96,3 +100,4 @@ void m5_module_llm::SerialPassthroughUpdate(Stream* port1, Stream* port2, bool p
96100
}
97101
}
98102
}
103+

0 commit comments

Comments
 (0)