Added separate receive line function with timeout and removed terminating string from received line

This commit is contained in:
BlackMark 2016-05-25 23:38:02 +02:00
parent c261d7d309
commit 7fd6d8ddec
2 changed files with 37 additions and 1 deletions

View File

@ -384,6 +384,41 @@ bool USART0::receiveLine( char *szBuffer, size_t sizeBufferLength, const char *s
if( strstr( szBuffer, szLineTerminator ) ) 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; return true;
} }
} }

View File

@ -1,7 +1,7 @@
/* /*
* Copyright (c) by BlackMark 2015-2016 * Copyright (c) by BlackMark 2015-2016
* Date 25/05/2016 * Date 25/05/2016
* Version 3.1 * Version 3.2
*/ */
#ifndef USART_H #ifndef USART_H
@ -145,6 +145,7 @@ public:
bool receiveByte( uint8_t &ui8Data ); bool receiveByte( uint8_t &ui8Data );
bool receiveByte( uint8_t &ui8Data, uint16_t ui16TimeoutMS ); 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, const char *szLineTerminator = "\r\n" );
bool receiveLine( char *szBuffer, size_t sizeBufferLength, uint16_t ui16TimeoutMS, const char *szLineTerminator = "\r\n" );
void flushReceive(); void flushReceive();
void transmitByte( uint8_t ui8Data ); void transmitByte( uint8_t ui8Data );