Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkSerialCommunication.h
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 #ifndef MITKSERIALCOMMUNICATION_H_HEADER_INCLUDED_
18 #define MITKSERIALCOMMUNICATION_H_HEADER_INCLUDED_
19 
20 #include <MitkIGTExports.h>
21 #include "mitkCommon.h"
22 #include <itkObject.h>
23 #include <itkObjectFactory.h>
24 
25 #ifdef WIN32
26 #include <windows.h>
27 #else // Posix
28 #include <termios.h>
29 #endif
30 
31 namespace mitk
32 {
47  class MITKIGT_EXPORT SerialCommunication : public itk::Object
48  {
49  public:
51  itkFactorylessNewMacro(Self)
52  itkCloneMacro(Self)
53 
55  {
56  COM1 = 1,
57  COM2 = 2,
58  COM3 = 3,
59  COM4 = 4,
60  COM5 = 5,
61  COM6 = 6,
62  COM7 = 7,
63  COM8 = 8,
64  COM9 = 9,
65  COM10 = 10,
66  COM11 = 11,
67  COM12 = 12,
68  COM13 = 13
69  };
70 
71  enum BaudRate
72  {
73  BaudRate9600 = 9600,
74  BaudRate14400 = 14400,
75  BaudRate19200 = 19200,
76  BaudRate38400 = 38400,
77  BaudRate57600 = 57600,
78  BaudRate115200 = 115200, // Highest supported rate for NDI Aurora
79  BaudRate230400 = 230400,
80  BaudRate460800 = 460800,
81  BaudRate500000 = 500000,
82  BaudRate576000 = 576000,
83  BaudRate921600 = 921600,
84  BaudRate1000000 = 1000000,
85  BaudRate1152000 = 1152000,
86  // BaudRate1228739 = 1228739, // Highest supported rate for NDI Polaris According to handbook, unknown value to most compilers though
87  BaudRate1500000 = 1500000,
88  BaudRate2000000 = 2000000,
89  BaudRate2500000 = 2500000,
90  BaudRate3000000 = 3000000,
91  BaudRate3500000 = 3500000,
92  BaudRate4000000 = 4000000
93  };
94 
95  enum DataBits
96  {
97  DataBits8 = 8,
98  DataBits7 = 7
99  };
100 
101  enum Parity
102  {
103  None = 'N',
104  Odd = 'O',
105  Even = 'E'
106  };
107 
108  enum StopBits
109  {
110  StopBits1 = 1,
111  StopBits2 = 2
112  };
113 
115  {
116  HardwareHandshakeOn = 1,
117  HardwareHandshakeOff = 0
118  };
119 
125  int OpenConnection();
130  void CloseConnection();
131 
151  int Receive(std::string& answer, unsigned int numberOfBytes, const char *eol=nullptr);
152 
162  int Send(const std::string& input, bool block = false);
163 
167  void SendBreak(unsigned int ms = 400);
168 
172  void ClearReceiveBuffer();
173 
177  void ClearSendBuffer();
178 
185  itkGetConstMacro(PortNumber, PortNumber);
186 
198  itkSetMacro(PortNumber, PortNumber);
199 
208  itkGetStringMacro(DeviceName);
209 
218  itkSetStringMacro(DeviceName);
219 
223  itkGetConstMacro(BaudRate, BaudRate);
224 
228  itkSetMacro(BaudRate, BaudRate);
229 
233  itkGetConstMacro(DataBits, DataBits);
234 
238  itkSetMacro(DataBits, DataBits);
239 
243  itkGetConstMacro(Parity, Parity);
244 
248  itkSetMacro(Parity, Parity);
249 
253  itkGetConstMacro(StopBits, StopBits);
254 
258  itkSetMacro(StopBits, StopBits);
259 
263  itkGetConstMacro(HardwareHandshake, HardwareHandshake);
264 
268  itkSetMacro(HardwareHandshake, HardwareHandshake);
269 
273  itkGetConstMacro(SendTimeout, unsigned int);
274 
280  itkSetMacro(SendTimeout, unsigned int);
281 
285  itkGetConstMacro(ReceiveTimeout, unsigned int);
286 
294  itkSetMacro(ReceiveTimeout, unsigned int);
295 
296  protected:
298  virtual ~SerialCommunication();
299 
305  int ApplyConfiguration();
306 
307 
308  #ifdef WIN32
309 
312  int ApplyConfigurationWin();
313 
314  #else
315 
318  int ApplyConfigurationUnix();
319 
320 
321  #endif
322 
323 
324  std::string m_DeviceName;
331  unsigned int m_ReceiveTimeout;
332  unsigned int m_SendTimeout;
333 
334  bool m_Connected;
335 
336 #ifdef WIN32
337  HANDLE m_ComPortHandle;
338  DWORD m_PreviousMask;
339  COMMTIMEOUTS m_PreviousTimeout;
340  DCB m_PreviousDeviceControlBlock;
341 #else
343 #endif
344  };
345 } // namespace mitk
346 #endif /* MITKSERIALCOMMUNICATION_H_HEADER_INCLUDED_ */
std::string m_DeviceName
device name that is used to connect to the serial interface (will be used if != "") ...
BaudRate m_BaudRate
baud rate of the serial interface connection
unsigned int m_SendTimeout
timeout for sending data to the serial interface in milliseconds
DataCollection - Class to facilitate loading/accessing structured data.
StopBits m_StopBits
number of stop bits per symbol
unsigned int m_ReceiveTimeout
timeout for receiving data from the serial interface in milliseconds
HardwareHandshake m_HardwareHandshake
whether to use hardware handshake for the connection
#define mitkClassMacroItkParent(className, SuperClassName)
Definition: mitkCommon.h:53
PortNumber m_PortNumber
port number of the device
serial communication interface
DataBits m_DataBits
number of data bits per symbol
bool m_Connected
is set to true if a connection currently established