17 #include <itksys/SystemTools.hxx> 19 #include <sys/types.h> 22 #include <sys/ioctl.h> 28 #define INVALID_HANDLE_VALUE -1 35 m_DeviceName(
""), m_PortNumber(COM1), m_BaudRate(BaudRate9600),
36 m_DataBits(DataBits8), m_Parity(
None), m_StopBits(StopBits1),
37 m_HardwareHandshake(HardwareHandshakeOff),
38 m_ReceiveTimeout(500), m_SendTimeout(500), m_Connected(false)
40 #ifdef WIN32 // Windows 65 ss <<
"\\\\.\\COM" << static_cast<unsigned int>(
m_PortNumber);
69 m_ComPortHandle = CreateFile(ss.str().c_str(), GENERIC_READ | GENERIC_WRITE,
78 GetCommState(m_ComPortHandle, &m_PreviousDeviceControlBlock);
79 GetCommTimeouts(m_ComPortHandle, &m_PreviousTimeout);
80 GetCommMask(m_ComPortHandle, &m_PreviousMask);
84 CloseHandle(m_ComPortHandle);
93 if (m_DeviceName.empty())
94 ss <<
"/dev/ttyS" << static_cast<unsigned int>(
m_PortNumber) - 1;
123 SetCommState(m_ComPortHandle, &m_PreviousDeviceControlBlock);
124 SetCommTimeouts(m_ComPortHandle, &m_PreviousTimeout);
125 SetCommMask(m_ComPortHandle, m_PreviousMask);
126 PurgeComm(m_ComPortHandle, PURGE_TXCLEAR | PURGE_RXCLEAR);
127 CloseHandle(m_ComPortHandle);
146 if (numberOfBytes == 0)
155 DWORD numberOfBytesRead = 0;
156 char* buffer =
new char[numberOfBytes];
157 if (ReadFile(m_ComPortHandle, buffer, numberOfBytes, &numberOfBytesRead,
nullptr) != 0)
159 if (numberOfBytesRead > 0)
161 answer.assign(buffer, numberOfBytesRead);
163 if (numberOfBytesRead == numberOfBytes)
186 unsigned long bytesRead = 0;
187 unsigned long bytesLeft = numberOfBytes;
188 auto buffer =
new char[numberOfBytes];
190 while ((bytesLeft > 0) && (bytesRead < numberOfBytes))
206 if (eol && *eol == buffer[bytesRead-1])
210 answer.assign(buffer, bytesRead);
214 if ( bytesRead == numberOfBytes ||
215 (eol && answer.size() > 0 && *eol == answer.at(answer.size()-1)) )
234 DWORD bytesWritten = 0;
235 if (WriteFile(m_ComPortHandle, input.data(),
static_cast<DWORD
>(input.size()), &bytesWritten,
nullptr) == TRUE)
244 long bytesWritten = 0;
245 long bytesLeft = input.size();
247 while (bytesLeft > 0)
249 bytesWritten = write(
m_FileDescriptor, input.data() + bytesWritten, bytesLeft);
250 if (bytesWritten <= 0)
252 bytesLeft -= bytesWritten;
266 #ifdef WIN32 // Windows implementation 267 return ApplyConfigurationWin();
278 int mitk::SerialCommunication::ApplyConfigurationWin()
284 if (GetCommState(m_ComPortHandle, &controlSettings) == 0)
289 std::ostringstream o;
291 if (BuildCommDCBA(o.str().c_str(), &controlSettings) == 0)
296 controlSettings.fDtrControl = DTR_CONTROL_ENABLE;
297 controlSettings.fRtsControl = RTS_CONTROL_ENABLE;
298 controlSettings.fOutxCtsFlow = TRUE;
299 controlSettings.fRtsControl = RTS_CONTROL_HANDSHAKE;
303 controlSettings.fDtrControl = DTR_CONTROL_DISABLE;
304 controlSettings.fRtsControl = RTS_CONTROL_DISABLE;
305 controlSettings.fOutxCtsFlow = FALSE;
306 controlSettings.fRtsControl = RTS_CONTROL_DISABLE;
308 if (SetCommState(m_ComPortHandle, &controlSettings) == FALSE)
311 COMMTIMEOUTS timeouts;
314 timeouts.ReadTotalTimeoutMultiplier = 0;
316 timeouts.WriteTotalTimeoutMultiplier = 0;
318 if (SetCommTimeouts(m_ComPortHandle, &timeouts) == FALSE)
321 PurgeComm(m_ComPortHandle, PURGE_TXCLEAR | PURGE_RXCLEAR);
334 struct termios termIOStructure;
338 cfmakeraw(&termIOStructure);
339 termIOStructure.c_cflag |= CLOCAL;
342 termIOStructure.c_cflag |= CRTSCTS;
343 termIOStructure.c_iflag &= ~(IXON|IXOFF);
347 termIOStructure.c_cflag &= ~CRTSCTS;
348 termIOStructure.c_iflag &= ~(IXON|IXOFF);
350 termIOStructure.c_cflag &= ~CSIZE;
354 termIOStructure.c_cflag |= CS7;
358 termIOStructure.c_cflag |= CS8;
363 termIOStructure.c_cflag |= CSTOPB;
367 termIOStructure.c_cflag &= ~CSTOPB;
372 termIOStructure.c_cflag |= (PARENB|PARODD);
375 termIOStructure.c_cflag |= PARENB;
376 termIOStructure.c_cflag &= ~PARODD;
382 termIOStructure.c_cflag &= ~PARENB;
452 MITK_WARN(
"mitk::SerialCommunication") <<
"Baud rate not recognized, using default of 9600 Baud.";
456 cfsetispeed(&termIOStructure, baudrate);
457 cfsetospeed(&termIOStructure, baudrate);
459 termIOStructure.c_cc[VMIN] = 0;
476 SetCommBreak(m_ComPortHandle);
477 itksys::SystemTools::Delay(ms);
478 ClearCommBreak(m_ComPortHandle);
494 PurgeComm(m_ComPortHandle, PURGE_RXCLEAR);
505 PurgeComm(m_ComPortHandle, PURGE_TXCLEAR);
std::string m_DeviceName
device name that is used to connect to the serial interface (will be used if != "") ...
int ApplyConfigurationUnix()
Applies the configuration for Linux.
void ClearReceiveBuffer()
erase the receive buffer of the serial interface
BaudRate m_BaudRate
baud rate of the serial interface connection
int ApplyConfiguration()
configures the serial interface with all parameters
Parity m_Parity
parity mode
unsigned int m_SendTimeout
timeout for sending data to the serial interface in milliseconds
~SerialCommunication() override
int Send(const std::string &input, bool block=false)
Send the string input.
void CloseConnection()
Closes the connection.
StopBits m_StopBits
number of stop bits per symbol
int Receive(std::string &answer, unsigned int numberOfBytes, const char *eol=nullptr)
Read numberOfBytes characters from the serial interface.
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
bool IsConnected()
Returns m_Connected.
PortNumber m_PortNumber
port number of the device
void ClearSendBuffer()
erase the send buffer of the serial interface
void SendBreak(unsigned int ms=400)
Send the break signal for ms milliseconds.
DataBits m_DataBits
number of data bits per symbol
int OpenConnection()
Opens connection to the COM port with port number m_PortNumber or the device name m_DeviceName and al...
bool m_Connected
is set to true if a connection currently established
#define INVALID_HANDLE_VALUE