metexascii.cpp

Go to the documentation of this file.
00001 //======================================================================
00002 // File:        metexascii.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 <metexascii.h>
00021 
00022 #include <iostream>
00023 
00024 MetexAscii::MetexAscii( const std::string & format,
00025                         const std::string & overflowStr,
00026                         bool poll, const std::string & pollString, 
00027                         int pollTime ) :
00028   DMMClass(),
00029   m_overflowStr( overflowStr ),
00030   m_poll( poll ),
00031   m_pollString( pollString ),
00032   m_pollTime( pollTime*1000 )
00033 {
00034   // parse format
00035   //
00036   int byte = 0;
00037   m_length = format.size();
00038   m_syncByte = format[m_length-1];
00039   
00040   int *start=0;
00041   int *len=0;
00042   
00043   for (int i=0; i<m_length; ++i)
00044   {
00045     if (byte != format[i])
00046     {
00047       if (len)
00048       {
00049         *len = i-(*start);
00050       }
00051       
00052       if (format[i] == 'M')
00053       {
00054         start = &m_modeStart;
00055         len   = &m_modeLen;
00056       }
00057       else if (format[i] == 'V')
00058       {
00059         start = &m_valueStart;
00060         len   = &m_valueLen;
00061       }
00062       else if (format[i] == 'U')
00063       {
00064         start = &m_unitStart;
00065         len   = &m_unitLen;
00066       }
00067       else 
00068       {
00069         start = 0;
00070         len = 0;
00071       }
00072       
00073       if (start) 
00074       {
00075         *start = i;
00076       }
00077       
00078       byte = format[i];
00079     }
00080   }
00081 }
00082 
00083 MetexAscii::~MetexAscii()
00084 {
00085   m_port.close();
00086 }
00087 
00088 Port::Error MetexAscii::open_impl( const std::string & config )
00089 {
00090   return m_port.open( config );
00091 }
00092 
00093 Port::Error MetexAscii::close()
00094 {
00095   return m_port.close();
00096 }
00097 
00098 void MetexAscii::setMetexAscii( const std::string & str, int index )
00099 {
00100   m_mutex.lock();
00101       
00102   double value;
00103   Util::fromString( &value, 
00104                     Util::strip_whitespace( str.substr( m_valueStart, m_valueLen )));
00105   addValue( value, index );
00106   m_mode[index] = Util::strip_whitespace( str.substr( m_modeStart, m_modeLen ));      
00107   m_unit[index] = Util::strip_whitespace( str.substr( m_unitStart, m_unitLen ));
00108       
00109   m_hasValue = true;
00110   if (str.substr(m_valueStart,m_valueLen).find(m_overflowStr)<(unsigned)m_valueLen)
00111   {
00112     m_overflow=true;
00113   }
00114   else m_overflow=false;
00115       
00116   m_mutex.unlock(); 
00117 }
00118 
00119 void MetexAscii::run()
00120 {
00121   char data[64];
00122   
00123   while (m_run)
00124   {
00125     if (m_poll)
00126     {
00127       m_port.writeString( m_pollString.c_str(), m_pollString.size() );
00128     }
00129     
00130     for (int i=0; i<m_numValues; ++i)
00131     {
00132       int cnt = readData( &m_port, (unsigned char *)data, 
00133                           m_syncByte, m_length, 3*m_length );
00134       
00135       if (-1 != cnt)
00136       {
00137         int start = cnt-m_length;
00138         data[cnt-1] = 0;
00139         std::string strData = data+start;
00140         //std::cerr << strData << std::endl;
00141         
00142         setMetexAscii( strData, i );
00143       }
00144       else m_hasValue = false;
00145     }
00146     
00147     usleep(m_pollTime);
00148   }
00149 }

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