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 //======================================================================]
unsigned long can_baudrate
Definition: sdhoptions.h:99
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
This file contains the interface to class #SDH::cSDH, the end user class to access the SDH from a PC...
int optind
Definition: getopt.c:131
char sdh_canpeak_device[MAX_DEV_LENGTH]
Definition: sdhoptions.h:97
A class to print colored debug messages.
Definition: dbg.h:113
char const * __copyright__
Definition: cancat.cpp:69
Interface of class #SDH::cCANSerial_PEAK, class to access CAN bus via PEAK card on cygwin/linux...
bool use_can_peak
Definition: sdhoptions.h:96
unsigned int id_write
Definition: sdhoptions.h:101
char const * __help__
Definition: cancat.cpp:65
void SetFlag(bool flag)
Definition: dbg.h:149
unsigned int id_read
Definition: sdhoptions.h:100
std::ostream * debuglog
Definition: sdhoptions.h:86
Interface of class #SDH::cCANSerial_ESD, class to access CAN bus via ESD card on cygwin/linux.
int debug_level
Definition: sdhoptions.h:85
Implementation of a class to parse common SDH related command line options.
static char const * GetLibraryRelease(void)
Definition: sdh.cpp:626
This file contains interface and implementation of class #SDH::cDBG, a class for colorfull debug mess...
Low-level communication class to access a serial port.
Definition: serialbase.h:105
char const * __version__
Definition: cancat.cpp:68
Base class for exceptions in the SDHLibrary-CPP.
Definition: sdhexception.h:132
Interface of auxilliary utility functions for SDHLibrary-CPP.
virtual const char * what() const
bool use_can_esd
Definition: sdhoptions.h:93
#define USING_NAMESPACE_SDH
virtual int write(char const *ptr, int len=0)=0
Write data to a previously opened port.
char const * __author__
Definition: cancat.cpp:66
This file contains settings to make the SDHLibrary compile on differen systems:
Low-level communication class to access a CAN port from company PEAK (http://www.peak-system.com)
int main(int argc, char **argv)
Definition: cancat.cpp:74
virtual void Open(void)=0
Open rs232 port port.
static char const * GetLibraryName(void)
Definition: sdh.cpp:633
class for command line option parsing holding option parsing results
Definition: sdhoptions.h:78
USING_NAMESPACE_SDH NAMESPACE_SDH_START std::ostream * g_sdh_debug_log
Definition: sdhbase.cpp:55
virtual ssize_t Read(void *data, ssize_t size, long timeout_us, bool return_on_less_data)=0
char const * __url__
Definition: cancat.cpp:67
Low-level communication class to access a CAN port from company ESD (http://www.esd.eu/)
Definition: canserial-esd.h:97
cDBG dbg
A stream object to print colored debug messages.
Definition: serialbase.h:227
double timeout
Definition: sdhoptions.h:90


sdhlibrary_cpp
Author(s): Dirk Osswald
autogenerated on Sun Aug 18 2019 03:42:20