00001 //====================================================================== 00002 // File: peaktech10ascii.cpp 00003 // Author: Matthias Toussaint 00004 // Created: Sun Nov 26 14:46:41 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 <peaktech10ascii.h> 00021 00022 #include <iostream> 00023 00024 PeakTech10Ascii::PeakTech10Ascii() : 00025 DMMClass() 00026 { 00027 } 00028 00029 PeakTech10Ascii::~PeakTech10Ascii() 00030 { 00031 m_port.close(); 00032 } 00033 00034 Port::Error PeakTech10Ascii::open_impl( const std::string & config ) 00035 { 00036 return m_port.open( config ); 00037 } 00038 00039 Port::Error PeakTech10Ascii::close() 00040 { 00041 return m_port.close(); 00042 } 00043 00044 void PeakTech10Ascii::run() 00045 { 00046 char data[64]; 00047 00048 while (m_run) 00049 { 00050 int cnt = readData( data ); 00051 00052 if (-1 != cnt) 00053 { 00054 char *bytes = data+cnt-11; 00055 bytes[11] = 0; 00056 std::string dataStr = bytes; 00057 std::string value = dataStr.substr( 1, 6 ); 00058 double d_val; 00059 Util::fromString( &d_val, Util::strip_whitespace( value ) ); 00060 00061 m_mutex.lock(); 00062 addValue( d_val, 0 ); 00063 m_unit[0] = Util::strip_whitespace( dataStr.substr( 7, 4 ) ); 00064 m_mode[0] = ""; 00065 00066 m_hasValue = true; 00067 m_mutex.unlock(); 00068 } 00069 else m_hasValue = false; 00070 } 00071 } 00072 00073 int PeakTech10Ascii::readData( char *data ) const 00074 { 00075 int cnt=0; 00076 int byte; 00077 bool gotcha = false; 00078 00079 do 00080 { 00081 if (m_port.readByte( &byte ) != Port::Ok) 00082 { 00083 return -1; 00084 } 00085 data[cnt++] = byte; 00086 00087 if (byte == '#') 00088 { 00089 gotcha = true; 00090 00091 for (int i=0; i<11; ++i) 00092 { 00093 if (m_port.readByte( &byte ) != Port::Ok) 00094 { 00095 return -1; 00096 } 00097 data[cnt++] = byte; 00098 } 00099 00100 return cnt; 00101 } 00102 } 00103 while (cnt < 63 && !gotcha); 00104 00105 return -1; 00106 }