IGSTK
Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | List of all members
igstk::SerialCommunication Class Reference

This class implements communication over a serial port via RS-232. More...

#include <igstkSerialCommunication.h>

Inheritance diagram for igstk::SerialCommunication:
Inheritance graph
[legend]
Collaboration diagram for igstk::SerialCommunication:
Collaboration graph
[legend]

Public Types

enum  PortNumberType {
  PortNumber0 = 0, PortNumber1 = 1, PortNumber2 = 2, PortNumber3 = 3,
  PortNumber4 = 4, PortNumber5 = 5, PortNumber6 = 6, PortNumber7 = 7
}
 Available port numbers. More...
 
enum  BaudRateType {
  BaudRate9600 = 9600, BaudRate19200 = 19200, BaudRate38400 = 38400, BaudRate57600 = 57600,
  BaudRate115200 = 115200
}
 Available baud rates. More...
 
enum  DataBitsType { DataBits7 = 7, DataBits8 = 8 }
 Available data bits settings. More...
 
enum  ParityType { NoParity = 'N', OddParity = 'O', EvenParity = 'E' }
 Available parity settings. More...
 
enum  StopBitsType { StopBits1 = 1, StopBits2 = 2 }
 Available stop bits settings. More...
 
enum  HandshakeType { HandshakeOff = 0, HandshakeOn = 1 }
 Available hardware handshaking settings. More...
 
typedef Communication::ResultType ResultType
 
- Public Types inherited from igstk::Object
typedef Object Self
 General Typedefs. More...
 
typedef ::itk::Object Superclass
 
typedef ::itk::SmartPointer< SelfPointer
 
typedef ::itk::SmartPointer
< const Self
ConstPointer
 
typedef igstk::Logger LoggerType
 

Public Member Functions

 igstkStandardClassBasicTraitsMacro (SerialCommunication, Communication)
 Standard traits of a basic class. More...
 
virtual void SetPortNumber (PortNumberType _arg)
 Specify which serial port to use. More...
 
virtual PortNumberType GetPortNumber ()
 Get the serial port, where the ports are numbered 0 through 3. More...
 
virtual void SetBaudRate (BaudRateType _arg)
 Set the baud rate to use. More...
 
virtual BaudRateType GetBaudRate ()
 Get the baud rate. More...
 
virtual void SetDataBits (DataBitsType _arg)
 Set the number of bits per character. More...
 
virtual DataBitsType GetDataBits ()
 Get the number of bits per character. More...
 
virtual void SetParity (ParityType _arg)
 Set the parity. More...
 
virtual ParityType GetParity ()
 Get the parity. More...
 
virtual void SetStopBits (StopBitsType _arg)
 Set the number of stop bits. More...
 
virtual StopBitsType GetStopBits ()
 Get the number of stop bits. More...
 
virtual void SetHardwareHandshake (HandshakeType _arg)
 Set whether to use hardware handshaking. More...
 
virtual HandshakeType GetHardwareHandshake ()
 Get whether hardware handshaking is enabled. More...
 
void SetCaptureFileName (const char *filename)
 Set the name of the file into which the data stream is recorded. More...
 
const char * GetCaptureFileName () const
 Get the filename into which the data stream is recorded. More...
 
virtual void SetCapture (bool _arg)
 Set whether to record the data. More...
 
virtual bool GetCapture ()
 Get whether the data is being recorded. More...
 
ResultType UpdateParameters (void)
 Update the communication parameters, in case you need to change the baud rate, handshaking, timeout, etc. More...
 
ResultType OpenCommunication (void)
 The method OpenCommunication sets up communication as per the data provided. More...
 
ResultType CloseCommunication (void)
 The method CloseCommunication closes the communication. More...
 
ResultType SetRTS (unsigned int signal)
 Set the RTS value 0 : Clear the RTS (request-to-send) signal 1 : Sends the RTS signal. More...
 
ResultType Write (const char *message, unsigned int numberOfBytes)
 Write method sends the string via the communication link. More...
 
ResultType Read (char *data, unsigned int numberOfBytes, unsigned int &bytesRead)
 Read method receives the string via the communication link. More...
 
ResultType SendBreak (void)
 Send a break in the serial communication, which by definition is a series of zeroes that lasts for a 0.3 second duration. More...
 
ResultType PurgeBuffers (void)
 Purge the contents of the buffers. More...
 
