IGSTK
igstkSerialCommunication.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Image Guided Surgery Software Toolkit
4  Module: $RCSfile: igstkSerialCommunication.h,v $
5  Language: C++
6  Date: $Date: 2008-02-11 01:41:51 $
7  Version: $Revision: 1.16 $
8 
9  Copyright (c) ISC Insight Software Consortium. All rights reserved.
10  See IGSTKCopyright.txt or http://www.igstk.org/copyright.htm for details.
11 
12  This software is distributed WITHOUT ANY WARRANTY; without even
13  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14  PURPOSE. See the above copyright notices for more information.
15 
16 =========================================================================*/
17 
18 #ifndef __igstkSerialCommunication_h
19 #define __igstkSerialCommunication_h
20 
21 // Disabling warning C4355: 'this' : used in base member initializer list
22 #if defined(_MSC_VER)
23 #pragma warning ( disable : 4355 )
24 #endif
25 
26 #include "itkObject.h"
27 #include "itkEventObject.h"
28 #include "igstkLogger.h"
29 #include "itkStdStreamLogOutput.h"
30 
31 #include "igstkMacros.h"
32 #include "igstkEvents.h"
33 #include "igstkCommunication.h"
34 #include "igstkStateMachine.h"
35 
36 
37 namespace igstk
38 {
39 
64 {
65 public:
66 
75  PortNumber7 = 7 };
76 
78  enum BaudRateType { BaudRate9600 = 9600,
79  BaudRate19200 = 19200,
80  BaudRate38400 = 38400,
81  BaudRate57600 = 57600,
82  BaudRate115200 = 115200 };
83 
85  enum DataBitsType { DataBits7 = 7,
86  DataBits8 = 8 };
87 
89  enum ParityType { NoParity = 'N',
90  OddParity = 'O',
91  EvenParity = 'E' };
92 
94  enum StopBitsType { StopBits1 = 1,
95  StopBits2 = 2 };
96 
99  HandshakeOn = 1 };
100 
101  typedef Communication::ResultType ResultType;
102 
105 
111  static Pointer New(void);
112 
113 
116  igstkSetMacro( PortNumber, PortNumberType );
118  igstkGetMacro( PortNumber, PortNumberType );
119 
122  igstkSetMacro( BaudRate, BaudRateType );
124  igstkGetMacro( BaudRate, BaudRateType );
125 
128  igstkSetMacro( DataBits, DataBitsType );
130  igstkGetMacro( DataBits, DataBitsType );
131 
133  igstkSetMacro( Parity, ParityType );
135  igstkGetMacro( Parity, ParityType );
136 
138  igstkSetMacro( StopBits, StopBitsType );
140  igstkGetMacro( StopBits, StopBitsType );
141 
143  igstkSetMacro( HardwareHandshake, HandshakeType );
145  igstkGetMacro( HardwareHandshake, HandshakeType );
146 
148  void SetCaptureFileName(const char* filename);
150  const char* GetCaptureFileName() const;
151 
153  igstkSetMacro( Capture, bool );
155  igstkGetMacro( Capture, bool );
156 
160 
164 
167 
171  ResultType SetRTS( unsigned int signal );
172 
174  ResultType Write( const char *message, unsigned int numberOfBytes );
175 
179  ResultType Read( char *data, unsigned int numberOfBytes,
180  unsigned int &bytesRead );
181 
187  ResultType SendBreak( void );
188 
193  ResultType PurgeBuffers( void );
194 
199  void Sleep( unsigned int milliseconds );
200 
203 
206 
207 protected:
208 
210 
212 
213  // These methods are the interface to the derived classes.
214 
216  virtual ResultType InternalOpenPort( void ) { return SUCCESS; }
217 
219  virtual ResultType InternalUpdateParameters( void ) { return SUCCESS; }
220 
222  virtual ResultType InternalClosePort( void ) { return SUCCESS; }
223 
225  virtual ResultType InternalSetRTS( unsigned int ) { return SUCCESS; }
226 
228  virtual ResultType InternalWrite( const char *, unsigned int ) {
229  return TIMEOUT; }
230 
232  virtual ResultType InternalRead( char *, unsigned int, unsigned int &) {
233  return TIMEOUT; }
234 
236  virtual ResultType InternalSendBreak( void ) { return SUCCESS; }
237 
239  virtual ResultType InternalPurgeBuffers( void ) { return SUCCESS; }
240 
242  virtual void InternalSleep( unsigned int ) {};
243 
245  virtual void PrintSelf( std::ostream& os, itk::Indent indent ) const;
246 
247 private:
248 
249  // Communication Parameters
250 
252  PortNumberType m_PortNumber;
253 
255  BaudRateType m_BaudRate;
256 
258  DataBitsType m_DataBits;
259 
261  ParityType m_Parity;
262 
264  StopBitsType m_StopBits;
265 
267  HandshakeType m_HardwareHandshake;
268 
270  unsigned int m_SleepPeriod;
271 
273  char *m_InputData;
274 
276  const char *m_OutputData;
277 
279  unsigned int m_BytesToWrite;
280 
282  unsigned int m_BytesToRead;
283 
285  unsigned int m_BytesRead;
286 
288  typedef std::map<int, InputType> IntegerInputMapType;
289 
291  std::string m_CaptureFileName;
292 
294  std::ofstream m_CaptureFileStream;
295 
297  unsigned int m_CaptureMessageNumber;
298 
300  bool m_Capture;
301 
304 
306  unsigned int m_RTSSignal;
307 
309  itk::StdStreamLogOutput::Pointer m_CaptureFileOutput;
310 
312  ResultType m_ReturnValue;
313 
315  IntegerInputMapType m_ResultInputMap;
316 
317  // List of States
318  igstkDeclareStateMacro( Idle );
319  igstkDeclareStateMacro( AttemptingToOpenPort );
320  igstkDeclareStateMacro( PortOpen );
321  igstkDeclareStateMacro( AttemptingToUpdateParameters );
322  igstkDeclareStateMacro( ReadyForCommunication );
323  igstkDeclareStateMacro( AttemptingToClosePort );
324  igstkDeclareStateMacro( AttemptingToRead );
325  igstkDeclareStateMacro( AttemptingToWrite );
326  igstkDeclareStateMacro( AttemptingToSendBreak );
327  igstkDeclareStateMacro( AttemptingToPurgeBuffers );
328  igstkDeclareStateMacro( AttemptingToSetRTS );
329  igstkDeclareStateMacro( Sleep );
330 
331  // List of Inputs
332  igstkDeclareInputMacro( Success );
333  igstkDeclareInputMacro( Failure );
334  igstkDeclareInputMacro( Timeout );
335  igstkDeclareInputMacro( OpenPort );
336  igstkDeclareInputMacro( ClosePort );
337  igstkDeclareInputMacro( UpdateParameters );
338  igstkDeclareInputMacro( Read );
339  igstkDeclareInputMacro( Write );
340  igstkDeclareInputMacro( SendBreak );
341  igstkDeclareInputMacro( PurgeBuffers );
342  igstkDeclareInputMacro( Sleep );
343  igstkDeclareInputMacro( SetRTS );
344 
346  void OpenPortSuccessProcessing( void );
347 
349  void OpenPortFailureProcessing( void );
350 
352  void ClosePortSuccessProcessing( void );
353 
355  void ClosePortFailureProcessing( void );
356 
358  void SuccessProcessing( void );
359 
361  void FailureProcessing( void );
362 
364  void TimeoutProcessing( void );
365 
367  void NoProcessing();
368 
370  void AttemptToOpenPortProcessing( void );
371 
373  void AttemptToUpdateParametersProcessing( void );
374 
376  void AttemptToClosePortProcessing( void );
377 
379  void AttemptToSetRTSProcessing( void );
380 
382  void AttemptToWriteProcessing( void );
383 
385  void AttemptToReadProcessing( void );
386 
388  void AttemptToSendBreakProcessing( void );
389 
391  void AttemptToPurgeBuffersProcessing( void );
392 
394  void SleepProcessing( void );
395 
397  const InputType &MapResultToInput( int condition );
398 };
399 
400 } // end namespace igstk
401 
402 #endif // __igstkSerialCommunication_h
ResultType Read(char *data, unsigned int numberOfBytes, unsigned int &bytesRead)
Read method receives the string via the communication link.
virtual ResultType InternalSendBreak(void)
Send a break to the across the serial port.
#define igstkStateMachineMacro()
This is the StateMachine Macro to be used with non-templated classes.
Definition: igstkMacros.h:216
#define igstkGetMacro(name, type)
Get built-in type.
Definition: igstkMacros.h:105
virtual void PrintSelf(std::ostream &os, itk::Indent indent) const
Print object information.
virtual ResultType InternalSetRTS(unsigned int)
Closes serial port.
virtual void InternalSleep(unsigned int)
Sleep for the period of time specified, in milliseconds.
::itk::SmartPointer< Self > Pointer
Definition: igstkLogger.h:42
virtual ResultType InternalClosePort(void)
Closes serial port.
static Pointer New(void)
Customized New method that will return the implementation of SerialCommunication that is appropriate ...
virtual ResultType InternalPurgeBuffers(void)
Purge the buffers.
ResultType Write(const char *message, unsigned int numberOfBytes)
Write method sends the string via the communication link.
ResultType UpdateParameters(void)
Update the communication parameters, in case you need to change the baud rate, handshaking, timeout, etc.
virtual ResultType InternalRead(char *, unsigned int, unsigned int &)
read the data from the serial port.
defines standard system-wide macros, constants, and other common parameters in the IGSTK Library...
BaudRateType
Available baud rates.
ResultType PurgeBuffers(void)
Purge the contents of the buffers.
virtual ResultType InternalOpenPort(void)
Opens serial port for communication;.
const char * GetCaptureFileName() const
Get the filename into which the data stream is recorded.
ResultType CloseCommunication(void)
The method CloseCommunication closes the communication.
::itk::SmartPointer< Self > Pointer
Definition: igstkObject.h:50
Class Communication is the base class for communication between the tracker class and the hardware tr...
StopBitsType
Available stop bits settings.
Communication::ResultType ResultType
PortNumberType
Available port numbers.
ResultType OpenCommunication(void)
The method OpenCommunication sets up communication as per the data provided.
#define igstkLoggerMacro()
Macro that defines all the standard elements related to the Logger.
Definition: igstkMacros.h:143
void Sleep(unsigned int milliseconds)
Sleep for the specified number of milliseconds.
virtual ResultType InternalWrite(const char *, unsigned int)
write the data to the serial port.
This class implements communication over a serial port via RS-232.
DataBitsType
Available data bits settings.
#define igstkSetMacro(name, type)
Set built-in type.
Definition: igstkMacros.h:93
HandshakeType
Available hardware handshaking settings.
igstkStandardClassBasicTraitsMacro(SerialCommunication, Communication)
Standard traits of a basic class.
void SetCaptureFileName(const char *filename)
Set the name of the file into which the data stream is recorded.
ResultType SetRTS(unsigned int signal)
Set the RTS value 0 : Clear the RTS (request-to-send) signal 1 : Sends the RTS signal.
ParityType
Available parity settings.
ResultType SendBreak(void)
Send a break in the serial communication, which by definition is a series of zeroes that lasts for a ...
virtual ResultType InternalUpdateParameters(void)
Set communication parameters on the open port.