MITK-IGT
IGT Extension of MITK
Loading...
Searching...
No Matches
mitk::SerialCommunication Class Reference

serial communication interface More...

#include <mitkSerialCommunication.h>

Inheritance diagram for mitk::SerialCommunication:

Public Types

enum  PortNumber {
  COM1 = 1 , COM2 = 2 , COM3 = 3 , COM4 = 4 ,
  COM5 = 5 , COM6 = 6 , COM7 = 7 , COM8 = 8 ,
  COM9 = 9 , COM10 = 10 , COM11 = 11 , COM12 = 12 ,
  COM13 = 13
}
 
enum  BaudRate {
  BaudRate9600 = 9600 , BaudRate14400 = 14400 , BaudRate19200 = 19200 , BaudRate38400 = 38400 ,
  BaudRate57600 = 57600 , BaudRate115200 = 115200 , BaudRate230400 = 230400 , BaudRate460800 = 460800 ,
  BaudRate500000 = 500000 , BaudRate576000 = 576000 , BaudRate921600 = 921600 , BaudRate1000000 = 1000000 ,
  BaudRate1152000 = 1152000 , BaudRate1500000 = 1500000 , BaudRate2000000 = 2000000 , BaudRate2500000 = 2500000 ,
  BaudRate3000000 = 3000000 , BaudRate3500000 = 3500000 , BaudRate4000000 = 4000000
}
 
enum  DataBits { DataBits8 = 8 , DataBits7 = 7 }
 
enum  Parity { None = 'N' , Odd = 'O' , Even = 'E' }
 
enum  StopBits { StopBits1 = 1 , StopBits2 = 2 }
 
enum  HardwareHandshake { HardwareHandshakeOn = 1 , HardwareHandshakeOff = 0 }
 

Public Member Functions

 mitkClassMacroItkParent (SerialCommunication, itk::Object)
 
 itkFactorylessNewMacro (Self)
 
 itkCloneMacro (Self)
 
bool IsConnected ()
 Returns m_Connected.
 
int OpenConnection ()
 Opens connection to the COM port with port number m_PortNumber or the device name m_DeviceName and all port settings.
 
void CloseConnection ()
 Closes the connection.
 
int Receive (std::string &answer, unsigned int numberOfBytes, const char *eol=nullptr)
 Read numberOfBytes characters from the serial interface.
 
int Send (const std::string &input, bool block=false)
 Send the string input.
 
void SendBreak (unsigned int ms=400)
 Send the break signal for ms milliseconds.
 
void ClearReceiveBuffer ()
 erase the receive buffer of the serial interface
 
void ClearSendBuffer ()
 erase the send buffer of the serial interface
 
 itkGetConstMacro (PortNumber, PortNumber)
 Get the port number of the serial interface.
 
 itkSetMacro (PortNumber, PortNumber)
 Set the port number of the serial interface.
 
 itkGetStringMacro (DeviceName)
 Get the device name.
 
 itkSetStringMacro (DeviceName)
 Set the device name.
 
 itkGetConstMacro (BaudRate, BaudRate)
 Get the baud rate of the serial interface.
 
 itkSetMacro (BaudRate, BaudRate)
 Set the baud rate of the serial interface.
 
 itkGetConstMacro (DataBits, DataBits)
 Get the number of data bits of the serial interface.
 
 itkSetMacro (DataBits, DataBits)
 Set the number of data bits of the serial interface.
 
 itkGetConstMacro (Parity, Parity)
 Get the parity mode of the serial interface.
 
 itkSetMacro (Parity, Parity)
 Set the parity mode of the serial interface.
 
 itkGetConstMacro (StopBits, StopBits)
 Get number of stop bits of the serial interface.
 
 itkSetMacro (StopBits, StopBits)
 Set number of stop bits of the serial interface.
 
 itkGetConstMacro (HardwareHandshake, HardwareHandshake)
 returns true if hardware handshake should is used
 
 itkSetMacro (HardwareHandshake, HardwareHandshake)
 Set if hardware handshake should be used.
 
 itkGetConstMacro (SendTimeout, unsigned int)
 returns the send timeout in milliseconds
 
 itkSetMacro (SendTimeout, unsigned int)
 set the send timeout in milliseconds
 
 itkGetConstMacro (ReceiveTimeout, unsigned int)
 returns the receive timeout in milliseconds
 
 itkSetMacro (ReceiveTimeout, unsigned int)
 set the send timeout in milliseconds
 

