Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitk::SerialCommunication Class Reference

serial communication interface More...

#include <mitkSerialCommunication.h>

Inheritance diagram for mitk::SerialCommunication:
Collaboration diagram for mitk::SerialCommunication:

Public Types

enum  PortNumber {
  COM1, COM2, COM3, COM4,
  COM5, COM6, COM7, COM8,
  COM9, COM10, COM11, COM12,
  COM13
}
 
enum  BaudRate {
  BaudRate9600, BaudRate14400, BaudRate19200, BaudRate38400,
  BaudRate57600, BaudRate115200, BaudRate230400, BaudRate460800,
  BaudRate500000, BaudRate576000, BaudRate921600, BaudRate1000000,
  BaudRate1152000, BaudRate1500000, BaudRate2000000, BaudRate2500000,
  BaudRate3000000, BaudRate3500000, BaudRate4000000
}
 
enum  DataBits { DataBits8, DataBits7 }
 
enum  Parity { None, Odd, Even }
 
enum  StopBits { StopBits1, StopBits2 }
 
enum  HardwareHandshake { HardwareHandshakeOn, HardwareHandshakeOff }
 

Public Member Functions

 mitkClassMacroItkParent (SerialCommunication, itk::Object)
 
Pointer Clone () const
 
int OpenConnection ()
 Opens connection to the COM port with port number m_PortNumber or the device name m_DeviceName and all port settings. More...
 
void CloseConnection ()
 Closes the connection. More...
 
int Receive (std::string &answer, unsigned int numberOfBytes, const char *eol=nullptr)
 Read numberOfBytes characters from the serial interface. More...
 
int Send (const std::string &input, bool block=false)
 Send the string input. More...
 
void SendBreak (unsigned int ms=400)
 Send the break signal for ms milliseconds. More...
 
void ClearReceiveBuffer ()
 erase the receive buffer of the serial interface More...
 
void ClearSendBuffer ()
 erase the send buffer of the serial interface More...
 
virtual PortNumber GetPortNumber () const
 Get the port number of the serial interface. More...
 
virtual void SetPortNumber (PortNumber _arg)
 Set the port number of the serial interface. More...
 
virtual const char * GetDeviceName () const
 Get the device name. More...
 
virtual void SetDeviceName (const char *_arg)
 Set the device name. More...
 
virtual BaudRate GetBaudRate () const
 Get the baud rate of the serial interface. More...
 
virtual void SetBaudRate (BaudRate _arg)
 Set the baud rate of the serial interface. More...
 
virtual DataBits GetDataBits () const
 Get the number of data bits of the serial interface. More...
 
virtual void SetDataBits (DataBits _arg)
 Set the number of data bits of the serial interface. More...
 
virtual Parity GetParity () const
 Get the parity mode of the serial interface. More...
 
virtual void SetParity (Parity _arg)
 Set the parity mode of the serial interface. More...
 
virtual StopBits GetStopBits () const
 Get number of stop bits of the serial interface. More...
 
virtual void SetStopBits (StopBits _arg)
 Set number of stop bits of the serial interface. More...
 
virtual HardwareHandshake GetHardwareHandshake () const
 returns true if hardware handshake should is used More...
 
virtual void SetHardwareHandshake (HardwareHandshake _arg)
 Set if hardware handshake should be used. More...
 
virtual unsigned int GetSendTimeout () const
 returns the send timeout in milliseconds More...
 
virtual void SetSendTimeout (unsigned int _arg)
 set the send timeout in milliseconds More...
 
virtual unsigned int GetReceiveTimeout () const
 returns the receive timeout in milliseconds More...
 
virtual void SetReceiveTimeout (unsigned int _arg)
 set the send timeout in milliseconds More...
 

Static Public Member Functions

static Pointer New ()
 

Protected Member Functions

 SerialCommunication ()
 
virtual ~SerialCommunication ()
 
int ApplyConfiguration ()
 configures the serial interface with all parameters More...
 
int ApplyConfigurationUnix ()
 Applies the configuration for Linux. More...
 

