main.cpp

Go to the documentation of this file.
00001 //======================================================================
00002 // File:        main.cpp
00003 // Author:      Matthias Toussaint
00004 // Created:     Sat Nov 25 15:37:12 CET 2006
00005 // Project:     QtDMM
00006 // Description: Console based multimeter readout program
00007 //----------------------------------------------------------------------
00008 // This file  may  be used under  the terms of  the GNU  General Public
00009 // License  version 2.0 as published   by the Free Software  Foundation
00010 // and appearing  in the file LICENSE.GPL included  in the packaging of
00011 // this file.
00012 // 
00013 // This file is provided AS IS with  NO WARRANTY OF ANY KIND, INCLUDING 
00014 // THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
00015 // PURPOSE.
00016 //----------------------------------------------------------------------
00017 // Copyright 2006 Matthias Toussaint
00018 //======================================================================
00019 
00020 #include <dmmclass.h>
00021 #include <configuration.h>
00022 #include <iostream>
00023 
00024 Configuration *create_config();
00025 
00050 int main( int argc, char **argv )
00051 {
00052   Configuration *config = create_config();
00053   
00054   if (!config->setCommandLine( argc, argv ))
00055   {
00056     std::cout << "Usage: cdmm [options] | --help" << std::endl;
00057     exit(-1);
00058   }
00059   
00060   // help
00061   //
00062   if (config->hasKey( "help" ))
00063   {
00064     std::cout << config->helpText();
00065     exit(-1);
00066   }
00067   
00068   // check for print flags
00069   //
00070   if (config->hasKey( "device-table" ))
00071   {
00072     std::cerr << DMMClass::device_table() << std::endl;
00073     exit(0);
00074   }
00075   // check for print flags
00076   //
00077   if (config->hasKey( "html-device-table" ))
00078   {
00079     std::cerr << DMMClass::html_device_table() << std::endl;
00080     exit(0);
00081   }
00082   
00083   bool showList=false;
00084   
00085   if (config->hasKey( "list-protocols" ))
00086   {
00087     showList = true;
00088     std::cout << "Known protocols:" << std::endl;
00089     std::cout << DMMClass::print_protocols();
00090   }
00091   
00092   if (config->hasKey( "list-devices" ))
00093   {
00094     showList = true;
00095     std::cout << "Known devices:" << std::endl;
00096     std::cout << DMMClass::print_devices();
00097   }
00098   
00099   if (showList) exit(0);
00100   
00101   // configuration file ?
00102   //
00103   if (config->hasKey( "file" ))
00104   {
00105     if (!config->loadFile( config->value( "file" )))
00106     {
00107       exit(-1);
00108     }
00109     // commandline always overrides
00110     config->setCommandLine( argc, argv );
00111   }
00112   
00113   config->printEntries();
00114   
00115 #if 0
00116   if (4 != argc)
00117   {
00118     std::cout << "Usage: cdmm port protocol num_values" << std::endl;
00119     std::cout << "port    : devicename:baudrate:bits:n|e|o:stopbits(1|2)[:RTS][:DTR][:DSR][:CTS]" 
00120         << std::endl;
00121     std::cout << "The optional RTS, DTR, DSR, CTS parameter can be used to "
00122         << "force" << std::endl;
00123     std::cout << "these signals to be set. Usually you'll need :DTR:DSR:CTS" << std::endl << std::endl;
00124     std::cout << "          Example: /dev/ttyS0:600:8:n:1:RTS" 
00125         << std::endl << std::endl;
00126     std::cout << "protocol: M14C|M15C|M14P|VC820|M9803R|IOCB|PT10A|ESC97|GDM703" 
00127         << std::endl << std::endl;
00128     std::cout << "          M14C:   Metex 14 bytes ASCII continuous" 
00129               << std::endl;
00130     std::cout << "          M15C:   Metex 15 bytes ASCII continuous" 
00131               << std::endl;
00132     std::cout << "          M14P:   Metex 14 bytes ASCII polling" 
00133               << std::endl;
00134     std::cout << "          VC820:  Voltcraft VC820 14 bytes binary continuous"
00135               << std::endl;
00136     std::cout << "          M9803R: Metex M9803R 11 bytes binary continuous"
00137               << std::endl;
00138     std::cout << "          IOCB:   UV Monitor (Radek Liboska)"
00139               << std::endl;
00140     std::cout << "          PT10A:  PeakTech 10 bytes ASCII continuous"
00141         << std::endl;
00142     std::cout << "          ESC97:  Escort 97 21 bytes binary continuous"
00143         << std::endl;
00144     std::cout << "          GDM703: Voltcraft GDM703 26 bytes ASCII continuous"
00145         << std::endl << std::endl;
00146     
00147     std::cerr << "Example:" << std::endl;
00148     std::cerr << "ME-32 at ttyUSB0: cdmm /dev/ttyUSB0:600:7:n:2:DTR:DSR:CTS M14P 1"
00149         << std::endl << std::endl;
00150     exit (0);
00151   }
00152 #endif
00153   std::string protocol;
00154   std::string port;
00155   
00156   if (!config->hasKey( "port" ))
00157   {
00158     std::cerr << "Port missing" << std::endl;
00159     exit(-1);
00160   }
00161   else port = config->value( "port" );
00162   
00163   if (!config->hasKey( "device" ))
00164   {
00165     if (!config->hasKey( "protocol" ))
00166     {
00167       std::cerr << "Protocol missing" << std::endl;
00168       exit(-1);
00169     }
00170     else protocol = config->value( "protocol" );
00171   }
00172   else
00173   {
00174     if (DMMClass::has_device( config->value( "device" )))
00175     {
00176       if (config->hasKey( "protocol" ))
00177       {
00178         std::cerr << "Ignoring --protocol. Device already specified"
00179             << std::endl;
00180       }
00181       
00182       protocol = DMMClass::get_device( config->value( "device" )).protocol;
00183       port += ":";
00184       port += DMMClass::get_device( config->value( "device" )).port;
00185     }
00186     else 
00187     {
00188       std::cerr << "Unknown device: " << config->value( "device" )
00189           << std::endl;
00190       exit( -1 );
00191     }
00192   }
00193   
00194   DMMClass *dmm = DMMClass::create_protocol( protocol );
00195   
00196   if (0 == dmm)
00197   {
00198     std::cout << "Unknow protocol: " << protocol 
00199         << std::endl;
00200     exit(-1);
00201   }
00202   
00203   if (config->hasKey( "console-logging" ))
00204   {
00205     dmm->setConsoleLogging( true );
00206   }
00207     
00208   if (config->hasKey( "num-values" ))
00209   {
00210     if (config->hasKey( "device" ))
00211     {
00212       std::cerr << "Ignoring --num-values. Device already specified"
00213           << std::endl;
00214       dmm->setNumValues( DMMClass::get_device( 
00215           config->value( "device" )).numValues );
00216     }
00217     else
00218     {
00219       dmm->setNumValues( config->intValue( "num-values" ) );
00220     }
00221   }
00222   
00223   if (config->hasKey( "device" ))
00224   {
00225     dmm->setNumValues( DMMClass::get_device( 
00226         config->value( "device" )).numValues );
00227   }
00228   
00229   if (Port::Ok == dmm->open( port ))
00230   {
00231     dmm->start();
00232     
00233     while (1)
00234     {
00235       if (dmm->timeout())
00236       {
00237         std::cerr << "Timeout!" << std::endl;
00238         dmm->close();
00239         exit(-1);
00240       }
00241       
00242       if (dmm->hasValue())
00243       {
00244         if (!dmm->overflow())
00245         {
00246           for (int i=0; i<dmm->numValues(); ++i)
00247           {
00248             std::cout << "V" << i << ": " << dmm->value(i) << " " 
00249                 << dmm->unit(i) << " " << dmm->mode(i) << std::endl;
00250           }
00251         }
00252         else std::cerr << "Overflow" << std::endl;
00253       }
00254       usleep( 500000 );
00255       //sleep( 1 );
00256     }
00257     
00258     dmm->stop();
00259   }
00260   else std::cerr << "Open failed!" << std::endl;
00261 }
00262 
00263 Configuration *create_config()
00264 {
00265   Configuration *config = new Configuration;
00266   config->addOption( Configuration::Option( "f", "file", true, 
00267                      "Paremeter file" ) );
00268   config->addOption( Configuration::Option( "p", "port", true, 
00269                      "Port description" ) );
00270   config->addOption( Configuration::Option( "P", "protocol", true, 
00271                      "Protocol name" ) );
00272   config->addOption( Configuration::Option( "h", "help", false, 
00273                      "Show helptext" ) );
00274   config->addOption( Configuration::Option( "v", "version", false, 
00275                      "Show version" ) );
00276   config->addOption( Configuration::Option( "l", "list-protocols", false, 
00277                      "Show list of implemented protocols" ) );
00278   config->addOption( Configuration::Option( "L", "list-devices", false, 
00279                      "Show list of known devices" ) );
00280   config->addOption( Configuration::Option( "d", "device", true, 
00281                      "Name of the device (See --list-devices)" ) );
00282   config->addOption( Configuration::Option( "n", "num-values", true, 
00283                      "Number of values this device provides" ) );
00284   config->addOption( Configuration::Option( "", "console-logging", false, 
00285                      "Enable raw output to console" ) );
00286   config->addOption( Configuration::Option( "s", "server-port", true, 
00287                      "Switch on server mode and set port" ) );
00288   config->addOption( Configuration::Option( "h", "help", false, 
00289                      "Show help" ) );
00290   config->addOption( Configuration::Option( "", "html-device-table", false, 
00291                      "Print a HTML table containing the devices\n"
00292                          "    (For the GUI and for documentation)") );
00293   config->addOption( Configuration::Option( "", "device-table", false, 
00294                      "Print a table containing the devices\n" ));
00295   
00296   config->setHelpText( "\nExample: Metex ME-32 at /dev/ttyUSB0\n"
00297       "cdmm --port=/dev/ttyUSB0:600:7:n:2:DSR:DTR:CTS --protocol=M14P or\n"
00298       "cdmm --device=\"Voltcraft ME-32\" --port=/dev/ttyUSB0\n"
00299       "\nThe corresponding parameter file for the last call\nwould be:\n"
00300       "device = Voltcraft ME-32\n"
00301       "port   = /dev/ttyUSB0\n" );
00302   return config;
00303 }
00304 

Generated on Mon Jan 22 23:24:18 2007 for cdmm by  doxygen 1.4.6