Commit d7a50a74 authored by kirov's avatar kirov
Browse files

+ getAvail -> getNumAvailBytes

+ read(int &len) -> read(int max_len)
+ add EspiaDev &m_dev as a private member of EspiaSerialLine class
+ move the serial line code from EspiaDev to EspiaSerialLine
+ change #define TMOUT to an enum inside of the class
+ m_line_term to be a string not char
+ get/set m_timeout, m_line_term - setTimeout, setLineTerm (virtual)
+ read() is blocking, while readAvail() is non-blocking; add max_len to
 readAvail() and remove the timeout (set it to TMOUT_NO_BLOCK internaly)
+ default timeout in read? yes, but we need to state the meaning of -2 clearly.
  or no timeout in the methods at all, use only m_timeout? no.
+ flush() can read bigger chunks, or right on the number of available bytes
+ remove block_size and block_delay as a parameters of the methods, add them
  as a private variables (with get/set functions) and use them instead
+ group default parameters at the end



git-svn-id: https://scm.blissgarden.org/svn/lima/trunk@62 45c4679d-1946-429d-baad-37912b19538b
parent 44910e3d
......@@ -20,15 +20,6 @@ class EspiaDev : public Espia
void registerCallback(struct espia_cb_data& cb_data, int& cb_nr);
void unregisterCallback(int& cb_nr);
void serWrite(const std::string& buffer,
int block_size = 0, double block_delay = 0,
bool no_wait = false);
void serRead(std::string& buffer, int len, double timeout);
void serReadStr(std::string& buffer, int len,
const std::string& term, double timeout);
void serFlush();
void serGetAvailableBytes(int& available_bytes);
private:
void open(int dev_nb);
void close();
......
......@@ -4,6 +4,7 @@
#include <string>
#include "Exceptions.h"
#include "HwSerialLine.h"
#include "EspiaDev.h"
namespace lima {
......@@ -12,18 +13,27 @@ namespace lima {
class EspiaSerialLine : public HwSerialLine
{
public :
EspiaSerialLine( char line_term='\r', double timeout=1.0 );
EspiaSerialLine( EspiaDev& edev, const std::string& line_term="\r",
double timeout=1.0, int block_size=0,
double block_delay=0 );
~EspiaSerialLine();
void serialGetAvail( int &avail );
void serialRead( std::string& buffer, int& len, double timeout );
void serialWrite( const std::string& buffer, int block_size = 0,
double block_delay = 0, bool no_wait = false );
virtual void write( const std::string& buffer, bool no_wait=false );
void serialFlush();
virtual void read( std::string& buffer, int max_len,
double timeout=TMOUT_DEFAULT );
private :
virtual void readStr( std::string& buffer, int max_len,
const std::string& term,
double timeout=TMOUT_DEFAULT );
void flush();
virtual void getNumAvailBytes( int &avail_bytes );
private :
EspiaDev& m_dev;
};
......
......@@ -61,46 +61,3 @@ void EspiaDev::unregisterCallback(int& cb_nr)
cb_nr = Invalid;
}
void EspiaDev::serWrite(const string& buffer, int block_size, double block_delay,
bool no_wait)
{
unsigned long len = buffer.length();
char *ptr = len ? (char *) buffer.data() : NULL;
CHECK_CALL(espia_ser_write(m_dev, ptr, &len, block_size,
sec2usec(block_delay), !no_wait));
}
void EspiaDev::serRead(string& buffer, int len, double timeout)
{
buffer.resize(len);
char *ptr = len ? (char *) buffer.data() : NULL;
unsigned long ret_len = len;
CHECK_CALL(espia_ser_read(m_dev, ptr, &ret_len, sec2usec(timeout)));
buffer.resize(ret_len);
}
void EspiaDev::serReadStr(string& buffer, int len, const string& term,
double timeout)
{
buffer.resize(len);
char *ptr = len ? (char *) buffer.data() : NULL;
char *term_ptr = (char *) term.data();
unsigned long ret_len = len;
CHECK_CALL(espia_ser_read_str(m_dev, ptr, &ret_len, term_ptr,
term.length(), sec2usec(timeout)));
buffer.resize(ret_len);
}
void EspiaDev::serFlush()
{
CHECK_CALL(espia_ser_flush(m_dev));
}
void EspiaDev::serGetAvailableBytes(int& available_bytes)
{
unsigned long ret_bytes = 0;
CHECK_CALL(espia_ser_read(m_dev, NULL, &ret_bytes, 0));
available_bytes = ret_bytes;
}
#include "EspiaSerialLine.h"
#include "Espia.h"
using namespace lima;
using namespace std;
#define CHECK_CALL(ret) ESPIA_CHECK_CALL(ret)
#define check_null(ptr) if( NULL == ptr ) \
{\
throw LIMA_HW_EXC(InvalidValue, #ptr " is NULL");\
EspiaSerialLine::EspiaSerialLine( EspiaDev& edev, const string& line_term,
double timeout, int block_size,
double block_delay ) :
m_dev(edev),
HwSerialLine(line_term, timeout, block_size, block_delay)
{
}
EspiaSerialLine::EspiaSerialLine( char line_term, double timeout ) :
HwSerialLine(line_term, timeout)
EspiaSerialLine::~EspiaSerialLine()
{
}
EspiaSerialLine::~EspiaSerialLine()
void EspiaSerialLine::write( const string& buffer, bool no_wait )
{
unsigned long len = buffer.length();
char *ptr = len ? (char *) buffer.data() : NULL;
int block_size; getBlockSize(block_size);
double block_delay; getBlockDelay(block_delay);
CHECK_CALL( espia_ser_write(m_dev, ptr, &len, block_size,
m_dev.sec2usec(block_delay), !no_wait) );
}
void EspiaSerialLine::serialGetAvail( int &avail )
void EspiaSerialLine::read( string& buffer, int max_len, double timeout )
{
buffer.resize(max_len);
char *ptr = max_len ? (char *) buffer.data() : NULL;
unsigned long ret_len = max_len;
unsigned long tmout = m_dev.sec2usec(checkDefTimeout(timeout));
CHECK_CALL( espia_ser_read(m_dev, ptr, &ret_len, tmout) );
buffer.resize(ret_len);
}
void EspiaSerialLine::serialRead( string& buffer, int& len, double timeout )
void EspiaSerialLine::readStr( string& buffer, int max_len,
const string& term, double timeout )
{
buffer.resize(max_len);
char *ptr = max_len ? (char *) buffer.data() : NULL;
char *term_ptr = (char *) term.data();
unsigned long ret_len = max_len;
unsigned long tmout = m_dev.sec2usec(checkDefTimeout(timeout));
CHECK_CALL( espia_ser_read_str(m_dev, ptr, &ret_len, term_ptr,
term.length(), tmout) );
buffer.resize(ret_len);
}
void EspiaSerialLine::serialWrite( const string& buffer, int block_size,
double block_delay, bool no_wait )
void EspiaSerialLine::flush()
{
CHECK_CALL(espia_ser_flush(m_dev));
}
void EspiaSerialLine::serialFlush()
void EspiaSerialLine::getNumAvailBytes( int &avail_bytes )
{
unsigned long ret_bytes = 0;
CHECK_CALL(espia_ser_read(m_dev, NULL, &ret_bytes, 0));
avail_bytes = ret_bytes;
}
......@@ -8,23 +8,27 @@
namespace lima {
#define TMOUT_DEFAULT -2
#define TMOUT_BLOCK_FOREVER -1
#define TMOUT_NO_BLOCK 0
class HwSerialLine
{
public :
HwSerialLine( char line_term='\r', double timeout=1.0 );
enum TimeoutConst {
TMOUT_DEFAULT = -2,
TMOUT_BLOCK_FOREVER = -1,
TMOUT_NO_BLOCK = 0
};
HwSerialLine( const std::string& line_term="\r", double timeout=1.0,
int block_size=0, double block_delay=0 );
virtual ~HwSerialLine();
virtual void getAvail( int &avail ) = 0;
virtual void flush();
virtual void read( std::string& buffer, int& len, double timeout ) = 0;
virtual void read( std::string& buffer, int max_len,
double timeout=TMOUT_DEFAULT ) = 0;
virtual void write( const std::string& buffer, int block_size = 0,
double block_delay=0, bool no_wait=false ) = 0;
virtual void write( const std::string& buffer, bool no_wait=false ) = 0;
virtual void readStr( std::string& buffer, int max_len,
const std::string& term,
......@@ -33,28 +37,49 @@ class HwSerialLine
virtual void readLine( std::string& buffer, int max_len,
double timeout=TMOUT_DEFAULT );
virtual void writeRead( const std::string& writebuffer, int block_size,
double block_delay, bool no_wait,
std::string& readbuffer, /*int max_len, ???*/
double timeout=TMOUT_DEFAULT );
virtual void writeRead( const std::string& writebuffer,
std::string& readbuffer, int max_len,
bool wr_no_wait=false,
double rd_timeout=TMOUT_DEFAULT );
virtual void writeReadStr( const std::string& writebuffer,
int block_size, double block_delay,
bool no_wait, std::string& readbuffer,
std::string& readbuffer,
int max_len, const std::string& term,
double timeout=TMOUT_DEFAULT );
bool wr_no_wait=false,
double rd_timeout=TMOUT_DEFAULT );
virtual void readAvailable( std::string& buffer, /*int max_len, ???*/
double timeout=TMOUT_DEFAULT );
virtual void readAvailable( std::string& buffer, int max_len );
virtual void flush();
virtual void getNumAvailBytes( int &avail ) = 0;
virtual void setLineTerm( const std::string& line_term );
virtual void getLineTerm( std::string& line_term ) const;
virtual void setTimeout( double timeout );
virtual void getTimeout( double& timeout ) const;
virtual void setBlockSize( int block_size );
virtual void getBlockSize( int& block_size ) const;
virtual void setBlockDelay( double block_delay );
virtual void getBlockDelay( double& block_delay ) const;
double checkDefTimeout( double timeout );
private :
char m_line_term;
double m_timeout; /* Will be used with TMOUT_DEFAULT */
std::string m_line_term;
double m_timeout; /* Will be used with TMOUT_DEFAULT */
int m_block_size;
double m_block_delay;
};
inline double HwSerialLine::checkDefTimeout( double timeout )
{
return (TMOUT_DEFAULT == (int)timeout)? m_timeout : timeout; // XXX
}
} // namespace lima
#endif /* HWSERIALLINE_H */
......@@ -5,9 +5,12 @@ using namespace lima;
using namespace std;
HwSerialLine::HwSerialLine( char line_term, double timeout ) :
HwSerialLine::HwSerialLine( const string& line_term, double timeout,
int block_size, double block_delay ) :
m_line_term(line_term),
m_timeout(timeout)
m_timeout(timeout),
m_block_size(block_size),
m_block_delay(block_delay)
{
}
......@@ -26,7 +29,7 @@ void HwSerialLine::readStr( string& buffer, int max_len,
Timestamp start=Timestamp::now();
int match=0, n, term_len=term.length(), len=0;
bool have_timeout=(timeout > 0), have_maxlen=(max_len > 0);
double tout=((TMOUT_DEFAULT==timeout)? m_timeout: timeout);
double tout=checkDefTimeout(timeout);
string buf;
buffer = "";
......@@ -34,14 +37,15 @@ void HwSerialLine::readStr( string& buffer, int max_len,
((!have_maxlen) || (len < max_len)) ) {
n = 1;
read( buf, n, tout );
n = buf.length();
if( 0 == n )
break; // ???
return; // Or throw an exception?
buffer += buf;
len += n;
if( 0 == term.compare(match, n, buf) ) {
match += n;
if( match == term_len )
return;
return; // Terminator found
} else {
match = 0;
}
......@@ -56,50 +60,43 @@ void HwSerialLine::readStr( string& buffer, int max_len,
*/
void HwSerialLine::readLine( string& buffer, int max_len, double timeout )
{
readStr( buffer, max_len, string(1, m_line_term), timeout );
readStr( buffer, max_len, m_line_term, timeout );
}
/**
* @brief Write and then immediately Read the serial line until available?
* @brief Write and then immediately Read the serial line.
*/
void HwSerialLine::writeRead( const std::string& writebuffer, int block_size,
double block_delay, bool no_wait,
std::string& readbuffer, /*int max_len, ???*/
double timeout )
void HwSerialLine::writeRead( const string& writebuffer,
string& readbuffer, int max_len,
bool wr_no_wait, double rd_timeout )
{
write( writebuffer, block_size, block_delay, no_wait );
readAvailable( readbuffer, timeout );
// Or: read( readbuffer, max_len, timeout );
write( writebuffer, wr_no_wait );
read( readbuffer, max_len, rd_timeout );
}
/**
* @brief Write and then immediately Read the serial line until term.
*/
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 )
void HwSerialLine::writeReadStr( const string& writebuffer,
string& readbuffer, int max_len,
const string& term,
bool wr_no_wait, double rd_timeout )
{
write( writebuffer, block_size, block_delay, no_wait );
readStr( readbuffer, max_len, term, timeout );
write( writebuffer, wr_no_wait );
readStr( readbuffer, max_len, term, rd_timeout );
}
/**
* @brief Read the serial line until there is something to read
* @brief Read without blocking from the serial line.
*/
void HwSerialLine::readAvailable( std::string& buffer, /*int max_len, ???*/
double timeout )
void HwSerialLine::readAvailable( string& buffer, int max_len )
{
int max_len;
getAvail( max_len );
read( buffer, max_len, ((TMOUT_DEFAULT==timeout)? m_timeout: timeout) );
read( buffer, max_len, TMOUT_NO_BLOCK );
}
......@@ -109,11 +106,52 @@ void HwSerialLine::readAvailable( std::string& buffer, /*int max_len, ???*/
void HwSerialLine::flush()
{
string buf;
int n=1; // We don't want to waste memory reading all that is available
int len;
while( n ) {
n = 1;
read(buf, n, TMOUT_NO_BLOCK); // timeout ???
}
getNumAvailBytes(len);
read( buf, len, TMOUT_NO_BLOCK );
}
void HwSerialLine::setLineTerm( const string& line_term )
{
m_line_term = line_term;
}
void HwSerialLine::getLineTerm( string& line_term ) const
{
line_term = m_line_term;
}
void HwSerialLine::setTimeout( double timeout )
{
m_timeout = timeout;
}
void HwSerialLine::getTimeout( double& timeout ) const
{
timeout = m_timeout;
}
void HwSerialLine::setBlockSize( int block_size )
{
m_block_size = block_size;
}
void HwSerialLine::getBlockSize( int& block_size ) const
{
block_size = m_block_size;
}
void HwSerialLine::setBlockDelay( double block_delay )
{
m_block_delay = block_delay;
}
void HwSerialLine::getBlockDelay( double& block_delay ) const
{
block_delay = m_block_delay;
}
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