Protected Attributes

std::string m_DeviceName
 device name that is used to connect to the serial interface (will be used if != "") More...
 
PortNumber m_PortNumber
 port number of the device More...
 
BaudRate m_BaudRate
 baud rate of the serial interface connection More...
 
DataBits m_DataBits
 number of data bits per symbol More...
 
Parity m_Parity
 parity mode More...
 
StopBits m_StopBits
 number of stop bits per symbol More...
 
HardwareHandshake m_HardwareHandshake
 whether to use hardware handshake for the connection More...
 
unsigned int m_ReceiveTimeout
 timeout for receiving data from the serial interface in milliseconds More...
 
unsigned int m_SendTimeout
 timeout for sending data to the serial interface in milliseconds More...
 
bool m_Connected
 is set to true if a connection currently established More...
 
int m_FileDescriptor
 

Detailed Description

serial communication interface

Documentation This class allows to send and receive data over a serial communication interface (COM Port). Define the serial interface that should be used either with SetPortNumber() or SetDeviceName() Next, define communication parameters: baud rate, number of data bits, number of stop bits, parity mode, usage of hardware handshake and timeout values (in ms). Use OpenConnection() to establish a connection on the serial interface with the selected parameters. While the connection is established, changes to the parameters will not take effect. You have to close the connection using CloseConnection() and then reopen it with the new parameters with OpenConnection().

Definition at line 47 of file mitkSerialCommunication.h.

Member Enumeration Documentation

Enumerator
BaudRate9600 
BaudRate14400 
BaudRate19200 
BaudRate38400 
BaudRate57600 
BaudRate115200 
BaudRate230400 
BaudRate460800 
BaudRate500000 
BaudRate576000 
BaudRate921600 
BaudRate1000000 
BaudRate1152000 
BaudRate1500000 
BaudRate2000000 
BaudRate2500000 
BaudRate3000000 
BaudRate3500000 
BaudRate4000000 

Definition at line 71 of file mitkSerialCommunication.h.

Enumerator
DataBits8 
DataBits7 

Definition at line 95 of file mitkSerialCommunication.h.

Enumerator
HardwareHandshakeOn 
HardwareHandshakeOff 

Definition at line 114 of file mitkSerialCommunication.h.

Enumerator
None 
Odd 
Even 

Definition at line 101 of file mitkSerialCommunication.h.

Enumerator
COM1 
COM2 
COM3 
COM4 
COM5 
COM6 
COM7 
COM8 
COM9 
COM10 
COM11 
COM12 
COM13 

Definition at line 54 of file mitkSerialCommunication.h.

Enumerator
StopBits1 
StopBits2 

Definition at line 108 of file mitkSerialCommunication.h.

Constructor & Destructor Documentation

mitk::SerialCommunication::SerialCommunication ( )
protected

Definition at line 38 of file mitkSerialCommunication.cpp.

References INVALID_HANDLE_VALUE, and m_FileDescriptor.

mitk::SerialCommunication::~SerialCommunication ( )
protectedvirtual

Definition at line 51 of file mitkSerialCommunication.cpp.

Member Function Documentation

int mitk::SerialCommunication::ApplyConfiguration ( )
protected

configures the serial interface with all parameters

This automatically reroutes the call to the hardware specific function

Definition at line 261 of file mitkSerialCommunication.cpp.

int mitk::SerialCommunication::ApplyConfigurationUnix ( )
protected

Applies the configuration for Linux.

The Code for Baud rates is highly platform specific and divided into separate subroutines for readability.

Definition at line 326 of file mitkSerialCommunication.cpp.

References ERROR_VALUE, INVALID_HANDLE_VALUE, MITK_WARN, and OK.

void mitk::SerialCommunication::ClearReceiveBuffer ( )

erase the receive buffer of the serial interface

Definition at line 484 of file mitkSerialCommunication.cpp.

References INVALID_HANDLE_VALUE.

void mitk::SerialCommunication::ClearSendBuffer ( )

erase the send buffer of the serial interface

