dmmclass.cpp

Go to the documentation of this file.
00001 //======================================================================
00002 // File:        dmmclass.cpp
00003 // Author:      Matthias Toussaint
00004 // Created:     Sat Nov 25 18:12:23 CET 2006
00005 // Project:     QtDMM
00006 // Description: Encapsulates a DMM class (protokoll)
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 <vector>
00021 #include <dmmclass.h>
00022 #include <metex14bytespolling.h>
00023 #include <metex14bytescontinuous.h>
00024 #include <metex15bytescontinuous.h>
00025 #include <vc820.h>
00026 #include <m9803r.h>
00027 #include <peaktech10ascii.h>
00028 #include <iocbuvmonitor.h>
00029 #include <gdm703.h>
00030 #include <escort97.h>
00031 
00032 #include <iostream>
00033 
00034 DMMClass::Protocol DMMClass::s_protocol[] = 
00035 {
00036   Protocol( &Metex14BytesPolling::create, "M14P", "Metex 14 bytes ASCII polling" ),
00037   Protocol( &Metex14BytesContinuous::create, "M14C", "Metex 14 bytes ASCII continuous" ),
00038   Protocol( &Metex15BytesContinuous::create, "M15C", "Metex 15 bytes ASCII continuous" ),
00039   Protocol( &VC820::create, "VC820", "VC820 14 bytes binary continuous" ),
00040   Protocol( &M9803R::create, "M9803R", "M9803R 12 bytes binary continuous" ),
00041   Protocol( &IOCBUVMonitor::create, "IOCB", "IOCB UV Monitor" ),
00042   Protocol( &PeakTech10Ascii::create, "PT10A", "Peaktech 11 bytes ASCII" ),
00043   Protocol( &GDM703::create, "GDM703", "GDM703 26 bytes binary continuous" ),
00044   Protocol( &Escort97::create, "ESC97", "Escort 97 binary continuous" ),
00045   // end of list
00046   Protocol( 0, "", "" ) 
00047 };
00048   
00049 DMMClass::Device DMMClass::s_device[] =
00050 {
00051   Device( "Digitek", "DT-9062", "VC820", "2400:8:n:1:RTS:DSR:DTR:CTS", 1 ),
00052   
00053   Device( "Digitech", "QM-1350", "M14P", "600:7:n:2:DSR:DTR:CTS", 1 ),
00054   Device( "Digitech", "QM1538", "VC820", "2400:8:n:1:RTS:DSR:DTR:CTS", 1 ),
00055   
00056   Device( "ELV", "M9803R", "M9803R", "9600:7:e:1:DSR:DTR:CTS", 1 ),
00057   
00058   Device( "Escort", "95", "Escort97", "2400:6:n:1:RTS:DSR:DTR:CTS", 1 ),
00059   Device( "Escort", "97", "Escort97", "2400:6:n:1:RTS:DSR:DTR:CTS", 1 ),
00060   
00061   Device( "MASTECH", "M9803R", "M9803R", "9600:7:e:1:DSR:DTR:CTS", 1 ),
00062   Device( "MASTECH", "MAS-345", "M14P", "600:7:n:2:DSR:DTR:CTS", 1 ),
00063   
00064   Device( "McVoice", "M-345pro", "M14P", "600:7:n:2:DSR:DTR:CTS", 1 ),
00065   Device( "McVoice", "M-980T", "M9803R", "9600:7:e:1:DSR:DTR:CTS", 1 ),
00066   
00067   Device( "Metex", "M-3360D", "M14P", "1200:7:n:2:DSR:DTR:CTS", 1 ),
00068   Device( "Metex", "M-3830D", "M14P", "1200:7:n:2:DSR:DTR:CTS", 4 ),
00069   Device( "Metex", "M-3850D", "M14P", "1200:7:n:2:DSR:DTR:CTS", 4 ),
00070   Device( "Metex", "M-3850M", "M14P", "9600:7:n:2:DSR:DTR:CTS", 4 ),
00071   Device( "Metex", "M9803R", "M9803R", "9600:7:e:1:DSR:DTR:CTS", 1 ),
00072   Device( "Metex", "ME-11", "M14P", "600:7:n:2:DSR:DTR:CTS", 1 ),
00073   Device( "Metex", "ME-22", "M14P", "600:7:n:2:DSR:DTR:CTS", 1 ),
00074   Device( "Metex", "ME-32", "M14P", "600:7:n:2:DSR:DTR:CTS", 1 ),
00075   Device( "Metex", "ME-42", "M14P", "600:7:n:2:DSR:DTR:CTS", 1 ),
00076   Device( "Metex", "Universal System 9160", "M14P", "1200:7:n:2:DSR:DTR:CTS", 1 ),
00077   
00078   Device( "PeakTech", "4010", "M14P", "9600:7:n:2:DSR:DTR:CTS", 1 ),
00079   Device( "PeakTech", "4390", "M14P", "9600:7:n:2:DSR:DTR:CTS", 4 ),
00080   Device( "PeakTech", "451", "PT10A", "600:7:n:2:DSR:DTR:CTS", 1 ),
00081   
00082   Device( "Radioshack", "22-805 DMM", "M14P", "600:7:n:2:DSR:DTR:CTS", 1 ),
00083   Device( "Radioshack", "RS22-168A", "M14P", "1200:7:n:2:DSR:DTR:CTS", 1 ),
00084   
00085   Device( "Uni-Trend", "UT30A", "VC820", "2400:8:n:1:RTS:DSR:DTR:CTS", 1 ),
00086   Device( "Uni-Trend", "UT30E", "VC820", "2400:8:n:1:RTS:DSR:DTR:CTS", 1 ),
00087   
00088   Device( "Voltcraft", "GDM703", "GDM703", "9600:8:n:1:DSR:DTR:CTS", 2 ),
00089   Device( "Voltcraft", "GDM704", "GDM703", "9600:8:n:1:DSR:DTR:CTS", 2 ),
00090   Device( "Voltcraft", "GDM705", "GDM703", "9600:8:n:1:DSR:DTR:CTS", 2 ),
00091   Device( "Voltcraft", "M-3650D", "M14P", "1200:7:n:2:DSR:DTR:CTS", 1 ),
00092   Device( "Voltcraft", "M-3830", "M14P", "1200:7:n:2:DSR:DTR:CTS", 1 ),
00093   Device( "Voltcraft", "M-3850", "M14P", "1200:7:n:2:DSR:DTR:CTS", 1 ),
00094   Device( "Voltcraft", "M-3860", "M14P", "9600:7:n:2:DSR:DTR:CTS", 4 ),
00095   Device( "Voltcraft", "M-4660", "M14P", "1200:7:n:2:DSR:DTR:CTS", 4 ),
00096   Device( "Voltcraft", "M-4660A", "M14P", "9600:7:n:2:DSR:DTR:CTS", 4 ),
00097   Device( "Voltcraft", "M-4660M", "M14P", "9600:7:n:2:DSR:DTR:CTS", 4 ),
00098   Device( "Voltcraft", "M-465CR", "M14C", "1200:7:n:2:DSR:DTR:CTS", 1 ),
00099   Device( "Voltcraft", "ME-11", "M14P", "600:7:n:2:DSR:DTR:CTS", 1 ),
00100   Device( "Voltcraft", "ME-22T", "M14P", "2400:7:n:2:DSR:DTR:CTS", 1 ),
00101   Device( "Voltcraft", "ME-32", "M14P", "600:7:n:2:DSR:DTR:CTS", 1 ),
00102   Device( "Voltcraft", "ME-42", "M14P", "600:7:n:2:DSR:DTR:CTS", 1 ),
00103   Device( "Voltcraft", "MXD-4660A", "M14P", "9600:7:n:2:DSR:DTR:CTS", 4 ),
00104   Device( "Voltcraft", "VC 630", "M14C", "4800:7:n:1:RTS:DSR:DTR:CTS", 1 ),
00105   Device( "Voltcraft", "VC 650", "M14C", "4800:7:n:1:RTS:DSR:DTR:CTS", 1 ),
00106   Device( "Voltcraft", "VC 670", "M14C", "4800:7:n:1:RTS:DSR:DTR:CTS", 1 ),
00107   Device( "Voltcraft", "VC 635", "M15C", "2400:7:n:1:RTS:DSR:DTR:CTS", 1 ),
00108   Device( "Voltcraft", "VC 655", "M15C", "2400:7:n:1:RTS:DSR:DTR:CTS", 1 ),
00109   Device( "Voltcraft", "VC 820", "VC820", "2400:8:n:1:RTS:DSR:DTR:CTS", 1 ),
00110   Device( "Voltcraft", "VC 840", "VC820", "2400:8:n:1:RTS:DSR:DTR:CTS", 1 ),
00111   
00112   Device( "IOCB", "UV Monitor", "IOCB", "9600:8:n:1:DSR:DTR:CTS", 1 ),
00113   // end of list
00114   Device( "", "", "", "", 0 )
00115 };
00116 
00117 DMMClass::DMMClass() :
00118   m_hasValue( false ),
00119   m_numValues( 1 ),
00120   m_overflow( false ),
00121   m_lowBat( false ),
00122   m_consoleLogging( false )
00123 {
00124 }
00125 
00126 DMMClass::~DMMClass()
00127 {
00128 }
00129 
00130 Port::Error DMMClass::open( const std::string & config )
00131 {
00132   m_openTime = time(0);
00133   
00134   return open_impl( config );
00135 }
00136 
00137 bool DMMClass::timeout() const
00138 {
00139   if (!m_hasValue && (time(0)-m_openTime)>5) return true;
00140   
00141   return false;
00142 }
00143 
00144 std::string DMMClass::mode( int i )
00145 {
00146   MutexLocker lock( &m_mutex );
00147   return m_mode[i];
00148 }
00149 
00150 std::string DMMClass::unit( int i )
00151 {
00152   MutexLocker lock( &m_mutex );
00153   return m_unit[i];
00154 }
00155 
00156 void DMMClass::addValue( double value, int index )
00157 {
00158   m_value[index] = value;
00159 }
00160 
00161 double DMMClass::value( int index )
00162 {
00163   MutexLocker lock( &m_mutex );  
00164   return m_value[index];
00165 }
00166 
00167 int DMMClass::readData( Port *port, unsigned char *buffer, 
00168                         int syncByte, int minLen, int maxLen ) const
00169 {
00170   int cnt=0;
00171   int byte;
00172   
00173   do
00174   {
00175     if (port->readByte( &byte ) != Port::Ok)
00176     {
00177       return -1;
00178     }
00179     buffer[cnt++] = byte;
00180     
00181     if (byte == syncByte && cnt >= minLen) 
00182     {
00183       if (m_consoleLogging)
00184       {
00185         for (int i=0; i<cnt; ++i)
00186         {
00187           fprintf( stderr, "0x%02X ", (int)buffer[i] );
00188         }
00189         fprintf( stderr, "\n" );
00190       }
00191       return cnt;
00192     }
00193   }
00194   while (cnt < maxLen && byte != syncByte);
00195   
00196   return -1;
00197 }
00198 
00199 DMMClass *DMMClass::create_protocol( std::string protocolName )
00200 {
00201   for (int i=0; i<DMMClass::num_protocols(); ++i)
00202   {
00203     if (s_protocol[i].name == protocolName)
00204     {
00205       return s_protocol[i].createFunction();
00206     }
00207   }
00208   
00209   return 0;
00210 }
00211 
00212 int DMMClass::num_protocols()
00213 {
00214   static int cnt = count_protocols();
00215   
00216   return cnt;
00217 }
00218 
00219 int DMMClass::count_protocols()
00220 {
00221   int cnt=0;
00222   
00223   while (s_protocol[cnt].createFunction) cnt++;
00224   
00225   return cnt;
00226 }
00227 
00228 std::string DMMClass::print_protocols()
00229 {
00230   std::string table; 
00231   std::stringstream ss( table );
00232   
00233   int cnt=0;
00234   while (s_protocol[cnt].createFunction) 
00235   {
00236     ss << s_protocol[cnt].name
00237        << ": " << s_protocol[cnt].menuText << "\n";
00238     cnt++;
00239   }
00240   
00241   return ss.str();
00242 }
00243 
00244 std::string DMMClass::print_devices()
00245 {
00246   std::string table; 
00247   std::stringstream ss( table );
00248   
00249   int cnt=0;
00250   while (s_device[cnt].numValues) 
00251   {
00252     ss << s_device[cnt].brand
00253        << " " << s_device[cnt].name << "\n";
00254     cnt++;
00255   }
00256   
00257   return ss.str();
00258 }
00259 
00260 bool DMMClass::has_device( const std::string & deviceName )
00261 {
00262   int cnt=0;
00263   
00264   while (s_device[cnt].numValues) 
00265   {
00266     std::string name = s_device[cnt].brand;
00267     name += " ";
00268     name += s_device[cnt].name;
00269     
00270     if (name == deviceName) return true;
00271     cnt++;
00272   }
00273   
00274   return false;
00275 }
00276 
00277 DMMClass::Device 
00278 DMMClass::get_device( const std::string & deviceName )
00279 {
00280   int cnt=0;
00281   
00282   while (s_device[cnt].numValues) 
00283   {
00284     std::string name = s_device[cnt].brand;
00285     name += " ";
00286     name += s_device[cnt].name;
00287     
00288     if (name == deviceName) return s_device[cnt];
00289     cnt++;
00290   }
00291   
00292   return s_device[cnt];
00293 }
00294 
00295 std::string DMMClass::device_table()
00296 {
00297   std::string table; 
00298   std::stringstream ss( table );
00299   
00300   int cnt=0;
00301   
00302   while (s_device[cnt].numValues) 
00303   {
00304     ss << s_device[cnt].brand;
00305     ss << ", ";
00306     ss << s_device[cnt].name;
00307     ss << ", ";
00308     TokenList tokenList;
00309     std::string argument = s_device[cnt].port;
00310     std::string token;
00311       
00312     for (unsigned i=0; i<argument.size(); ++i)
00313     {
00314       if (argument[i] != ':')
00315       {
00316         token += argument[i];
00317       }
00318       else
00319       {
00320         tokenList.push_back( token );
00321         token = "";
00322       }
00323     }
00324     if (token.size()) tokenList.push_back( token );
00325     
00326     ss << tokenList[0];
00327     ss << ", ";
00328     ss << tokenList[1] + tokenList[2] + tokenList[3];
00329     ss << ", ";
00330     
00331     if (tokenList.size() > 4)
00332     {
00333       for (unsigned i=4; i<tokenList.size(); ++i)
00334       {
00335         if (i>4) ss << ", ";
00336         ss << tokenList[i];
00337       }
00338     }
00339     else ss << ", ";
00340     
00341     ss << ", ";
00342     ss << "???";
00343     ss << ", ";
00344     ss << s_device[cnt].numValues;
00345     ss << ", ";
00346     ss << s_device[cnt].protocol;
00347     ss << "\n";
00348     
00349     cnt++;
00350   }
00351   
00352   return ss.str();
00353 }
00354 
00355 std::string DMMClass::html_device_table()
00356 {
00357   std::string table; 
00358   std::stringstream ss( table );
00359   
00360   ss << "<table border=\"1\" cellpadding=\"10\" cellspacing=\"0\">\n";
00361   ss << "<tr><td><b>Brand</b></td><td><b>Model</b></td>";
00362   ss << "<td><b>Baud</b></td><td><b>Bits</b></td>";
00363   ss << "<td><b>Flags</b></td><td><b>Counts</b></td>";
00364   ss << "<td><b>Values</b></td><td><b>Protocol</b></td></tr>\n";
00365   
00366   int cnt=0;
00367   std::string lastBrand;
00368   
00369   while (s_device[cnt].numValues) 
00370   {
00371     if (s_device[cnt].brand != lastBrand)
00372     {
00373       if (lastBrand.size())
00374       {
00375         ss << "<tr><Td colspan=\"8 \">&nbsp;</Td></tr>\n";
00376       }
00377       ss << "<tr><td>";
00378       ss << s_device[cnt].brand;
00379     }
00380     else ss << "<tr><td>&nbsp;";
00381     lastBrand = s_device[cnt].brand;
00382     ss << "</td><td>";
00383     ss << s_device[cnt].name;
00384     
00385     TokenList tokenList;
00386     std::string argument = s_device[cnt].port;
00387     std::string token;
00388       
00389     for (unsigned i=0; i<argument.size(); ++i)
00390     {
00391       if (argument[i] != ':')
00392       {
00393         token += argument[i];
00394       }
00395       else
00396       {
00397         tokenList.push_back( token );
00398         token = "";
00399       }
00400     }
00401     if (token.size()) tokenList.push_back( token );
00402     
00403     ss << "</td><td>";
00404     ss << tokenList[0];
00405     ss << "</td><td>";
00406     ss << tokenList[1] + tokenList[2] + tokenList[3];
00407     ss << "</td><td>";
00408     
00409     if (tokenList.size() > 4)
00410     {
00411       for (unsigned i=4; i<tokenList.size(); ++i)
00412       {
00413         if (i>4) ss << ", ";
00414         ss << tokenList[i];
00415       }
00416     }
00417     else ss << "&nbsp;";
00418     
00419     ss << "</td><td>";
00420     ss << "???";
00421     ss << "</td><td>";
00422     ss << s_device[cnt].numValues;
00423     ss << "</td><td>";
00424     ss << s_device[cnt].protocol;
00425     ss << "</td></tr>\n";
00426     
00427     cnt++;
00428   }
00429   
00430   ss << "</table>\n";
00431   
00432   return ss.str();
00433 }

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