From 438ae2bb21fe597dd4851854ee811e5ea1cf086d Mon Sep 17 00:00:00 2001 From: turmary Date: Mon, 1 Feb 2021 20:27:53 +0800 Subject: [PATCH] Fixed: inputString maybe contain multiple commands --- examples/canbus-monitor/can-serial.cpp | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/examples/canbus-monitor/can-serial.cpp b/examples/canbus-monitor/can-serial.cpp index 6c3687b..92c2972 100644 --- a/examples/canbus-monitor/can-serial.cpp +++ b/examples/canbus-monitor/can-serial.cpp @@ -87,7 +87,7 @@ void Can232::initFunc() { // lw232TimeStamp = //read from eeprom // lw232Message[0] = 'Z'; lw232Message[1] = '1'; exec(); //if (lw232AutoStart) { - inputString = "O\0x0D"; + inputString = "O\x0D"; stringComplete = true; loopFunc(); //} @@ -101,11 +101,16 @@ void Can232::loopFunc() { if (stringComplete) { unsigned len = inputString.length(); if (len > 0 && len < LW232_FRAME_MAX_SIZE) { - strcpy((char*)lw232Message, inputString.c_str()); - exec(); + // maybe not single commands + int cr_pos; + for (; (cr_pos = inputString.indexOf(LW232_CR)) >= 0;) { + strncpy((char*)lw232Message, inputString.c_str(), cr_pos + 1); + lw232Message[cr_pos] = '\0'; + exec(); + inputString = inputString.substring(cr_pos + 1); + } } // clear the string: - inputString = ""; stringComplete = false; } if (lw232CanChannelMode != LW232_STATUS_CAN_CLOSED) { @@ -130,7 +135,6 @@ void Can232::serialEventFunc() { } INT8U Can232::exec() { - dbg2("Command received:", inputString); lw232LastErr = parseAndRunCommand(); switch (lw232LastErr) { case LW232_OK: @@ -162,6 +166,8 @@ INT8U Can232::parseAndRunCommand() { lw232LastErr = LW232_OK; + // __debug_buf("RX:", (char*)lw232Message, strlen((char*)lw232Message)); + switch (lw232Message[0]) { case LW232_CMD_SETUP: // Sn[CR] Setup with standard CAN bit-rates where n is 0-9.