Definition at line 495 of file mitkSerialCommunication.cpp.

References INVALID_HANDLE_VALUE.

Pointer mitk::SerialCommunication::Clone ( ) const
void mitk::SerialCommunication::CloseConnection ( )

Closes the connection.

Definition at line 115 of file mitkSerialCommunication.cpp.

References INVALID_HANDLE_VALUE.

virtual BaudRate mitk::SerialCommunication::GetBaudRate ( ) const
virtual

Get the baud rate of the serial interface.

virtual DataBits mitk::SerialCommunication::GetDataBits ( ) const
virtual

Get the number of data bits of the serial interface.

virtual const char* mitk::SerialCommunication::GetDeviceName ( ) const
virtual

Get the device name.

SerialCommunication can either use m_PortNumber to create serial interface device names or use m_DeviceName directly. This method allows to set an arbitrary device name that will be used to connect to the device. Common names are COM1, CNCA0, CNCB9 on windows and /dev/ttyS0 or /dev/ttyUSB0 on linux.

virtual HardwareHandshake mitk::SerialCommunication::GetHardwareHandshake ( ) const
virtual

returns true if hardware handshake should is used

virtual Parity mitk::SerialCommunication::GetParity ( ) const
virtual

Get the parity mode of the serial interface.

virtual PortNumber mitk::SerialCommunication::GetPortNumber ( ) const
virtual

Get the port number of the serial interface.

Returns the port number that will be used in the connection. The port number is only used if the m_DeviceName is empty ("").

virtual unsigned int mitk::SerialCommunication::GetReceiveTimeout ( ) const
virtual

returns the receive timeout in milliseconds

virtual unsigned int mitk::SerialCommunication::GetSendTimeout ( ) const
virtual

returns the send timeout in milliseconds

virtual StopBits mitk::SerialCommunication::GetStopBits ( ) const
virtual

Get number of stop bits of the serial interface.

mitk::SerialCommunication::mitkClassMacroItkParent ( SerialCommunication  ,
itk::Object   
)
static Pointer mitk::SerialCommunication::New ( )
static
int mitk::SerialCommunication::OpenConnection ( )

Opens connection to the COM port with port number m_PortNumber or the device name m_DeviceName and all port settings.

Definition at line 56 of file mitkSerialCommunication.cpp.

References ERROR_VALUE, INVALID_HANDLE_VALUE, and OK.

int mitk::SerialCommunication::Receive ( std::string &  answer,
unsigned int  numberOfBytes,
const char *  eol = nullptr 
)

Read numberOfBytes characters from the serial interface.

This method tries to read numberOfBytes characters from the serial interface or until an eol byte is received, whichever comes first. If The ReceiveTimeout is set to 0, the Receive() method will wait indefinetly until all characters are received or an eol character is received. If the ReceiveTimeout is set to another value, it will return after m_ReceiveTimeout milliseconds (or after all characters are read or an eol character is received).

Parameters
[out]answerString that stores the received characters. Note that this will overwrite the content of answer!
[in]numberOfBytesThe number of bytes to read. When an eol character is used this is interpretted as the maximum number of bytes to read.
[in]eolPointer to an End-of-Line character. If this is NULL (the default) then no End-of-Line character is used.

Definition at line 143 of file mitkSerialCommunication.cpp.

References ERROR_VALUE, INVALID_HANDLE_VALUE, and OK.

int mitk::SerialCommunication::Send ( const std::string &  input,
bool  block = false 
)

Send the string input.

Parameters
[in]inputThe string to send to the serial interface. The string termination character \0 is not sent.
[in]blockIf false, the this method will return immediately. If true, this method will block until all bytes have been physically transmitted over the serial interface.

Definition at line 219 of file mitkSerialCommunication.cpp.

References ERROR_VALUE, INVALID_HANDLE_VALUE, and OK.

void mitk::SerialCommunication::SendBreak ( unsigned int  ms = 400)

Send the break signal for ms milliseconds.

Definition at line 465 of file mitkSerialCommunication.cpp.

References INVALID_HANDLE_VALUE.