Protected Member Functions

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

Protected Attributes

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

Member Enumeration Documentation

◆ BaudRate

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

Definition at line 67 of file mitkSerialCommunication.h.

◆ DataBits

Enumerator
DataBits8 
DataBits7 

Definition at line 91 of file mitkSerialCommunication.h.

◆ HardwareHandshake

Enumerator
HardwareHandshakeOn 
HardwareHandshakeOff 

Definition at line 110 of file mitkSerialCommunication.h.

◆ Parity

Enumerator
None 
Odd 
Even 

Definition at line 97 of file mitkSerialCommunication.h.

◆ PortNumber

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

Definition at line 50 of file mitkSerialCommunication.h.

◆ StopBits

Enumerator
StopBits1 
StopBits2 

Definition at line 104 of file mitkSerialCommunication.h.

Constructor & Destructor Documentation

◆ SerialCommunication()

mitk::SerialCommunication::SerialCommunication ( )
protected

Definition at line 34 of file mitkSerialCommunication.cpp.

◆ ~SerialCommunication()

mitk::SerialCommunication::~SerialCommunication ( )
overrideprotected

Definition at line 47 of file mitkSerialCommunication.cpp.

Member Function Documentation

◆ ApplyConfiguration()

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 264 of file mitkSerialCommunication.cpp.

◆ ApplyConfigurationUnix()

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 329 of file mitkSerialCommunication.cpp.

◆ ClearReceiveBuffer()

void mitk::SerialCommunication::ClearReceiveBuffer ( )

erase the receive buffer of the serial interface

Definition at line 490 of file mitkSerialCommunication.cpp.

◆ ClearSendBuffer()

void mitk::SerialCommunication::ClearSendBuffer ( )

erase the send buffer of the serial interface

Definition at line 501 of file mitkSerialCommunication.cpp.

◆ CloseConnection()

void mitk::SerialCommunication::CloseConnection ( )

Closes the connection.

Definition at line 116 of file mitkSerialCommunication.cpp.

◆ IsConnected()

bool mitk::SerialCommunication::IsConnected ( )

Returns m_Connected.

Definition at line 52 of file mitkSerialCommunication.cpp.

◆ itkCloneMacro()

mitk::SerialCommunication::itkCloneMacro ( Self )

◆ itkFactorylessNewMacro()

mitk::SerialCommunication::itkFactorylessNewMacro ( Self )

◆ itkGetConstMacro() [1/8]

mitk::SerialCommunication::itkGetConstMacro ( BaudRate ,
BaudRate  )

Get the baud rate of the serial interface.

◆ itkGetConstMacro() [2/8]

mitk::SerialCommunication::itkGetConstMacro ( DataBits ,
DataBits  )

Get the number of data bits of the serial interface.

◆ itkGetConstMacro() [3/8]

mitk::SerialCommunication::itkGetConstMacro ( HardwareHandshake ,
HardwareHandshake  )

returns true if hardware handshake should is used

◆ itkGetConstMacro() [4/8]

mitk::SerialCommunication::itkGetConstMacro ( Parity ,
Parity  )

Get the parity mode of the serial interface.

◆ itkGetConstMacro() [5/8]

mitk::SerialCommunication::itkGetConstMacro ( PortNumber ,
PortNumber  )

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 ("").

◆ itkGetConstMacro() [6/8]

mitk::SerialCommunication::itkGetConstMacro ( ReceiveTimeout ,
unsigned int  )

returns the receive timeout in milliseconds

◆ itkGetConstMacro() [7/8]

mitk::SerialCommunication::itkGetConstMacro ( SendTimeout ,
unsigned int  )

returns the send timeout in milliseconds

◆ itkGetConstMacro() [8/8]

mitk::SerialCommunication::itkGetConstMacro ( StopBits ,
StopBits  )

Get number of stop bits of the serial interface.

◆ itkGetStringMacro()

