IGSTK
|
00001 /*========================================================================= 00002 00003 Program: Image Guided Surgery Software Toolkit 00004 Module: $RCSfile: igstkSerialCommunication.h,v $ 00005 Language: C++ 00006 Date: $Date: 2008-02-11 01:41:51 $ 00007 Version: $Revision: 1.16 $ 00008 00009 Copyright (c) ISC Insight Software Consortium. All rights reserved. 00010 See IGSTKCopyright.txt or http://www.igstk.org/copyright.htm for details. 00011 00012 This software is distributed WITHOUT ANY WARRANTY; without even 00013 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 00014 PURPOSE. See the above copyright notices for more information. 00015 00016 =========================================================================*/ 00017 00018 #ifndef __igstkSerialCommunication_h 00019 #define __igstkSerialCommunication_h 00020 00021 // Disabling warning C4355: 'this' : used in base member initializer list 00022 #if defined(_MSC_VER) 00023 #pragma warning ( disable : 4355 ) 00024 #endif 00025 00026 #include "itkObject.h" 00027 #include "itkEventObject.h" 00028 #include "igstkLogger.h" 00029 #include "itkStdStreamLogOutput.h" 00030 00031 #include "igstkMacros.h" 00032 #include "igstkEvents.h" 00033 #include "igstkCommunication.h" 00034 #include "igstkStateMachine.h" 00035 00036 00037 namespace igstk 00038 { 00039 00063 class SerialCommunication : public Communication 00064 { 00065 public: 00066 00068 enum PortNumberType { PortNumber0 = 0, 00069 PortNumber1 = 1, 00070 PortNumber2 = 2, 00071 PortNumber3 = 3, 00072 PortNumber4 = 4, 00073 PortNumber5 = 5, 00074 PortNumber6 = 6, 00075 PortNumber7 = 7 }; 00076 00078 enum BaudRateType { BaudRate9600 = 9600, 00079 BaudRate19200 = 19200, 00080 BaudRate38400 = 38400, 00081 BaudRate57600 = 57600, 00082 BaudRate115200 = 115200 }; 00083 00085 enum DataBitsType { DataBits7 = 7, 00086 DataBits8 = 8 }; 00087 00089 enum ParityType { NoParity = 'N', 00090 OddParity = 'O', 00091 EvenParity = 'E' }; 00092 00094 enum StopBitsType { StopBits1 = 1, 00095 StopBits2 = 2 }; 00096 00098 enum HandshakeType { HandshakeOff = 0, 00099 HandshakeOn = 1 }; 00100 00101 typedef Communication::ResultType ResultType; 00102 00104 igstkStandardClassBasicTraitsMacro( SerialCommunication, Communication ); 00105 00111 static Pointer New(void); 00112 00113 00116 igstkSetMacro( PortNumber, PortNumberType ); 00118 igstkGetMacro( PortNumber, PortNumberType ); 00119 00122 igstkSetMacro( BaudRate, BaudRateType ); 00124 igstkGetMacro( BaudRate, BaudRateType ); 00125 00128 igstkSetMacro( DataBits, DataBitsType ); 00130 igstkGetMacro( DataBits, DataBitsType ); 00131 00133 igstkSetMacro( Parity, ParityType ); 00135 igstkGetMacro( Parity, ParityType ); 00136 00138 igstkSetMacro( StopBits, StopBitsType ); 00140 igstkGetMacro( StopBits, StopBitsType ); 00141 00143 igstkSetMacro( HardwareHandshake, HandshakeType ); 00145 igstkGetMacro( HardwareHandshake, HandshakeType ); 00146 00148 void SetCaptureFileName(const char* filename); 00150 const char* GetCaptureFileName() const; 00151 00153 igstkSetMacro( Capture, bool ); 00155 igstkGetMacro( Capture, bool ); 00156 00159 ResultType UpdateParameters( void ); 00160 00163 ResultType OpenCommunication( void ); 00164 00166 ResultType CloseCommunication( void ); 00167 00171 ResultType SetRTS( unsigned int signal ); 00172 00174 ResultType Write( const char *message, unsigned int numberOfBytes ); 00175 00179 ResultType Read( char *data, unsigned int numberOfBytes, 00180 unsigned int &bytesRead ); 00181 00187 ResultType SendBreak( void ); 00188 00193 ResultType PurgeBuffers( void ); 00194 00199 void Sleep( unsigned int milliseconds ); 00200 00202 igstkStateMachineMacro(); 00203 00205 igstkLoggerMacro(); 00206 00207 protected: 00208 00209 SerialCommunication(); 00210 00211 ~SerialCommunication(); 00212 00213 // These methods are the interface to the derived classes. 00214 00216 virtual ResultType InternalOpenPort( void ) { return SUCCESS; } 00217 00219 virtual ResultType InternalUpdateParameters( void ) { return SUCCESS; } 00220 00222 virtual ResultType InternalClosePort( void ) { return SUCCESS; } 00223 00225 virtual ResultType InternalSetRTS( unsigned int ) { return SUCCESS; } 00226 00228 virtual ResultType InternalWrite( const char *, unsigned int ) { 00229 return TIMEOUT; } 00230 00232 virtual ResultType InternalRead( char *, unsigned int, unsigned int &) { 00233 return TIMEOUT; } 00234 00236 virtual ResultType InternalSendBreak( void ) { return SUCCESS; } 00237 00239 virtual ResultType InternalPurgeBuffers( void ) { return SUCCESS; } 00240 00242 virtual void InternalSleep( unsigned int ) {}; 00243 00245 virtual void PrintSelf( std::ostream& os, itk::Indent indent ) const; 00246 00247 private: 00248 00249 // Communication Parameters 00250 00252 PortNumberType m_PortNumber; 00253 00255 BaudRateType m_BaudRate; 00256 00258 DataBitsType m_DataBits; 00259 00261 ParityType m_Parity; 00262 00264 StopBitsType m_StopBits; 00265 00267 HandshakeType m_HardwareHandshake; 00268 00270 unsigned int m_SleepPeriod; 00271 00273 char *m_InputData; 00274 00276 const char *m_OutputData; 00277 00279 unsigned int m_BytesToWrite; 00280 00282 unsigned int m_BytesToRead; 00283 00285 unsigned int m_BytesRead; 00286 00288 typedef std::map<int, InputType> IntegerInputMapType; 00289 00291 std::string m_CaptureFileName; 00292 00294 std::ofstream m_CaptureFileStream; 00295 00297 unsigned int m_CaptureMessageNumber; 00298 00300 bool m_Capture; 00301 00303 igstk::Object::LoggerType::Pointer m_Recorder; 00304 00306 unsigned int m_RTSSignal; 00307 00309 itk::StdStreamLogOutput::Pointer m_CaptureFileOutput; 00310 00312 ResultType m_ReturnValue; 00313 00315 IntegerInputMapType m_ResultInputMap; 00316 00317 // List of States 00318 igstkDeclareStateMacro( Idle ); 00319 igstkDeclareStateMacro( AttemptingToOpenPort ); 00320 igstkDeclareStateMacro( PortOpen ); 00321 igstkDeclareStateMacro( AttemptingToUpdateParameters ); 00322 igstkDeclareStateMacro( ReadyForCommunication ); 00323 igstkDeclareStateMacro( AttemptingToClosePort ); 00324 igstkDeclareStateMacro( AttemptingToRead ); 00325 igstkDeclareStateMacro( AttemptingToWrite ); 00326 igstkDeclareStateMacro( AttemptingToSendBreak ); 00327 igstkDeclareStateMacro( AttemptingToPurgeBuffers ); 00328 igstkDeclareStateMacro( AttemptingToSetRTS ); 00329 igstkDeclareStateMacro( Sleep ); 00330 00331 // List of Inputs 00332 igstkDeclareInputMacro( Success ); 00333 igstkDeclareInputMacro( Failure ); 00334 igstkDeclareInputMacro( Timeout ); 00335 igstkDeclareInputMacro( OpenPort ); 00336 igstkDeclareInputMacro( ClosePort ); 00337 igstkDeclareInputMacro( UpdateParameters ); 00338 igstkDeclareInputMacro( Read ); 00339 igstkDeclareInputMacro( Write ); 00340 igstkDeclareInputMacro( SendBreak ); 00341 igstkDeclareInputMacro( PurgeBuffers ); 00342 igstkDeclareInputMacro( Sleep ); 00343 igstkDeclareInputMacro( SetRTS ); 00344 00346 void OpenPortSuccessProcessing( void ); 00347 00349 void OpenPortFailureProcessing( void ); 00350 00352 void ClosePortSuccessProcessing( void ); 00353 00355 void ClosePortFailureProcessing( void ); 00356 00358 void SuccessProcessing( void ); 00359 00361 void FailureProcessing( void ); 00362 00364 void TimeoutProcessing( void ); 00365 00367 void NoProcessing(); 00368 00370 void AttemptToOpenPortProcessing( void ); 00371 00373 void AttemptToUpdateParametersProcessing( void ); 00374 00376 void AttemptToClosePortProcessing( void ); 00377 00379 void AttemptToSetRTSProcessing( void ); 00380 00382 void AttemptToWriteProcessing( void ); 00383 00385 void AttemptToReadProcessing( void ); 00386 00388 void AttemptToSendBreakProcessing( void ); 00389 00391 void AttemptToPurgeBuffersProcessing( void ); 00392 00394 void SleepProcessing( void ); 00395 00397 const InputType &MapResultToInput( int condition ); 00398 }; 00399 00400 } // end namespace igstk 00401 00402 #endif // __igstkSerialCommunication_h