void Sleep (unsigned int milliseconds)
 Sleep for the specified number of milliseconds. More...
 
void ExportStateMachineDescription (OutputStreamType &ostr, bool skipLoops=false) const
 Declarations related to the State Machine. More...
 
void SetLogger (LoggerType *logger)
 
- Public Member Functions inherited from igstk::Communication
virtual void SetTimeoutPeriod (unsigned int _arg)
 The method SetTimeoutPeriod sets the amount of time, in milliseconds to wait on a reply from the device before generating a timeout event. More...
 
virtual unsigned int GetTimeoutPeriod ()
 
virtual void SetReadTerminationCharacter (char _arg)
 The method SetReadTerminationCharacter sets a special character that the device uses to mark the end of a reply (defaults to end-of-file character, ascii 255). More...
 
virtual char GetReadTerminationCharacter ()
 
virtual void SetUseReadTerminationCharacter (bool _arg)
 The method SetUseReadTerminationCharacter sets whether to use the termination character, or not use a termination character. More...
 
virtual bool GetUseReadTerminationCharacter ()
 
- Public Member Functions inherited from igstk::Object
virtual const char * GetNameOfClass () const
 
void SetLogger (LoggerType *logger)
 Connect the Logger for this class. More...
 
void RemoveObserver (unsigned long tag) const
 

Static Public Member Functions

static Pointer New (void)
 Customized New method that will return the implementation of SerialCommunication that is appropriate for this platform. More...
 
- Static Public Member Functions inherited from igstk::Object
static Pointer New (void)
 

Protected Member Functions

LoggerTypeGetLogger () const
 Declarations related to the Logger. More...
 
 SerialCommunication ()
 
 ~SerialCommunication ()
 
virtual ResultType InternalOpenPort (void)
 Opens serial port for communication;. More...
 
virtual ResultType InternalUpdateParameters (void)
 Set communication parameters on the open port. More...
 
virtual ResultType InternalClosePort (void)
 Closes serial port. More...
 
virtual ResultType InternalSetRTS (unsigned int)
 Closes serial port. More...
 
virtual ResultType InternalWrite (const char *, unsigned int)
 write the data to the serial port. More...
 
virtual ResultType InternalRead (char *, unsigned int, unsigned int &)
 read the data from the serial port. More...
 
virtual ResultType InternalSendBreak (void)
 Send a break to the across the serial port. More...
 
virtual ResultType InternalPurgeBuffers (void)
 Purge the buffers. More...
 
virtual void InternalSleep (unsigned int)
 Sleep for the period of time specified, in milliseconds. More...
 
virtual void PrintSelf (std::ostream &os, itk::Indent indent) const
 Print object information. More...
 
- Protected Member Functions inherited from igstk::Communication
 Communication (void)
 Constructor is protected in order to enforce the use of the New() operator. More...
 
virtual ~Communication (void)
 
- Protected Member Functions inherited from igstk::Object
LoggerTypeGetLogger () const
 
 Object (void)
 Constructor is protected in order to enforce the use of the New() operator. More...
 
virtual ~Object (void)
 
void RegisterObservedObject (const ::igstk::Object *object, unsigned long tag)
 Register observed objects in an internal array so that they can be disconnected upon destruction. More...
 
void RemoveFromObservedObjects ()
 Remove observers that this object may have connected to other objects. More...
 

Detailed Description

This class implements communication over a serial port via RS-232.

This class provides a common interface for the interactions with a serial port independently of the platform. The actual interactions are implemented on derived classes that are platform specific. This class is not intended for being instantiated directly, instead, the derived classes should be used.

igstkSerialCommunication.png

"Serial Communication State Machine Diagram"

"Serial Communication State Machine Diagram"

Definition at line 63 of file igstkSerialCommunication.h.

Member Typedef Documentation

typedef Communication::ResultType igstk::SerialCommunication::ResultType

Definition at line 101 of file igstkSerialCommunication.h.

Member Enumeration Documentation

Available port numbers.

Enumerator
PortNumber0 
PortNumber1 
PortNumber2 
PortNumber3 
PortNumber4 
PortNumber5 
PortNumber6 
PortNumber7 

Definition at line 68 of file igstkSerialCommunication.h.

Available baud rates.

Enumerator
BaudRate9600 
BaudRate19200 
BaudRate38400 
BaudRate57600 
BaudRate115200 

