Medical Imaging Interaction Toolkit  2023.04.00
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 (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #ifndef mitkSerialCommunication_h
14 #define mitkSerialCommunication_h
15 
16 #include <MitkIGTExports.h>
17 #include "mitkCommon.h"
18 #include <itkObject.h>
19 #include <itkObjectFactory.h>
20 
21 #ifdef WIN32
22 #include <windows.h>
23 #else // Posix
24 #include <termios.h>
25 #endif
26 
27 namespace mitk
28 {
43  class MITKIGT_EXPORT SerialCommunication : public itk::Object
44  {
45  public:
47  itkFactorylessNewMacro(Self);
48  itkCloneMacro(Self);
49 
51  {
52  COM1 = 1,
53  COM2 = 2,
54  COM3 = 3,
55  COM4 = 4,
56  COM5 = 5,
57  COM6 = 6,
58  COM7 = 7,
59  COM8 = 8,
60  COM9 = 9,
61  COM10 = 10,
62  COM11 = 11,
63  COM12 = 12,
64  COM13 = 13
65  };
66 
67  enum BaudRate
68  {
69  BaudRate9600 = 9600,
70  BaudRate14400 = 14400,
71  BaudRate19200 = 19200,
72  BaudRate38400 = 38400,
73  BaudRate57600 = 57600,
74  BaudRate115200 = 115200, // Highest supported rate for NDI Aurora
75  BaudRate230400 = 230400,
76  BaudRate460800 = 460800,
77  BaudRate500000 = 500000,
78  BaudRate576000 = 576000,
79  BaudRate921600 = 921600,
80  BaudRate1000000 = 1000000,
81  BaudRate1152000 = 1152000,
82  // BaudRate1228739 = 1228739, // Highest supported rate for NDI Polaris According to handbook, unknown value to most compilers though
83  BaudRate1500000 = 1500000,
84  BaudRate2000000 = 2000000,
85  BaudRate2500000 = 2500000,
86  BaudRate3000000 = 3000000,
87  BaudRate3500000 = 3500000,
88  BaudRate4000000 = 4000000
89  };
90 
91  enum DataBits
92  {
93  DataBits8 = 8,
94  DataBits7 = 7
95  };
96 
97  enum Parity
98  {
99  None = 'N',
100  Odd = 'O',
101  Even = 'E'
102  };
103 
104  enum StopBits
105  {
106  StopBits1 = 1,
107  StopBits2 = 2
108  };
109 
111  {
112  HardwareHandshakeOn = 1,
113  HardwareHandshakeOff = 0
114  };
115 
120  bool IsConnected();
126  int OpenConnection();
131  void CloseConnection();
132 
152  int Receive(std::string& answer, unsigned int numberOfBytes, const char *eol=nullptr);
153 
163  int Send(const std::string& input, bool block = false);
164 
168  void SendBreak(unsigned int ms = 400);
169 
173  void ClearReceiveBuffer();
174 
178  void ClearSendBuffer();
179 
186  itkGetConstMacro(PortNumber, PortNumber);
187 
199  itkSetMacro(PortNumber, PortNumber);
200 
209  itkGetStringMacro(DeviceName);
210 
219  itkSetStringMacro(DeviceName);
220 
224  itkGetConstMacro(BaudRate, BaudRate);
225 
229  itkSetMacro(BaudRate, BaudRate);
230 
234  itkGetConstMacro(DataBits, DataBits);
235 
239  itkSetMacro(DataBits, DataBits);
240 
244  itkGetConstMacro(Parity, Parity);
245 
249  itkSetMacro(Parity, Parity);
250 
254  itkGetConstMacro(StopBits, StopBits);
255 
259  itkSetMacro(StopBits, StopBits);
260 
264  itkGetConstMacro(HardwareHandshake, HardwareHandshake);
265 
269  itkSetMacro(HardwareHandshake, HardwareHandshake);
270 
274  itkGetConstMacro(SendTimeout, unsigned int);
275 
281  itkSetMacro(SendTimeout, unsigned int);
282 
286  itkGetConstMacro(ReceiveTimeout, unsigned int);
287 
295  itkSetMacro(ReceiveTimeout, unsigned int);
296 
297  protected:
299  ~SerialCommunication() override;
300 
306  int ApplyConfiguration();
307 
308 
309  #ifdef WIN32
310 
313  int ApplyConfigurationWin();
314 
315  #else
316 
319  int ApplyConfigurationUnix();
320 
321 
322  #endif
323 
324 
325  std::string m_DeviceName;
332  unsigned int m_ReceiveTimeout;
333  unsigned int m_SendTimeout;
334 
335  bool m_Connected;
336 
337 #ifdef WIN32
338  HANDLE m_ComPortHandle;
339  DWORD m_PreviousMask;
340  COMMTIMEOUTS m_PreviousTimeout;
341  DCB m_PreviousDeviceControlBlock;
342 #else
344 #endif
345  };
346 } // namespace mitk
347 #endif
mitk::SerialCommunication::HardwareHandshake
HardwareHandshake
Definition: mitkSerialCommunication.h:110
mitk::SerialCommunication::m_PortNumber
PortNumber m_PortNumber
port number of the device
Definition: mitkSerialCommunication.h:326
mitk::SerialCommunication::m_HardwareHandshake
HardwareHandshake m_HardwareHandshake
whether to use hardware handshake for the connection
Definition: mitkSerialCommunication.h:331
mitk::SerialCommunication::m_DataBits
DataBits m_DataBits
number of data bits per symbol
Definition: mitkSerialCommunication.h:328
mitk::SerialCommunication::Parity
Parity
Definition: mitkSerialCommunication.h:97
mitk::SerialCommunication::BaudRate
BaudRate
Definition: mitkSerialCommunication.h:67
mitk::SerialCommunication
serial communication interface
Definition: mitkSerialCommunication.h:43
mitkClassMacroItkParent
#define mitkClassMacroItkParent(className, SuperClassName)
Definition: mitkCommon.h:45
mitk
DataCollection - Class to facilitate loading/accessing structured data.
Definition: RenderingTests.dox:1
mitk::AntiAliasing::None
@ None
mitk::SerialCommunication::m_SendTimeout
unsigned int m_SendTimeout
timeout for sending data to the serial interface in milliseconds
Definition: mitkSerialCommunication.h:333
mitk::SerialCommunication::PortNumber
PortNumber
Definition: mitkSerialCommunication.h:50
mitk::SerialCommunication::m_BaudRate
BaudRate m_BaudRate
baud rate of the serial interface connection
Definition: mitkSerialCommunication.h:327
mitk::SerialCommunication::DataBits
DataBits
Definition: mitkSerialCommunication.h:91
mitk::SerialCommunication::m_FileDescriptor
int m_FileDescriptor
Definition: mitkSerialCommunication.h:343
mitkCommon.h
mitk::SerialCommunication::m_ReceiveTimeout
unsigned int m_ReceiveTimeout
timeout for receiving data from the serial interface in milliseconds
Definition: mitkSerialCommunication.h:332
mitk::SerialCommunication::m_StopBits
StopBits m_StopBits
number of stop bits per symbol
Definition: mitkSerialCommunication.h:330
mitk::SerialCommunication::StopBits
StopBits
Definition: mitkSerialCommunication.h:104
mitk::SerialCommunication::m_DeviceName
std::string m_DeviceName
device name that is used to connect to the serial interface (will be used if != "")
Definition: mitkSerialCommunication.h:325
mitk::SerialCommunication::m_Connected
bool m_Connected
is set to true if a connection currently established
Definition: mitkSerialCommunication.h:335
mitk::SerialCommunication::m_Parity
Parity m_Parity
parity mode
Definition: mitkSerialCommunication.h:329