From 89344d1cd53402335d828eb2b46eda5aa2984a8b Mon Sep 17 00:00:00 2001 From: xinyang <895639507@qq.com> Date: Tue, 16 Jul 2019 12:15:14 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=B8=B2=E5=8F=A3=E6=8E=A5?= =?UTF-8?q?=E6=94=B6=E6=96=B9=E5=BC=8F=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- others/src/additions/additions.cpp | 19 +++++-------------- others/src/serial/serial.cpp | 2 +- 2 files changed, 6 insertions(+), 15 deletions(-) diff --git a/others/src/additions/additions.cpp b/others/src/additions/additions.cpp index 2661127..6325ec0 100644 --- a/others/src/additions/additions.cpp +++ b/others/src/additions/additions.cpp @@ -30,27 +30,18 @@ extern ArmorFinder armorFinder; extern Energy energy; void uartReceive(Serial *pSerial) { - char buffer[20]; - int cnt = 0; + char buffer[30]; LOGM(STR_CTR(WORD_LIGHT_WHITE, "data receive start!")); while (true) { char byte = 0; memset(buffer, 0, sizeof(buffer)); - while (pSerial->ReadData((uint8_t *) &byte, 1) && byte != '\n') { - buffer[cnt++] = byte; - if (cnt >= sizeof(buffer)) { -// LOGE("data receive over flow!"); - cnt = 0; - } - } - if (cnt == 0 && byte == '\n') { - LOGM("%d", cnt); - } - if (cnt == sizeof(mcuData)) { + pSerial->ReadData((uint8_t *) &byte, sizeof(mcuData)+1); + if (buffer[sizeof(mcuData)] == '\n') { memcpy(&mcuData, buffer, sizeof(mcuData)); LOGM("Get, state:%c, mark:%d!", mcuData.state, (int) mcuData.mark); + }else{ + LOGW("corrupt data!"); } - cnt = 0; } } diff --git a/others/src/serial/serial.cpp b/others/src/serial/serial.cpp index 5192001..a2fe4ec 100644 --- a/others/src/serial/serial.cpp +++ b/others/src/serial/serial.cpp @@ -191,7 +191,7 @@ Serial::Serial(int nSpeed, char nEvent, int nBits, int nStop) : nSpeed(nSpeed), nEvent(nEvent), nBits(nBits), nStop(nStop) { if (wait_uart) { LOGM("Wait for serial be ready!"); - while (InitPort(nSpeed, nEvent, nBits, nStop) == false); + while (!InitPort(nSpeed, nEvent, nBits, nStop)); LOGM("Port set successfully!"); } else { if (InitPort(nSpeed, nEvent, nBits, nStop)) {