メインページ   モジュール   名前空間一覧   クラス階層   アルファベット順一覧   構成   ファイル一覧   構成メンバ   ファイルメンバ   関連ページ    

TRs232cModule.cc

解説を見る。
00001 // =====================================================================
00002 //  $Id: TRs232cModule.cc,v 1.4 2004/03/07 10:30:31 goiwai Exp $
00003 //  $Name: CLDAQ-1-14-03 $
00004 //  $Log: TRs232cModule.cc,v $
00005 //  Revision 1.4  2004/03/07 10:30:31  goiwai
00006 //  ROOTに組みこむためのおまじないマクロを埋めこみました。
00007 //  全てにおいて完全に動作するわけではありません。
00008 //
00009 //  Revision 1.3  2003/10/06 17:02:40  goiwai
00010 //  *** empty log message ***
00011 //
00012 //  Revision 1.2  2003/07/30 16:19:11  goiwai
00013 //  ファイルにコミットログをつけることにしました.
00014 //
00015 // =====================================================================
00016 #include "TRs232cModule.hh"
00017 #include "TDataElement.hh"
00018 
00019 static const Tchar _CR = '\x0d';
00020 static const Tchar _LF = '\x0a';
00021 
00022 TRs232cModule::TRs232cModule( const Tstring devfile, Tint nchannel )
00023   : TModule( nchannel ), 
00024     theFileDescriptor( -1 ), 
00025     theDeviceFile( devfile ),
00026     theSerialPort()
00027 {
00028   if ( !IsOpen() ) {
00029     Open();
00030   }
00031 }
00032 
00033 TRs232cModule::TRs232cModule( Tint nchannel, Tstring devfile )
00034   : TModule( nchannel ), 
00035     theFileDescriptor( -1 ), 
00036     theDeviceFile( devfile ),
00037     theSerialPort()
00038 {
00039   if ( !IsOpen() ) {
00040     Open();
00041   }
00042 }
00043 
00044 TRs232cModule::TRs232cModule( const TRs232cModule& right )
00045   : TModule( right ), 
00046     theFileDescriptor( -1 ), 
00047     theDeviceFile( right.theDeviceFile ), 
00048     theSerialPort( right.theSerialPort )
00049 {
00050   if ( !IsOpen() ) {
00051     Open();
00052   }
00053 }
00054 
00055 TRs232cModule::~TRs232cModule()
00056 {
00057   Close();
00058 }
00059 
00060 const TRs232cModule& TRs232cModule::operator=( const TRs232cModule& right )
00061 {
00062   *( (TModule*)this ) = *( (TModule*)(&right) );
00063   if ( IsOpen() ) {
00064     Close();
00065   }
00066   theFileDescriptor = -1;
00067   theDeviceFile = right.theDeviceFile;
00068   theSerialPort = right.theSerialPort;
00069   Open();
00070   return *this;
00071 }
00072 
00073 Tbool TRs232cModule::operator==( const TRs232cModule& right ) const
00074 {
00075   Tbool retval = Ttrue;
00076   retval &= ( *( (TModule*)this ) == *( (TModule*)(&right) ) );
00077   retval &= ( theDeviceFile == right.theDeviceFile );
00078   return retval;
00079 }
00080 
00081 Tbool TRs232cModule::operator!=( const TRs232cModule& right ) const
00082 {
00083   Tbool retval = Tfalse;
00084   retval |= ( *( (TModule*)this ) != *( (TModule*)(&right) ) );
00085   retval |= ( theDeviceFile != right.theDeviceFile );
00086   return retval;
00087 }
00088 
00089 Tint TRs232cModule::Clear()
00090 {
00091   for ( Tint ch = 0; ch < theNumberOfChannels; ch ++ ) {
00092     DoClearMethod( ch );
00093   }
00094   return theStatus = tStatusSuccess;
00095 }
00096 
00097 Tint TRs232cModule::Update()
00098 {
00099   for ( Tint ch = 0; ch < theNumberOfChannels; ch ++ ) {
00100     DoUpdateMethod( ch );
00101   }
00102   return theStatus = tStatusSuccess;
00103 }
00104 
00105 Tint TRs232cModule::Initialize()
00106 {
00107   for ( Tint ch = 0; ch < theNumberOfChannels; ch ++ ) {
00108     DoInitializeMethod( ch );
00109   }
00110   return theStatus = tStatusSuccess;
00111 }
00112 
00113 Tvoid TRs232cModule::FillData( TDataElement& element, Tint channel )
00114 {
00115   if ( channel < 0 || channel >= theNumberOfChannels ) {
00116     theStatus = -EFAULT;
00117     Tcerr << "TRs232cModule::FillData: invalid ID " << channel << Tendl;
00118     element.FillData( &theStatus, tTypeInt, 1 );
00119   } else {
00120     Tstring accept = DoReadMethod( channel );
00121     Tdouble databuf;
00122     if ( modifyData( accept, databuf ) ) {
00123       element.FillData( &databuf, tTypeDouble, 1 );
00124     } else {
00125       element.FillData( &accept, tTypeString, 1 );
00126     }
00127   }
00128   return;
00129 }
00130 
00131 Tint TRs232cModule::Open()
00132 {
00133   theFileDescriptor = open( theDeviceFile.c_str(), O_RDWR );
00134   if ( theFileDescriptor == -1 ) {
00135     perror( "TRs232cModule::Open" );
00136     exit( theStatus = -errno );
00137   }
00138 
00139 
00140   if( ioctl( theFileDescriptor, TCGETA, &theSerialPort ) == -1 ) {
00141     perror( "TRs232cModule::Open" );
00142     exit( theStatus = -errno );
00143   }
00144 
00145   struct termio termbuf = theSerialPort;
00146   termbuf.c_lflag &= ~ECHO;
00147   //
00148   // DEFAULT SPEED.これは問題あるかもしんない
00149   termbuf.c_cflag |= B9600;
00150   //
00151   //
00152   termbuf.c_cflag |= CS8;
00153   termbuf.c_cflag |= CREAD;
00154   termbuf.c_iflag |= ICRNL;
00155   termbuf.c_iflag |= ICANON;
00156   termbuf.c_iflag |= INLCR;
00157   termbuf.c_iflag &= ~OPOST;
00158   termbuf.c_oflag |= OCRNL;
00159   termbuf.c_oflag &= ~BRKINT;
00160   termbuf.c_oflag &= ~ISTRIP;
00161   termbuf.c_oflag &= ~IMAXBEL;
00162   termbuf.c_cflag &= ~PARENB;
00163 
00164   if ( ioctl( theFileDescriptor, TCSETA, &termbuf ) == -1 ) {
00165     perror( "TRs232cModule::Open" );
00166     exit( theStatus = -errno );
00167   }
00168 
00169   return theStatus = tStatusSuccess;
00170 }
00171 
00172 Tint TRs232cModule::Write( const Tstring& token )
00173 {
00174   if ( write( theFileDescriptor, token.c_str(), token.size() ) != (Tint)token.size() ) {
00175     perror( "TRs232cModule::Write" );
00176     exit( theStatus = -errno );
00177   }
00178   if ( token[ token.size() - 1 ] != _LF ) {
00179     if ( write( theFileDescriptor, &_LF, 1 ) != 1 ) {
00180       perror( "TRs232cModule::Write" );
00181       exit( theStatus = -errno );
00182     }
00183   }
00184   return theStatus = tStatusSuccess;
00185 }
00186 
00187 Tstring TRs232cModule::Read()
00188 {
00189 
00190   Tstring answer;
00191   Tchar cbuf;
00192   while ( read( theFileDescriptor, &cbuf, 1 ) == 1 && cbuf != _LF ) {
00193     answer += cbuf;
00194   }
00195   theStatus = tStatusSuccess;
00196   return answer;
00197 }
00198 
00199 Tint TRs232cModule::Close()
00200 {
00201   if ( IsOpen() ) {
00202     if ( ioctl( theFileDescriptor, TCSETA, &theSerialPort ) == -1 ) {
00203       perror( "TRs232cModule::Close" );
00204       exit( theStatus = -errno );
00205     }
00206     close( theFileDescriptor );
00207     theFileDescriptor = -1;
00208   }
00209   return theStatus = tStatusSuccess;
00210 }
00211 
00212 #ifdef __CLDAQ_ROOT_DLL
00213     ClassImp(TRs232cModule)
00214 #endif


CLDAQ - a Class Library for DataAcQuisition (Version 1.14.3)
Go IWAI -- goiwai at users.sourceforge.jp