Definition at line 78 of file igstkSerialCommunication.h.

Available data bits settings.

Enumerator
DataBits7 
DataBits8 

Definition at line 85 of file igstkSerialCommunication.h.

Available parity settings.

Enumerator
NoParity 
OddParity 
EvenParity 

Definition at line 89 of file igstkSerialCommunication.h.

Available stop bits settings.

Enumerator
StopBits1 
StopBits2 

Definition at line 94 of file igstkSerialCommunication.h.

Available hardware handshaking settings.

Enumerator
HandshakeOff 
HandshakeOn 

Definition at line 98 of file igstkSerialCommunication.h.

Constructor & Destructor Documentation

igstk::SerialCommunication::SerialCommunication ( )
protected
igstk::SerialCommunication::~SerialCommunication ( )
protected

Member Function Documentation

igstk::SerialCommunication::igstkStandardClassBasicTraitsMacro ( SerialCommunication  ,
Communication   
)

Standard traits of a basic class.

static Pointer igstk::SerialCommunication::New ( void  )
static

Customized New method that will return the implementation of SerialCommunication that is appropriate for this platform.

See Also
SerialCommunicationForWindows
SerialCommunicationForPosix
virtual void igstk::SerialCommunication::SetPortNumber ( PortNumberType  _arg)
virtual

Specify which serial port to use.

If communication is open, this has no effect until communication is closed and reopened.

virtual PortNumberType igstk::SerialCommunication::GetPortNumber ( )
virtual

Get the serial port, where the ports are numbered 0 through 3.

virtual void igstk::SerialCommunication::SetBaudRate ( BaudRateType  _arg)
virtual

Set the baud rate to use.

Baud rates of 57600 or higher should not be used unless some sort of error checking is in place.

virtual BaudRateType igstk::SerialCommunication::GetBaudRate ( )
virtual

Get the baud rate.

virtual void igstk::SerialCommunication::SetDataBits ( DataBitsType  _arg)
virtual

Set the number of bits per character.

This should usually be set to 8, since 7 bits is only valid for pure ASCII data.

virtual DataBitsType igstk::SerialCommunication::GetDataBits ( )
virtual

Get the number of bits per character.

virtual void igstk::SerialCommunication::SetParity ( ParityType  _arg)
virtual

Set the parity.

The default is no parity.

virtual ParityType igstk::SerialCommunication::GetParity ( )
virtual

Get the parity.

virtual void igstk::SerialCommunication::SetStopBits ( StopBitsType  _arg)
virtual

Set the number of stop bits.

The default is one stop bit.

virtual StopBitsType igstk::SerialCommunication::GetStopBits ( )
virtual

Get the number of stop bits.

virtual void igstk::SerialCommunication::SetHardwareHandshake ( HandshakeType  _arg)
virtual

Set whether to use hardware handshaking.

virtual HandshakeType igstk::SerialCommunication::GetHardwareHandshake ( )
virtual

Get whether hardware handshaking is enabled.

void igstk::SerialCommunication::SetCaptureFileName ( const char *  filename)

Set the name of the file into which the data stream is recorded.

const char* igstk::SerialCommunication::GetCaptureFileName ( ) const

Get the filename into which the data stream is recorded.

virtual void igstk::SerialCommunication::SetCapture ( bool  _arg)
virtual

Set whether to record the data.

virtual bool igstk::SerialCommunication::GetCapture ( )
virtual

Get whether the data is being recorded.

ResultType igstk::SerialCommunication::UpdateParameters ( void  )

Update the communication parameters, in case you need to change the baud rate, handshaking, timeout, etc.

after opening the port

ResultType igstk::SerialCommunication::OpenCommunication ( void  )

The method OpenCommunication sets up communication as per the data provided.

ResultType igstk::SerialCommunication::CloseCommunication ( void  )
virtual

The method CloseCommunication closes the communication.

Reimplemented from igstk::Communication.

ResultType igstk::SerialCommunication::SetRTS ( unsigned int  signal)

Set the RTS value 0 : Clear the RTS (request-to-send) signal 1 : Sends the RTS signal.

ResultType igstk::SerialCommunication::Write ( const char *  message,
unsigned int  numberOfBytes 
)
virtual

Write method sends the string via the communication link.

Reimplemented from igstk::Communication.

