Commit 5b7b5573 authored by thomas.forbriger's avatar thomas.forbriger Committed by thomas.forbriger
Browse files

first simple success

This is a legacy commit from before 2015-03-01.
It may be incomplete as well as inconsistent.
See COPYING.legacy and README.history for details.


SVN Path:     http://gpitrsvn.gpi.uni-karlsruhe.de/repos/TFSoftware/trunk
SVN Revision: 2661
SVN UUID:     67feda4a-a26e-11df-9d6e-31afc202ad0c
parent 1e1a66f7
# this is <Makefile>
# ----------------------------------------------------------------------------
# $Id: Makefile,v 1.1 2008-11-24 14:02:20 tforb Exp $
# $Id: Makefile,v 1.2 2008-11-24 15:38:20 tforb Exp $
#
# Copyright (c) 2008 by Thomas Forbriger (BFO Schiltach)
#
......@@ -32,8 +32,7 @@ HEADERS=$(wildcard *.h)
LIBSRC=$(wildcard *.cc)
#TESTSRC=tests/hpmotest.cc tests/pdastest.cc tests/mseedtest.cc \
# tests/sfftest.cc tests/sactest.cc
TESTSRC=$(wildcard tests/*.cc)
INCINSTALLPATH=$(LOCINCLUDEDIR)/serialxx
LIBINSTALLPATH=$(LOCLIBDIR)
......@@ -66,7 +65,7 @@ CPPFLAGS=-I$(LOCINCLUDEDIR) $(FLAGS)
all: install doxybrief doxyfull
install: install-include libserialxx.a
flist: Makefile $(wildcard *.cfg) $(HEADERS) $(LIBSRC) README
flist: Makefile $(wildcard *.cfg) $(HEADERS) $(LIBSRC) README $(TESTSRC)
echo $^ | tr ' ' '\n' | sort > $@
.PHONY: edit
......@@ -154,6 +153,12 @@ doxyview: doxyfullview
# test code
# ---------
portread portwrite: %: tests/%.o libserialxx.a
$(CXX) $(FLAGS) -o $@ $< -I$(LOCINCLUDEDIR) \
-lserialxx -ltfxx \
-L$(LOCLIBDIR)
/bin/mv -fv $@ $(LOCBINDIR)
#======================================================================
# create package
# --------------
......
......@@ -3,7 +3,7 @@
*
* ----------------------------------------------------------------------------
*
* $Id: README,v 1.1 2008-11-24 14:02:21 tforb Exp $
* $Id: README,v 1.2 2008-11-24 15:38:20 tforb Exp $
*
* Copyright (c) 2002 by Thomas Forbriger (IMG Frankfurt)
*
......@@ -34,11 +34,10 @@ namespace serialport {
\since November 2008
\date November 2008
\version V1.0
$Id: README,v 1.1 2008-11-24 14:02:21 tforb Exp $
$Id: README,v 1.2 2008-11-24 15:38:20 tforb Exp $
Contents of this page:
- \ref sec_intention
- \ref sec_howto
\section sec_intention Intention
......
......@@ -3,7 +3,7 @@
*
* ----------------------------------------------------------------------------
*
* $Id: error.cc,v 1.1 2008-11-24 14:02:21 tforb Exp $
* $Id: error.cc,v 1.2 2008-11-24 15:38:20 tforb Exp $
* \author Thomas Forbriger
* \date 24/11/2008
*
......@@ -36,12 +36,106 @@
#define TF_ERROR_CC_VERSION \
"TF_ERROR_CC V1.0 "
#define TF_ERROR_CC_CVSID \
"$Id: error.cc,v 1.1 2008-11-24 14:02:21 tforb Exp $"
"$Id: error.cc,v 1.2 2008-11-24 15:38:20 tforb Exp $"
#include <iostream>
#include <serialxx/error.h>
using std::cerr;
using std::endl;
namespace serialport {
//! initialize and instantiate
bool Exception::Mreport_on_construct=true;
//! construct from nothing
Exception::Exception():
Mmessage(0), Mfile(0), Mline(0), Mcondition(0)
{ if (Mreport_on_construct) { report(); } }
//! construct with message
Exception::Exception(const char* message):
Mmessage(message), Mfile(0), Mline(0), Mcondition(0)
{ if (Mreport_on_construct) { report(); } }
//! construct with message and file info
Exception::Exception(const char* message,
const char* condition):
Mmessage(message), Mfile(0), Mline(0), Mcondition(condition)
{ if (Mreport_on_construct) { report(); } }
//! construct with message and file info
Exception::Exception(const char* message,
const char* file,
const int& line):
Mmessage(message), Mfile(file), Mline(line), Mcondition(0)
{ if (Mreport_on_construct) { report(); } }
//! construct with message and file info and condition
Exception::Exception(const char* message,
const char* file,
const int& line,
const char* condition):
Mmessage(message), Mfile(file), Mline(line), Mcondition(condition)
{ if (Mreport_on_construct) { report(); } }
//! switch on
void Exception::report_on_construct()
{
Mreport_on_construct=true;
}
//! switch off
void Exception::dont_report_on_construct()
{
Mreport_on_construct=false;
}
//! report
void Exception::report() const
{
base_report();
}
//! report
void Exception::base_report() const
{
cerr << "Exception report:" << endl;
if (Mmessage==0)
{
cerr << " No message" << endl;
}
else
{
cerr << " message: " << Mmessage << endl;
}
if (Mfile!=0)
{
cerr << " triggered in \"" << Mfile << "\" at line #" << Mline << endl;
}
if (Mcondition!=0)
{
cerr << " by condition:" << endl
<< " \"" << Mcondition << "\"" << endl;
}
}
/*----------------------------------------------------------------------*/
//! report violation of assertion
void report_violation(const char* message,
const char* file,
const int& line,
const char* condition)
{
std::cerr << std::endl;
std::cerr << "VIOLATION of condition: " << condition << std::endl;
std::cerr << "* in " << file << " at line " << line << std::endl;
std::cerr << "* message: " << message << std::endl;
}
} // namespace serialport
/* ----- END OF error.cc ----- */
......@@ -3,7 +3,7 @@
*
* ----------------------------------------------------------------------------
*
* $Id: error.h,v 1.1 2008-11-24 14:02:21 tforb Exp $
* $Id: error.h,v 1.2 2008-11-24 15:38:20 tforb Exp $
* \author Thomas Forbriger
* \date 24/11/2008
*
......@@ -40,12 +40,150 @@
#define TF_ERROR_H_VERSION \
"TF_ERROR_H V1.0 "
#define TF_ERROR_H_CVSID \
"$Id: error.h,v 1.1 2008-11-24 14:02:21 tforb Exp $"
"$Id: error.h,v 1.2 2008-11-24 15:38:20 tforb Exp $"
namespace serialport {
/*! \defgroup group_error Error handling module
*/
/*! \brief Base class for exceptions
*
* This is an exception base class. It holds some information about the
* reason for throwing the exception. The information is printed to cerr
* through function report(). This function may be overloaded by a derived
* type. But its functionality is still accessible through base_report().
*
* The standard behaviour is to print out the message during object
* initialization. If you don't like this, call dont_report_on_construct().
*
* \ingroup group_error
* \sa SERIALPORT_Xassert
* \sa SERIALPORT_assert
* \sa SERIALPORT_abort
*/
class Exception
{
public:
//! Creates exception with no explaning comments
Exception();
//! Creates an exception with an explanation message
Exception(const char* message);
//! Creates an exception with message and failed assertion
Exception(const char* message,
const char* condition);
//! Create with message, failed assertion, and code position
Exception(const char* message,
const char* file,
const int& line,
const char* condition);
//! Create with message and code position
Exception(const char* message,
const char* file,
const int& line);
//! provide explicit virtual destructor
virtual ~Exception() { }
//! Screen report
virtual void report() const;
//! Issue a screen report on construction of exception
static void report_on_construct();
//! Issue NO screen report on construction of exception
static void dont_report_on_construct();
protected:
//! Screen report
void base_report() const;
private:
//! Shall we print to cerr at construction time?
static bool Mreport_on_construct;
//! pointer to message string
const char* Mmessage;
//! pointer to file name string
const char* Mfile;
//! pointer to line number in source file
const int& Mline;
//! pointer to assertion condition text string
const char* Mcondition;
}; // class Exception
/*! \brief report violation of condition
*
* \ingroup group_error
* \param message message of type char*
* \param file name of source code file
* \param line source code line number
* \param condition assert condition
*/
void report_violation(const char* message,
const char* file,
const int& line,
const char* condition);
} // namespace serialport
/*======================================================================*/
//
// preprocessor macros
// ===================
/*! \brief Check an assertion and report by throwing an exception.
*
* \ingroup group_error
* \param C assert condition
* \param M message of type char*
* \param E exception class to throw
*/
#define SERIALPORT_Xassert(C,M,E) \
if (!(C)) { throw( E ( M , __FILE__, __LINE__, #C )); }
/*! \brief Check an assertion and report by throwing an exception.
*
* \ingroup group_error
* \param C assert condition
* \param M message of type char*
*/
#define SERIALPORT_assert(C,M) \
SERIALPORT_Xassert( C , M , serialport::Exception )
/*! \brief Abort and give a message.
*
* \ingroup group_error
* \param M message of type char*
* \param E exception class to throw
*/
#define SERIALPORT_abort(M) \
throw( serialport::Exception ( M , __FILE__, __LINE__ ))
#define SERIALPORT_illegal SERIALPORT_abort("illegal call!")
/*! \brief Check an assertion and report only.
*
* \ingroup group_error
* \param C assert condition
* \param M message of type char*
* \param V any values that should be output (comment)
* a sequence of values and output operators
*/
#define SERIALPORT_report_assert(C,M,V) \
if (!(C)) { \
serialport::report_violation(M, __FILE__, __LINE__, #C); \
std::cerr << "* comment: " << V << std::endl; \
std::cerr << std::endl; \
std::cerr.flush(); \
}
/*! \brief Macro to distinguish between fatal and non fatal assertions.
*
* \ingroup group_error
* \param F true for non fatal behaviour
* \param C assert condition
* \param M message of type char*
* \param V any values that should be output (comment)
* a sequence of values and output operators
*/
#define SERIALPORT_nonfatal_assert(F,C,M,V) \
if (F) { SERIALPORT_report_assert(C,M,V) } else { SERIALPORT_assert(C,M) }
#endif // TF_ERROR_H_VERSION (includeguard)
/* ----- END OF error.h ----- */
......@@ -3,7 +3,7 @@
*
* ----------------------------------------------------------------------------
*
* $Id: serialport.cc,v 1.1 2008-11-24 14:02:22 tforb Exp $
* $Id: serialport.cc,v 1.2 2008-11-24 15:38:21 tforb Exp $
* \author Thomas Forbriger
* \date 24/11/2008
*
......@@ -36,12 +36,65 @@
#define TF_SERIALPORT_CC_VERSION \
"TF_SERIALPORT_CC V1.0 "
#define TF_SERIALPORT_CC_CVSID \
"$Id: serialport.cc,v 1.1 2008-11-24 14:02:22 tforb Exp $"
"$Id: serialport.cc,v 1.2 2008-11-24 15:38:21 tforb Exp $"
#include <serialxx/serialport.h>
#include <serialxx/error.h>
#include <unistd.h>
#include <iostream>
namespace serialport {
//! open serial port
SerialPort::SerialPort(const std::string& devname,
const int& flags)
{
Mfd=open(devname.c_str(), flags);
SERIALPORT_assert(Mfd>0, "ERROR on opening port");
Mdebug=false;
}
/*----------------------------------------------------------------------*/
//! close serial port
SerialPort::~SerialPort()
{
SERIALPORT_assert(close(Mfd)!=-1, "ERROR on closing port");
}
/*----------------------------------------------------------------------*/
//! write to port
void SerialPort::write(const std::string& text) const
{
int n,len=text.size();
n=::write(Mfd, text.c_str(), len);
SERIALPORT_assert(n==len, "ERROR on writing to port");
}
/*----------------------------------------------------------------------*/
//! read from port
std::string SerialPort::read(const std::string& delim) const
{
std::string retval;
const int nbuf=255;
char buffer[nbuf];
bool hot=true;
while (hot)
{
int nbytes=::read(Mfd, buffer, nbuf);
if (Mdebug)
{
std::cout << "SerialPort::read: " << std::endl
<< "read " << nbytes << " characters" << std::endl;
}
if (nbytes>0) { retval.append(buffer, nbytes); }
if (retval.find(delim)!=std::string::npos) { hot=false; }
}
return(retval);
}
} // namespace serialport
/* ----- END OF serialport.cc ----- */
......@@ -3,7 +3,7 @@
*
* ----------------------------------------------------------------------------
*
* $Id: serialport.h,v 1.1 2008-11-24 14:02:22 tforb Exp $
* $Id: serialport.h,v 1.2 2008-11-24 15:38:21 tforb Exp $
* \author Thomas Forbriger
* \date 24/11/2008
*
......@@ -37,13 +37,38 @@
// include guard
#ifndef TF_SERIALPORT_H_VERSION
#include <fcntl.h>
#include <string>
#define TF_SERIALPORT_H_VERSION \
"TF_SERIALPORT_H V1.0 "
#define TF_SERIALPORT_H_CVSID \
"$Id: serialport.h,v 1.1 2008-11-24 14:02:22 tforb Exp $"
"$Id: serialport.h,v 1.2 2008-11-24 15:38:21 tforb Exp $"
namespace serialport {
/*! This class provides access to a serial port
*/
class SerialPort {
public:
//! open port by name of device file
SerialPort(const std::string& devname,
const int& flags=(O_RDWR | O_NOCTTY | O_NDELAY));
//! close port
~SerialPort();
//! write string to port
void write(const std::string& text) const;
//! read from port until delimiter
std::string read(const std::string& delim="\n") const;
//! set debug mode
void debug(const bool& flag) { Mdebug=flag; }
private:
//! file descriptor to this port
int Mfd;
//! debug flag
bool Mdebug;
}; // class SerialPort
} // namespace serialport
#endif // TF_SERIALPORT_H_VERSION (includeguard)
......
This is a legacy version of the repository. It may be incomplete as well as
inconsistent. See README.history for details. For the old stock of the
repository copyright and licence conditions apply as specified for versions
commited after 2015-03-01. Use recent versions as a base for new development.
The legacy version is only stored to keep a record of history.
/*! \file portread.cc
* \brief read from port
*
* ----------------------------------------------------------------------------
*
* $Id: portread.cc,v 1.1 2008-11-24 15:38:21 tforb Exp $
* \author Thomas Forbriger
* \date 24/11/2008
*
* read from port
*
* Copyright (c) 2008 by Thomas Forbriger (BFO Schiltach)
*
* ----
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
* ----
*
*
* REVISIONS and CHANGES
* - 24/11/2008 V1.0 Thomas Forbriger
*
* ============================================================================
*/
#define PORTREAD_VERSION \
"PORTREAD V1.0 read from port"
#define PORTREAD_CVSID \
"$Id: portread.cc,v 1.1 2008-11-24 15:38:21 tforb Exp $"
#include <iostream>
#include <string>
#include <tfxx/commandline.h>
#include <serialxx/serialport.h>
using std::cout;
using std::cerr;
using std::endl;
struct Options {
bool verbose;
std::string device, delimiter;
};
int main(int iargc, char* argv[])
{
// define usage information
char usage_text[]=
{
PORTREAD_VERSION "\n"
"usage: portread [-p device] [-v] [-d delimiter]" "\n"
" or: portread --help|-h" "\n"
};
// define full help text
char help_text[]=
{
PORTREAD_CVSID
};
// define commandline options
using namespace tfxx::cmdline;
static Declare options[]=
{
// 0: print help
{"help",arg_no,"-"},
// 1: verbose mode
{"v",arg_no,"-"},
// 2: device name
{"p",arg_yes,"/dev/ttyS0"},
// 3: delimiter name
{"d",arg_yes,"\r"},
{NULL}
};
// no arguments? print usage...
if (iargc<1)
{
cerr << usage_text << endl;
exit(0);
}
// collect options from commandline
Commandline cmdline(iargc, argv, options);
// help requested? print full help text...
if (cmdline.optset(0))
{
cerr << usage_text << endl;
cerr << help_text << endl;
exit(0);
}
/*
// dummy operation: print option settings
for (int iopt=0; iopt<2; iopt++)
{
cout << "option: '" << options[iopt].opt_string << "'" << endl;
if (cmdline.optset(iopt)) { cout << " option was set"; }
else { cout << "option was not set"; }
cout << endl;
cout << " argument (string): '" << cmdline.string_arg(iopt) << "'" << endl;
cout << " argument (int): '" << cmdline.int_arg(iopt) << "'" << endl;
cout << " argument (long): '" << cmdline.long_arg(iopt) << "'" << endl;
cout << " argument (float): '" << cmdline.float_arg(iopt) << "'" << endl;
cout << " argument (double): '" << cmdline.double_arg(iopt) << "'" << endl;
cout << " argument (bool): '";
if (cmdline.bool_arg(iopt))
{ cout << "true"; } else { cout << "false"; }
cout << "'" << endl;
}
while (cmdline.extra()) { cout << cmdline.next() << endl; }
// dummy operation: print rest of command line
while (cmdline.extra()) { cout << cmdline.next() << endl; }
*/
Options opt;
opt.verbose=cmdline.optset(1);
opt.device=cmdline.string_arg(2);
opt.delimiter=cmdline.string_arg(3);
serialport::SerialPort port(opt.device);
// port.debug(true);
std::string text=port.read(opt.delimiter);
cout << text << endl;
}
/* ----- END OF portread.cc ----- */
/*! \file portwrite.cc
* \brief write to port
*
* ----------------------------------------------------------------------------
*
* $Id: portwrite.cc,v 1.1 2008-11-24 15:38:21 tforb Exp $
* \author Thomas Forbriger
* \date 24/11/2008
*
* write to port
*
* Copyright (c) 2008 by Thomas Forbriger (BFO Schiltach)
*
* ----
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or