mitk::SerialCommunication::itkGetStringMacro ( DeviceName )

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.

◆ itkSetMacro() [1/8]

mitk::SerialCommunication::itkSetMacro ( BaudRate ,
BaudRate  )

Set the baud rate of the serial interface.

◆ itkSetMacro() [2/8]

mitk::SerialCommunication::itkSetMacro ( DataBits ,
DataBits  )

Set the number of data bits of the serial interface.

◆ itkSetMacro() [3/8]

mitk::SerialCommunication::itkSetMacro ( HardwareHandshake ,
HardwareHandshake  )

Set if hardware handshake should be used.

◆ itkSetMacro() [4/8]

mitk::SerialCommunication::itkSetMacro ( Parity ,
Parity  )

Set the parity mode of the serial interface.

◆ itkSetMacro() [5/8]

mitk::SerialCommunication::itkSetMacro ( PortNumber ,
PortNumber  )

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

◆ itkSetMacro() [6/8]

mitk::SerialCommunication::itkSetMacro ( ReceiveTimeout ,
unsigned int  )

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.

◆ itkSetMacro() [7/8]

mitk::SerialCommunication::itkSetMacro ( SendTimeout ,
unsigned int  )

set the send timeout in milliseconds

Only applies to WIN32, not POSIX

◆ itkSetMacro() [8/8]

mitk::SerialCommunication::itkSetMacro ( StopBits ,
StopBits  )

Set number of stop bits of the serial interface.

◆ itkSetStringMacro()

mitk::SerialCommunication::itkSetStringMacro ( DeviceName )

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).

◆ mitkClassMacroItkParent()

mitk::SerialCommunication::mitkClassMacroItkParent ( SerialCommunication ,
itk::Object  )

◆ OpenConnection()

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 57 of file mitkSerialCommunication.cpp.

◆ Receive()

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 nullptr (the default) then no End-of-Line character is used.

Definition at line 144 of file mitkSerialCommunication.cpp.

◆ Send()

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 222 of file mitkSerialCommunication.cpp.

◆ SendBreak()

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

Send the break signal for ms milliseconds.

Definition at line 471 of file mitkSerialCommunication.cpp.

Member Data Documentation

◆ m_BaudRate

BaudRate mitk::SerialCommunication::m_BaudRate
protected

baud rate of the serial interface connection

Definition at line 327 of file mitkSerialCommunication.h.

◆ m_Connected

bool mitk::SerialCommunication::m_Connected
protected

is set to true if a connection currently established

Definition at line 335 of file mitkSerialCommunication.h.

◆ m_DataBits

DataBits mitk::SerialCommunication::m_DataBits
protected

number of data bits per symbol

Definition at line 328 of file mitkSerialCommunication.h.

◆ m_DeviceName

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 325 of file mitkSerialCommunication.h.

◆ m_FileDescriptor

int mitk::SerialCommunication::m_FileDescriptor
protected

Definition at line 343 of file mitkSerialCommunication.h.

◆ m_HardwareHandshake

HardwareHandshake mitk::SerialCommunication::m_HardwareHandshake
protected

whether to use hardware handshake for the connection

Definition at line 331 of file mitkSerialCommunication.h.

◆ m_Parity

Parity mitk::SerialCommunication::m_Parity
protected

parity mode

Definition at line 329 of file mitkSerialCommunication.h.

◆ m_PortNumber

PortNumber mitk::SerialCommunication::m_PortNumber
protected

port number of the device

Definition at line 326 of file mitkSerialCommunication.h.

◆ m_ReceiveTimeout

unsigned int mitk::SerialCommunication::m_ReceiveTimeout
protected

timeout for receiving data from the serial interface in milliseconds

Definition at line 332 of file mitkSerialCommunication.h.

◆ m_SendTimeout

unsigned int mitk::SerialCommunication::m_SendTimeout
protected

timeout for sending data to the serial interface in milliseconds

Definition at line 333 of file mitkSerialCommunication.h.

◆ m_StopBits

StopBits mitk::SerialCommunication::m_StopBits
protected

number of stop bits per symbol

Definition at line 330 of file mitkSerialCommunication.h.


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