Skip to content

Commit 438ae2b

Browse files
committed
Fixed: inputString maybe contain multiple commands
1 parent 8920835 commit 438ae2b

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

examples/canbus-monitor/can-serial.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ void Can232::initFunc() {
8787
// lw232TimeStamp = //read from eeprom
8888
// lw232Message[0] = 'Z'; lw232Message[1] = '1'; exec();
8989
//if (lw232AutoStart) {
90-
inputString = "O\0x0D";
90+
inputString = "O\x0D";
9191
stringComplete = true;
9292
loopFunc();
9393
//}
@@ -101,11 +101,16 @@ void Can232::loopFunc() {
101101
if (stringComplete) {
102102
unsigned len = inputString.length();
103103
if (len > 0 && len < LW232_FRAME_MAX_SIZE) {
104-
strcpy((char*)lw232Message, inputString.c_str());
105-
exec();
104+
// maybe not single commands
105+
int cr_pos;
106+
for (; (cr_pos = inputString.indexOf(LW232_CR)) >= 0;) {
107+
strncpy((char*)lw232Message, inputString.c_str(), cr_pos + 1);
108+
lw232Message[cr_pos] = '\0';
109+
exec();
110+
inputString = inputString.substring(cr_pos + 1);
111+
}
106112
}
107113
// clear the string:
108-
inputString = "";
109114
stringComplete = false;
110115
}
111116
if (lw232CanChannelMode != LW232_STATUS_CAN_CLOSED) {
@@ -130,7 +135,6 @@ void Can232::serialEventFunc() {
130135
}
131136

132137
INT8U Can232::exec() {
133-
dbg2("Command received:", inputString);
134138
lw232LastErr = parseAndRunCommand();
135139
switch (lw232LastErr) {
136140
case LW232_OK:
@@ -162,6 +166,8 @@ INT8U Can232::parseAndRunCommand() {
162166

163167
lw232LastErr = LW232_OK;
164168

169+
// __debug_buf("RX:", (char*)lw232Message, strlen((char*)lw232Message));
170+
165171
switch (lw232Message[0]) {
166172
case LW232_CMD_SETUP:
167173
// Sn[CR] Setup with standard CAN bit-rates where n is 0-9.

0 commit comments

Comments
 (0)