cancat.cpp
Go to the documentation of this file.
1 //======================================================================
30 //======================================================================
31 
32 #include <iostream>
33 #include <vector>
34 
35 
36 // Include the cSDH interface
37 #if WITH_ESD_CAN
38 # include "sdh/canserial-esd.h"
39 #endif
40 #if WITH_PEAK_CAN
41 # if ! SDH_USE_VCC
42 # include <unistd.h>
43 # endif
44 # include "sdh/canserial-peak.h"
45 #endif
46 #if ! WITH_ESD_CAN && ! WITH_PEAK_CAN
47 # error "At least support for ESD-CAN or PEAK-CAN must be enabled to compile cancat"
48 #endif
49 #include "sdh/sdh.h"
50 #include "sdh/dbg.h"
51 #include "sdh/util.h"
53 #include "sdhoptions.h"
54 
55 using namespace std;
57 
64 char const* __help__ = "Send data from command line via ESD or PEAK CAN and display replies until CTRL-C is pressed.";
66 char const* __author__ = "Dirk Osswald: dirk.osswald@de.schunk.com";
67 char const* __url__ = "http://www.schunk.com";
68 char const* __version__ = "$Id: cancat.cpp 9739 2013-02-04 15:39:29Z Osswald2 $";
69 char const* __copyright__ = "Copyright (c) 2007 SCHUNK GmbH & Co. KG";
70 
71 // end of doxygen name group sdhlibrary_cpp_cancat_cpp_vars
72 // @}
73 
74 int main( int argc, char** argv )
75 {
76  //---------------------
77  // handle command line options: set defaults first then overwrite by parsing actual command line
78  cSDHOptions options( "general sdhcom_common sdhcom_esdcan sdhcom_peakcan sdhcom_cancommon" );
79 
80  options.timeout = 0.0;
81 
82  options.Parse( argc, argv, __help__, "cancat", __version__, cSDH::GetLibraryName(), cSDH::GetLibraryRelease() );
83 
84  //
85  //---------------------
86 
87  cDBG dbg( options.debug_level > 0, "red", options.debuglog );
88  g_sdh_debug_log = options.debuglog;
89 
90  try
91  {
92  cSerialBase* canserial;
93 
94 #if WITH_ESD_CAN
95  if ( options.use_can_esd )
96  canserial = new cCANSerial_ESD( options.net, options.can_baudrate, options.timeout, options.id_read, options.id_write );
97 #endif
98 #if WITH_PEAK_CAN
99  if ( options.use_can_peak )
100  canserial = new cCANSerial_PEAK( options.can_baudrate, options.timeout, options.id_read, options.id_write, options.sdh_canpeak_device );
101 #endif
102  canserial->dbg.SetFlag( options.debug_level > 2 );
103 
104  canserial->Open();
105 
106  if ( optind < argc)
107  {
108  char buffer[ 512 ];
109  buffer[0] = '\0';
110  char const* sep = "";
111  while (optind < argc)
112  {
113  strncat( buffer, argv[ optind++ ], 511 );
114  strncat( buffer, sep, 511 );
115  sep = " ";
116  }
117  strncat( buffer, "\r\n", 511 );
118 
119  dbg << "Sending String \"" << buffer << "\" via CAN\n";
120 
121 
122  canserial->write( buffer, strlen( buffer ) );
123  }
124  while ( 1 )
125  {
126  char reply;
127  if ( 1 == canserial->Read( &reply, 1, long(options.timeout*1E6), false ) )
128  printf( "%c", reply );
129  else
130  dbg << "nutin\n";
131  fflush( stdout );
132  }
133  }
134  catch ( cSDHLibraryException* e )
135  {
136  cerr << "cancat main(): Caught exception from SDHLibrary: " << e->what() << ". Giving up!\n";
137  delete e;
138  }
139  catch (...)
140  {
141  cerr << "cancat main(): caught unexpected exception!\n";
142  }
143 }
144 //----------------------------------------------------------------------
145 
146 
147 //======================================================================
148 /*
149  Here are some settings for the emacs/xemacs editor (and can be safely ignored)
150  (e.g. to explicitely set C++ mode for *.h header files)
151 
152  Local Variables:
153  mode:C++
154  mode:ELSE
155  End:
156 */
157 //======================================================================]
USING_NAMESPACE_SDH
#define USING_NAMESPACE_SDH
Definition: sdhlibrary_settings.h:81
cSDHLibraryException
Base class for exceptions in the SDHLibrary-CPP.
Definition: sdhexception.h:132
__version__
char const * __version__
Definition: cancat.cpp:68
cSerialBase
Low-level communication class to access a serial port.
Definition: serialbase.h:105
cSDHOptions::id_write
unsigned int id_write
Definition: sdhoptions.h:101
__help__
char const * __help__
Definition: cancat.cpp:65
cSDHOptions::can_baudrate
unsigned long can_baudrate
Definition: sdhoptions.h:99
cSDHOptions::use_can_peak
bool use_can_peak
Definition: sdhoptions.h:96
sdhoptions.h
Implementation of a class to parse common SDH related command line options.
__author__
char const * __author__
Definition: cancat.cpp:66
main
int main(int argc, char **argv)
Definition: cancat.cpp:74
cCANSerial_PEAK
Low-level communication class to access a CAN port from company PEAK (http://www.peak-system....
Definition: canserial-peak.h:78
dbg.h
This file contains interface and implementation of class #SDH::cDBG, a class for colorfull debug mess...
cSDHOptions::debuglog
std::ostream * debuglog
Definition: sdhoptions.h:86
cSDHOptions::debug_level
int debug_level
Definition: sdhoptions.h:85
cSDH::GetLibraryRelease
static char const * GetLibraryRelease(void)
Definition: sdh.cpp:626
__copyright__
char const * __copyright__
Definition: cancat.cpp:69
cSDHOptions::id_read
unsigned int id_read
Definition: sdhoptions.h:100
cSDHOptions::net
int net
Definition: sdhoptions.h:94
cSDHOptions::use_can_esd
bool use_can_esd
Definition: sdhoptions.h:93
cSDHOptions
class for command line option parsing holding option parsing results
Definition: sdhoptions.h:78
__url__
char const * __url__
Definition: cancat.cpp:67
canserial-esd.h
Interface of class #SDH::cCANSerial_ESD, class to access CAN bus via ESD card on cygwin/linux.
cDBG
A class to print colored debug messages.
Definition: dbg.h:113
cSerialBase::Open
virtual void Open(void)=0
Open rs232 port port.
optind
int optind
Definition: getopt.c:131
cSerialBase::Read
virtual ssize_t Read(void *data, ssize_t size, long timeout_us, bool return_on_less_data)=0
cDBG::SetFlag
void SetFlag(bool flag)
Definition: dbg.h:149
cSerialBase::write
virtual int write(char const *ptr, int len=0)=0
Write data to a previously opened port.
sdhlibrary_settings.h
This file contains settings to make the SDHLibrary compile on differen systems:
cSDHOptions::timeout
double timeout
Definition: sdhoptions.h:90
cSDH::GetLibraryName
static char const * GetLibraryName(void)
Definition: sdh.cpp:633
g_sdh_debug_log
USING_NAMESPACE_SDH NAMESPACE_SDH_START std::ostream * g_sdh_debug_log
Definition: sdhbase.cpp:55
sdh.h
This file contains the interface to class #SDH::cSDH, the end user class to access the SDH from a PC.
cCANSerial_ESD
Low-level communication class to access a CAN port from company ESD (http://www.esd....
Definition: canserial-esd.h:97
cSDHLibraryException::what
virtual const char * what() const
Definition: sdhexception.cpp:118
cSerialBase::dbg
cDBG dbg
A stream object to print colored debug messages.
Definition: serialbase.h:227
cSDHOptions::Parse
int Parse(int argc, char **argv, char const *helptext, char const *progname, char const *version, char const *libname, char const *librelease)
Definition: sdhoptions.cpp:464
util.h
Interface of auxilliary utility functions for SDHLibrary-CPP.
cSDHOptions::sdh_canpeak_device
char sdh_canpeak_device[MAX_DEV_LENGTH]
Definition: sdhoptions.h:97
canserial-peak.h
Interface of class #SDH::cCANSerial_PEAK, class to access CAN bus via PEAK card on cygwin/linux.


sdhlibrary_cpp
Author(s): Dirk Osswald
autogenerated on Wed Mar 2 2022 01:00:58