virtual void mitk::SerialCommunication::SetBaudRate ( BaudRate  _arg)
virtual

Set the baud rate of the serial interface.

virtual void mitk::SerialCommunication::SetDataBits ( DataBits  _arg)
virtual

Set the number of data bits of the serial interface.

virtual void mitk::SerialCommunication::SetDeviceName ( const char *  _arg)
virtual

Set the device name.

if the device name is set (!=""), OpenConnection() will try to open the serial device on that device name. Normally, the serial interfaces are named COM1-COM9 on windows and /dev/ttyS0 to /dev/ttyS9 on linux, but other names are possible too (e.g. /dev/ttyUSB0).

virtual void mitk::SerialCommunication::SetHardwareHandshake ( HardwareHandshake  _arg)
virtual

Set if hardware handshake should be used.

virtual void mitk::SerialCommunication::SetParity ( Parity  _arg)
virtual

Set the parity mode of the serial interface.

virtual void mitk::SerialCommunication::SetPortNumber ( PortNumber  _arg)
virtual

Set the port number of the serial interface.

SerialCommunication can either use PortNumber to create serial interface device names COM1 to COM9 for windows and /dev/ttyS0 to /dev/ttyS8 on linux (SetPortNumber(COM1) is mapped to /dev/ttyS0 and so on). Alternatively, use SetDeviceName() to set the device name directly (e.g. "CNCA0" for a com0com virtual com port or "/dev/ttyUSB0" for a USB to serial adapter on linux. If a device name is set (m_DeviceName != "") then OpenConnection() will try to open that device. Otherwise, it will build the device name using the port number

virtual void mitk::SerialCommunication::SetReceiveTimeout ( unsigned int  _arg)
virtual

set the send timeout in milliseconds

Specify the receive timeout in milliseconds. Setting this value to 0 will cause the Receive() method to wait until all expected characters are received.

virtual void mitk::SerialCommunication::SetSendTimeout ( unsigned int  _arg)
virtual

set the send timeout in milliseconds

Only applies to WIN32, not POSIX

virtual void mitk::SerialCommunication::SetStopBits ( StopBits  _arg)
virtual

Set number of stop bits of the serial interface.

Member Data Documentation

BaudRate mitk::SerialCommunication::m_BaudRate
protected

baud rate of the serial interface connection

Definition at line 326 of file mitkSerialCommunication.h.

bool mitk::SerialCommunication::m_Connected
protected

is set to true if a connection currently established

Definition at line 334 of file mitkSerialCommunication.h.

DataBits mitk::SerialCommunication::m_DataBits
protected

number of data bits per symbol

Definition at line 327 of file mitkSerialCommunication.h.

std::string mitk::SerialCommunication::m_DeviceName
protected

device name that is used to connect to the serial interface (will be used if != "")

Definition at line 324 of file mitkSerialCommunication.h.

int mitk::SerialCommunication::m_FileDescriptor
protected

Definition at line 342 of file mitkSerialCommunication.h.

Referenced by SerialCommunication().

HardwareHandshake mitk::SerialCommunication::m_HardwareHandshake
protected

whether to use hardware handshake for the connection

Definition at line 330 of file mitkSerialCommunication.h.

Parity mitk::SerialCommunication::m_Parity
protected

parity mode

Definition at line 328 of file mitkSerialCommunication.h.

PortNumber mitk::SerialCommunication::m_PortNumber
protected

port number of the device

Definition at line 325 of file mitkSerialCommunication.h.

unsigned int mitk::SerialCommunication::m_ReceiveTimeout
protected

timeout for receiving data from the serial interface in milliseconds

Definition at line 331 of file mitkSerialCommunication.h.

unsigned int mitk::SerialCommunication::m_SendTimeout
protected

timeout for sending data to the serial interface in milliseconds

Definition at line 332 of file mitkSerialCommunication.h.

StopBits mitk::SerialCommunication::m_StopBits
protected

number of stop bits per symbol

Definition at line 329 of file mitkSerialCommunication.h.


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