17 #include <sys/ioctl.h> 18 #include <sys/param.h> 27 #ifndef _CRT_SECURE_NO_DEPRECATE 28 #define _CRT_SECURE_NO_DEPRECATE 30 #pragma warning(disable : 4996) 37 #define FSEEK(x) _fseeki64(m_handle, x, SEEK_SET) 38 #define FSEEK_R(x) _fseeki64(m_handle, x, SEEK_END) 39 #define FTELL() _ftelli64(m_handle) 41 #define FSEEK(x) fseeko(m_handle, x, SEEK_SET) 42 #define FSEEK_R(x) fseeko(m_handle, x, SEEK_END) 43 #define FTELL() ftello(m_handle) 49 #define XDA1LOG_OBSOLETE XSENSLOG 51 #define XDA1LOG_OBSOLETE(...) (void)0 72 memset(&m_commState, 0,
sizeof(m_commState));
113 if (::GetCommTimeouts(
m_handle, &cto))
115 cto.ReadIntervalTimeout = MAXDWORD;
116 cto.ReadTotalTimeoutConstant = 0;
117 cto.ReadTotalTimeoutMultiplier = 0;
118 if (::SetCommTimeouts(
m_handle, &cto))
166 rv = EscapeCommFunction(
m_handle, SETDTR);
168 rv = EscapeCommFunction(
m_handle, CLRDTR);
174 rv = EscapeCommFunction(
m_handle, SETRTS);
176 rv = EscapeCommFunction(
m_handle, CLRRTS);
227 if (!PurgeComm(
m_handle, PURGE_TXCLEAR | PURGE_RXCLEAR))
262 return m_handle != INVALID_HANDLE_VALUE;
287 gJournal,
"port " << portInfo.portName().toStdString() <<
" at " 288 << portInfo.baudrate() <<
" baud");
294 "Port " << portInfo.portName().toStdString() <<
" is already open");
301 char winPortName[256];
304 sprintf(winPortName,
"\\\\.\\%s", portInfo.portName().c_str());
306 winPortName, GENERIC_READ | GENERIC_WRITE, 0,
nullptr, OPEN_EXISTING, 0,
308 if (
m_handle == INVALID_HANDLE_VALUE)
311 gJournal,
"Port " << portInfo.portName().toStdString()
312 <<
" cannot be opened");
319 commState.DCBlength =
sizeof(DCB);
322 if (!GetCommState(
m_handle, &commState))
325 commState.BaudRate = (int)portInfo.baudrate();
326 commState.Parity = NOPARITY;
327 commState.ByteSize = 8;
328 commState.StopBits = TWOSTOPBITS;
329 commState.fDsrSensitivity =
FALSE;
330 commState.fOutxCtsFlow =
FALSE;
331 commState.fOutxDsrFlow =
FALSE;
332 commState.fOutX =
FALSE;
333 commState.fInX =
FALSE;
334 commState.fRtsControl = RTS_CONTROL_ENABLE;
335 if (!SetCommState(
m_handle, (LPDCB)&commState))
339 commState.StopBits = ONESTOPBIT;
340 if (!SetCommState(
m_handle, (LPDCB)&commState))
343 std::string tmp = portInfo.portName().toStdString();
344 m_port = atoi(&tmp.c_str()[3]);
350 if (!EscapeCommFunction(
m_handle, SETDTR))
354 if (!SetupComm(
m_handle, readBufSize, writeBufSize))
361 PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR))
375 std::string pn = portInfo.portName().toStdString();
390 if (flock(
m_handle, LOCK_EX | LOCK_NB))
401 if (cfsetispeed(&m_commState, portInfo.baudrate()) != 0)
return XRV_ERROR;
403 if (cfsetospeed(&m_commState, portInfo.baudrate()) != 0)
return XRV_ERROR;
406 m_commState.c_cflag |= (CLOCAL | CREAD);
409 m_commState.c_cflag &= ~(CSIZE | PARENB);
410 m_commState.c_cflag |= CS8;
411 m_commState.c_cflag |= CSTOPB;
413 m_commState.c_cflag &= ~CRTSCTS;
414 m_commState.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
416 m_commState.c_iflag &=
417 ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
419 m_commState.c_oflag &= ~OPOST;
421 m_commState.c_cc[VMIN] = 0;
422 m_commState.c_cc[VTIME] = (
m_timeout + 99) / 100;
425 if (tcsetattr(
m_handle, TCSANOW, &m_commState) != 0)
428 termios checkCommState;
431 if ((m_commState.c_cflag != checkCommState.c_cflag) ||
432 (m_commState.c_iflag != checkCommState.c_iflag) ||
433 (m_commState.c_oflag != checkCommState.c_oflag) ||
434 (m_commState.c_cc[VMIN] != checkCommState.c_cc[VMIN]) ||
435 (m_commState.c_cc[VTIME] != checkCommState.c_cc[VTIME]))
439 "commstates do not match, which is OK for USB connected MkIV " 450 if (ioctl(
m_handle, TIOCMGET, &cmbits) < 0)
454 "TIOCMGET failed, which is OK for USB connected MkIV devices");
457 cmbits |= TIOCM_RTS | TIOCM_DTR;
459 if (ioctl(
m_handle, TIOCMSET, &cmbits) < 0)
463 "TIOCMSET failed, which is OK for USB connected MkIV devices");
468 gJournal,
"Port " << portInfo.portName().toStdString() <<
" opened");
476 FILE* pf =
fopen(filename,
"r");
477 if (pf ==
nullptr)
return false;
491 strcpy(basename, filename);
492 basename[strlen(basename) - 4] = 0;
499 sprintf(filename2,
"%s_n.log", basename);
503 strcpy(filename, filename2);
527 JLTRACE(gJournal,
"ReadFile result " << rres <<
", length " <<
length);
531 DWORD wErr = ::GetLastError();
532 JLALERT(gJournal,
"ReadFile returned windows error " << wErr);
533 if (wErr >= ERROR_INVALID_FUNCTION && wErr <= ERROR_INVALID_HANDLE)
549 timeout.tv_usec = (
m_timeout - (timeout.tv_sec * 1000)) * 1000;
551 int res = select(FD_SETSIZE, &fd,
nullptr, &err, &timeout);
579 fname,
"rx_%s_%d.log", devname + 1,
587 #ifdef LOG_RX_TX_FLUSH 594 gJournal,
"returned success, read " 596 <<
" bytes, first: " << JLHEXLOG(
data[0]));
612 JLDEBUG(gJournal,
"Setting timeout to " << ms <<
" ms");
617 COMMTIMEOUTS commTimeouts;
619 if (!GetCommTimeouts(
626 commTimeouts.ReadIntervalTimeout = 0;
627 commTimeouts.ReadTotalTimeoutConstant =
m_timeout;
628 commTimeouts.ReadTotalTimeoutMultiplier = 0;
629 commTimeouts.WriteTotalTimeoutConstant =
m_timeout;
630 commTimeouts.WriteTotalTimeoutMultiplier = 0;
635 commTimeouts.ReadIntervalTimeout = MAXDWORD;
636 commTimeouts.ReadTotalTimeoutConstant = 0;
637 commTimeouts.ReadTotalTimeoutMultiplier = 0;
638 commTimeouts.WriteTotalTimeoutConstant = 0;
639 commTimeouts.WriteTotalTimeoutMultiplier = 0;
642 if (!SetCommTimeouts(
647 m_commState.c_cc[VMIN] = 0;
648 m_commState.c_cc[VTIME] = (
m_timeout + 99) / 100;
691 gJournal,
"Read " <<
data.size() <<
" of " <<
maxLength <<
" bytes");
706 if (written ==
nullptr) written = &bytes;
737 fname,
"tx_%s_%d.log", devname + 1,
745 #ifdef LOG_RX_TX_FLUSH bool isOpen(void) const override
Return whether the communication port is open or not.
Operation was performed successfully.
XsIoHandle m_handle
The serial port handle, also indicates if the port is open or not.
uint16_t getPortNumber(void) const
Retrieve the port number that was last successfully opened.
#define XS_MAX_FILENAME_LENGTH
unsigned __int16 uint16_t
XsResultValue readData(XsSize maxLength, XsByteArray &data) override
Read data from the serial port and put it into the data buffer.
uint16_t m_port
The opened COM port nr.
int void fclose(FILE *f)
An OS-independent version of fclose.
pin 4: Data Terminal Ready
XsResultValue flushData(void) override
Flush all data in the buffers to and from the device.
struct XsByteArray XsByteArray
XsControlLine
Serial control lines.
size_t XsSize
XsSize must be unsigned number!
XsResultValue setTimeout(uint32_t ms) override
Set the default timeout value to use in blocking operations.
XSTYPES_DLL_API uint32_t XsTime_getTimeOfDay(struct tm *date_, time_t *secs_)
XsBaudRate getBaudrate(void) const
Return the baudrate that is currently being used by the port.
XsResultValue
Xsens result values.
XsBaudRate m_baudrate
The baudrate that was last set to be used by the port.
char m_portname[32]
The name of the open serial port.
XsResultValue escape(XsControlLine mask, XsControlLine state)
Manipulate the Serial control lines.
XsResultValue closeLive(void)
Close the serial communication port.
XsResultValue m_lastResult
The last result of an operation.
void cancelIo(void) const override
Cancel any pending io requests.
GLsizei const GLchar ** string
void makeFilenameUnique(char *filename)
Helper function for making filename of log file unique.
XsIoHandle getHandle(void) const
Return the handle of the port.
XsResultValue open(const XsPortInfo &portInfo, uint32_t readBufSize=XS_DEFAULT_READ_BUFFER_SIZE, uint32_t writeBufSize=XS_DEFAULT_WRITE_BUFFER_SIZE) override
Open a communication channel to the given port info.
uint32_t getTimeout(void) const override
Return the current timeout value.
XsResultValue close(void) override
Close the serial communication port.
GLuint GLsizei GLsizei * length
uint32_t m_endTime
The time at which an operation will end in ms, used by several functions.
~SerialInterface() override
Destructor, de-initializes, frees memory allocated for buffers, etc.
XsResultValue writeData(const XsByteArray &data, XsSize *written=0) override
Write the data contained in data to the device.
FILE * fopen(const char *fileName, const char *mode) noexcept
An OS-independent version of fopen.
typedef void(APIENTRYP PFNGLBLENDCOLORPROC)(GLclampf red
void getPortName(XsString &portname) const
Retrieve the port name that was last successfully opened.
char * strcpy(char *dest, size_t destSize, const char *source) noexcept
An OS-independent version of strcpy.
unsigned __int32 uint32_t
XsBaudRate
Communication speed.
GLsizei GLsizei GLenum GLenum const GLvoid * data
XsResultValue getLastResult(void) const override
Return the error code of the last operation.
SerialInterface()
Default constructor, initializes all members to their default values.
XsResultValue waitForData(XsSize maxLength, XsByteArray &data) override
Wait for data to arrive or a timeout to occur.
int sprintf(char *buf, size_t bufSize, const char *format,...) noexcept MRPT_printf_format_check(3
An OS-independent version of sprintf (Notice the bufSize param, which may be ignored in some compiler...
bool doesFileExist(char *filename)
Helper function for making filename of log file unique.