Commit 005c66a6 authored by kirov's avatar kirov
Browse files

+ HwSerialLine methods default implementation.



git-svn-id: https://scm.blissgarden.org/svn/lima/trunk@59 45c4679d-1946-429d-baad-37912b19538b
parent 44ad9ca8
......@@ -8,6 +8,11 @@
namespace lima {
#define TMOUT_DEFAULT -2
#define TMOUT_BLOCK_FOREVER -1
#define TMOUT_NO_BLOCK 0
class HwSerialLine
{
public :
......@@ -15,24 +20,36 @@ class HwSerialLine
virtual ~HwSerialLine();
virtual void getAvail( int &avail ) = 0;
virtual void read( std::string& buffer, int& len, double timeout ) = 0;
virtual void write( const std::string& buffer, int block_size = 0,
double block_delay = 0, bool no_wait = false ) = 0;
double block_delay=0, bool no_wait=false ) = 0;
virtual void readStr( std::string& buffer, int max_len,
const std::string& term, double timeout );
virtual void readLine( std::string& buffer, int max_len,
double timeout );
const std::string& term, double timeout );
virtual void readLine( std::string& buffer, int max_len, double timeout );
virtual void writeRead( const std::string& writebuffer, int block_size,
double block_delay, bool no_wait,
std::string& readbuffer, /*int max_len, ???*/
double timeout );
virtual void writeReadStr( const std::string& writebuffer,
int block_size, double block_delay,
bool no_wait, std::string& readbuffer,
int max_len, const std::string& term,
double timeout );
virtual void writeRead( );
virtual void writeReadStr();
virtual void readAvailable( std::string& buffer, /*int max_len, ???*/
double timeout );
virtual void flush();
virtual void readAvailable();
private :
char m_line_term;
double m_timeout;
double m_timeout; /* Will be used with TMOUT_DEFAULT */
};
......
......@@ -5,12 +5,6 @@ using namespace lima;
using namespace std;
#define check_null(ptr) if( NULL == ptr ) \
{\
throw LIMA_HW_EXC(InvalidValue, #ptr " is NULL");\
}
HwSerialLine::HwSerialLine( char line_term, double timeout ) :
m_line_term(line_term),
m_timeout(timeout)
......@@ -30,23 +24,24 @@ void HwSerialLine::readStr( string& buffer, int max_len,
const string& term, double timeout )
{
Timestamp start=Timestamp::now();
int match=0, n, term_len=term.length();
bool have_timeout = (timeout > 0);
int match=0, n, term_len=term.length(), len=0;
bool have_timeout=(timeout > 0), have_maxlen=(max_len > 0);
double tout=timeout;
string buf;
buffer = "";
while( ((!have_timeout) || (tout > 0)) && (buffer.length() < max_len) ) {
while( ((!have_timeout) || (tout > 0)) &&
((!have_maxlen) || (len < max_len)) ) {
n = 1;
read( buf, n, tout );
if( 0 == n )
break; // ???
buffer += buf;
len += n;
if( 0 == term.compare(match, n, buf) ) {
match += n;
if( match == term_len ) {
if( match == term_len )
return;
}
} else {
match = 0;
}
......@@ -68,35 +63,57 @@ void HwSerialLine::readLine( string& buffer, int max_len, double timeout )
/**
* @brief Write and then immediately Read the serial line until available?
*/
void HwSerialLine::writeRead()
void HwSerialLine::writeRead( const std::string& writebuffer, int block_size,
double block_delay, bool no_wait,
std::string& readbuffer, /*int max_len, ???*/
double timeout )
{
write( writebuffer, block_size, block_delay, no_wait );
readAvailable( readbuffer, timeout );
// Or: read( readbuffer, max_len, timeout );
}
/**
* @brief Write and then immediately Read the serial line until term.
*/
void HwSerialLine::writeReadStr()
void HwSerialLine::writeReadStr( const std::string& writebuffer,
int block_size, double block_delay,
bool no_wait, std::string& readbuffer,
int max_len, const std::string& term,
double timeout )
{
write( writebuffer, block_size, block_delay, no_wait );
readStr( readbuffer, max_len, term, timeout );
}
/**
* @brief Flush the serial RX buffer
* @brief Read the serial line until there is something to read
*/
void HwSerialLine::flush()
void HwSerialLine::readAvailable( std::string& buffer, /*int max_len, ???*/
double timeout )
{
// Get available
// Read until there is something to read and thow it away
int max_len;
getAvail( max_len );
read( buffer, max_len, timeout );
}
/**
* @brief Read the serial line until there is something to read
* @brief Flush the serial RX buffer
*/
void HwSerialLine::readAvailable()
void HwSerialLine::flush()
{
// Get available
// Read until there is something to read
string buf;
int n=1; // We don't want to waste memory reading all that is available
while( n ) {
n = 1;
read(buf, n, TMOUT_NO_BLOCK); // timeout ???
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment