Skip to content

Commit cd2f6b7

Browse files
committed
Fix buffer fill while receiving command chunks
Due to the nature of serial communication, during reads, sent command can be splitted. ie. you will not receive whole command in one row. This commit fixes the way serial byte are stored in buffer: it now fill buffer using an saved offset, reset that offset when m_endChar is reached or buffer overflow. Please note that when buffer overflow occured, you will loose whole buffer and restart to fill buffer like the behavior before this commit.
1 parent 9438735 commit cd2f6b7

File tree

2 files changed

+10
-14
lines changed

2 files changed

+10
-14
lines changed

src/CmdBuffer.cpp

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,15 @@ bool CmdBufferObject::readFromSerial(Stream *serial, uint32_t timeOut)
1010
{
1111
uint32_t isTimeOut;
1212
uint32_t startTime;
13-
size_t readPtr;
1413
uint8_t readChar;
15-
uint8_t *buffer = this->getBuffer();
16-
bool over = false;
14+
uint8_t *buffer = this->getBuffer();
15+
bool over = false;
1716

1817
// UART initialize?
1918
if (serial == NULL) {
2019
return false;
2120
}
2221

23-
////
24-
// init buffers
25-
this->clear();
26-
27-
// init Counter
28-
readPtr ^= readPtr;
29-
3022
////
3123
// Calc Timeout
3224
if (timeOut != 0) {
@@ -49,7 +41,8 @@ bool CmdBufferObject::readFromSerial(Stream *serial, uint32_t timeOut)
4941
while (serial->available()) {
5042

5143
// is buffer full?
52-
if (readPtr >= this->getBufferSize()) {
44+
if (m_dataOffset >= this->getBufferSize()) {
45+
m_dataOffset = 0;
5346
return false;
5447
}
5548

@@ -58,12 +51,14 @@ bool CmdBufferObject::readFromSerial(Stream *serial, uint32_t timeOut)
5851

5952
// is that the end of command
6053
if (readChar == m_endChar) {
54+
buffer[m_dataOffset] = '\0';
55+
m_dataOffset = 0;
6156
return true;
6257
}
6358

6459
// is a printable character
6560
if (readChar > CMDBUFFER_CHAR_PRINTABLE) {
66-
buffer[readPtr++] = readChar;
61+
buffer[m_dataOffset++] = readChar;
6762
}
6863
}
6964

src/CmdBuffer.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class CmdBufferObject
2626
/**
2727
* Clear buffer and set defaults.
2828
*/
29-
CmdBufferObject() : m_endChar(CMDBUFFER_CHAR_LF) {}
29+
CmdBufferObject() : m_endChar(CMDBUFFER_CHAR_LF), m_dataOffset(0) {}
3030

3131
/**
3232
* Read data from serial communication to buffer. It read only printable
@@ -67,7 +67,7 @@ class CmdBufferObject
6767
virtual void clear() = 0;
6868

6969
/**
70-
* Return a 0x00 terminatet string
70+
* Return a null-terminated ('\0') string
7171
*
7272
* @return String from Buffer
7373
*/
@@ -83,6 +83,7 @@ class CmdBufferObject
8383
private:
8484
/** Character for handling the end of serial data communication */
8585
uint8_t m_endChar;
86+
size_t m_dataOffset;
8687
};
8788

8889
/**

0 commit comments

Comments
 (0)