ResultType igstk::SerialCommunication::Read ( char *  data,
unsigned int  numberOfBytes,
unsigned int &  bytesRead 
)
virtual

Read method receives the string via the communication link.

The data will always be null-terminated, so ensure that 'data' is at least numberOfBytes+1 in size.

Reimplemented from igstk::Communication.

ResultType igstk::SerialCommunication::SendBreak ( void  )

Send a break in the serial communication, which by definition is a series of zeroes that lasts for a 0.3 second duration.

Some devices interpret this as a "reset" signal because the device is guaranteed to see it even if the baud rate, parity, or data bits are not matched between the host and the device.

ResultType igstk::SerialCommunication::PurgeBuffers ( void  )

Purge the contents of the buffers.

This is used if the device connected to the serial port has just been reset after an error, and the contents of the serial port buffers has to be thrown out before communication can continue.

void igstk::SerialCommunication::Sleep ( unsigned int  milliseconds)

Sleep for the specified number of milliseconds.

This is useful after a reset of a device on the other end of the serial port, if the device is known to take a certain amount of time to initialize.

void igstk::SerialCommunication::ExportStateMachineDescription ( OutputStreamType &  ostr,
bool  skipLoops = false 
) const

Declarations related to the State Machine.

LoggerType* igstk::SerialCommunication::GetLogger ( ) const
protected

Declarations related to the Logger.

void igstk::SerialCommunication::SetLogger ( LoggerType logger)
virtual ResultType igstk::SerialCommunication::InternalOpenPort ( void  )
inlineprotectedvirtual

Opens serial port for communication;.

Reimplemented in igstk::SerialCommunicationForWindows, igstk::SerialCommunicationSimulator, and igstk::SerialCommunicationForPosix.

Definition at line 216 of file igstkSerialCommunication.h.

virtual ResultType igstk::SerialCommunication::InternalUpdateParameters ( void  )
inlineprotectedvirtual

Set communication parameters on the open port.

Reimplemented in igstk::SerialCommunicationForWindows, igstk::SerialCommunicationSimulator, and igstk::SerialCommunicationForPosix.

Definition at line 219 of file igstkSerialCommunication.h.

virtual ResultType igstk::SerialCommunication::InternalClosePort ( void  )
inlineprotectedvirtual
virtual ResultType igstk::SerialCommunication::InternalSetRTS ( unsigned int  )
inlineprotectedvirtual

Closes serial port.

Reimplemented in igstk::SerialCommunicationForWindows, and igstk::SerialCommunicationForPosix.

Definition at line 225 of file igstkSerialCommunication.h.

virtual ResultType igstk::SerialCommunication::InternalWrite ( const char *  ,
unsigned int   
)
inlineprotectedvirtual

write the data to the serial port.

Reimplemented in igstk::SerialCommunicationForWindows, igstk::SerialCommunicationSimulator, and igstk::SerialCommunicationForPosix.

Definition at line 228 of file igstkSerialCommunication.h.

virtual ResultType igstk::SerialCommunication::InternalRead ( char *  ,
unsigned int  ,
unsigned int &   
)
inlineprotectedvirtual

read the data from the serial port.

Reimplemented in igstk::SerialCommunicationForWindows, igstk::SerialCommunicationSimulator, and igstk::SerialCommunicationForPosix.

Definition at line 232 of file igstkSerialCommunication.h.

virtual ResultType igstk::SerialCommunication::InternalSendBreak ( void  )
inlineprotectedvirtual

Send a break to the across the serial port.

Reimplemented in igstk::SerialCommunicationForWindows, igstk::SerialCommunicationSimulator, and igstk::SerialCommunicationForPosix.

Definition at line 236 of file igstkSerialCommunication.h.

virtual ResultType igstk::SerialCommunication::InternalPurgeBuffers ( void  )
inlineprotectedvirtual
virtual void igstk::SerialCommunication::InternalSleep ( unsigned int  )
inlineprotectedvirtual

Sleep for the period of time specified, in milliseconds.

Reimplemented in igstk::SerialCommunicationSimulator, and igstk::SerialCommunicationForPosix.

Definition at line 242 of file igstkSerialCommunication.h.

virtual void igstk::SerialCommunication::PrintSelf ( std::ostream &  os,
itk::Indent  indent 
) const
protectedvirtual

The documentation for this class was generated from the following file: