From 7fd6d8ddec2b51ddd3453f3b6671846c32e696ed Mon Sep 17 00:00:00 2001 From: BlackMark Date: Wed, 25 May 2016 23:38:02 +0200 Subject: [PATCH] Added separate receive line function with timeout and removed terminating string from received line --- usart.cpp | 35 +++++++++++++++++++++++++++++++++++ usart.h | 3 ++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/usart.cpp b/usart.cpp index 23f347d..9711f5a 100644 --- a/usart.cpp +++ b/usart.cpp @@ -384,6 +384,41 @@ bool USART0::receiveLine( char *szBuffer, size_t sizeBufferLength, const char *s if( strstr( szBuffer, szLineTerminator ) ) { + szBuffer[sizeReceived - strlen( szLineTerminator )] = '\0'; + return true; + } + } + + return false; +} + +////////////////////////////////////////////////////////////////////////// +bool USART0::receiveLine( char *szBuffer, size_t sizeBufferLength, uint16_t ui16TimeoutMS, const char *szLineTerminator /* = "\r\n" */ ) +{ + size_t sizeReceived = 0; + + while( sizeReceived < sizeBufferLength - 1 ) + { + uint8_t ui8ReceiveByte; + uint16_t ui16DelayCounter = 0; + + while( !receiveByte( ui8ReceiveByte ) ) + { + _delay_ms( 1 ); + + if( ui16DelayCounter++ > ui16TimeoutMS ) + { + szBuffer[sizeReceived] = '\0'; + return false; + } + } + + szBuffer[sizeReceived++] = ui8ReceiveByte; + szBuffer[sizeReceived] = '\0'; + + if( strstr( szBuffer, szLineTerminator ) ) + { + szBuffer[sizeReceived - strlen( szLineTerminator )] = '\0'; return true; } } diff --git a/usart.h b/usart.h index a80d174..dfa6f31 100644 --- a/usart.h +++ b/usart.h @@ -1,7 +1,7 @@ /* * Copyright (c) by BlackMark 2015-2016 * Date 25/05/2016 -* Version 3.1 +* Version 3.2 */ #ifndef USART_H @@ -145,6 +145,7 @@ public: bool receiveByte( uint8_t &ui8Data ); bool receiveByte( uint8_t &ui8Data, uint16_t ui16TimeoutMS ); bool receiveLine( char *szBuffer, size_t sizeBufferLength, const char *szLineTerminator = "\r\n" ); + bool receiveLine( char *szBuffer, size_t sizeBufferLength, uint16_t ui16TimeoutMS, const char *szLineTerminator = "\r\n" ); void flushReceive(); void transmitByte( uint8_t ui8Data );