16 # include <sys/ioctl.h>
19 # include <sys/param.h>
20 # include <sys/file.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
71 memset(&m_commState, 0,
sizeof(m_commState));
114 if (::GetCommTimeouts(
m_handle,&cto))
116 cto.ReadIntervalTimeout = MAXDWORD;
117 cto.ReadTotalTimeoutConstant = 0;
118 cto.ReadTotalTimeoutMultiplier = 0;
119 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);
190 else status &= ~TIOCM_DTR;
201 else status &= ~TIOCM_RTS;
223 if (!PurgeComm(
m_handle, PURGE_TXCLEAR | PURGE_RXCLEAR))
253 return m_handle != INVALID_HANDLE_VALUE;
274 JLDEBUG(gJournal,
"port " << portInfo.portName().toStdString() <<
" at " << portInfo.baudrate() <<
" baud");
278 JLALERT(gJournal,
"Port " << portInfo.portName().toStdString() <<
" is already open");
285 char winPortName[256];
288 sprintf(winPortName,
"\\\\.\\%s", portInfo.portName().c_str());
289 m_handle = CreateFileA(winPortName, GENERIC_READ | GENERIC_WRITE, 0, NULL,
290 OPEN_EXISTING, 0, NULL);
291 if (
m_handle == INVALID_HANDLE_VALUE)
293 JLDEBUG(gJournal,
"Port " << portInfo.portName().toStdString() <<
" cannot be opened");
299 commState.DCBlength =
sizeof(DCB);
302 if (!GetCommState(
m_handle, &commState))
305 commState.BaudRate = (int) portInfo.baudrate();
306 commState.Parity = NOPARITY;
307 commState.ByteSize = 8;
308 commState.StopBits = TWOSTOPBITS;
309 commState.fDsrSensitivity =
FALSE;
310 commState.fOutxCtsFlow =
FALSE;
311 commState.fOutxDsrFlow =
FALSE;
312 commState.fOutX =
FALSE;
313 commState.fInX =
FALSE;
314 commState.fRtsControl = RTS_CONTROL_ENABLE;
315 if (!SetCommState(
m_handle, (LPDCB)&commState))
319 commState.StopBits = ONESTOPBIT;
320 if (!SetCommState(
m_handle, (LPDCB)&commState))
323 std::string tmp = portInfo.portName().toStdString();
324 m_port = atoi(&tmp.c_str()[3]);
331 if (!EscapeCommFunction(
m_handle, SETDTR))
333 if (!SetupComm(
m_handle,readBufSize,writeBufSize))
338 if (!PurgeComm(
m_handle, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR))
352 std::string pn = portInfo.portName().toStdString();
365 if (flock(
m_handle, LOCK_EX | LOCK_NB))
373 if (tcgetattr(
m_handle, &m_commState) != 0)
377 if (cfsetispeed(&m_commState, portInfo.baudrate()) != 0)
380 if (cfsetospeed(&m_commState, portInfo.baudrate()) != 0)
384 m_commState.c_cflag |= (CLOCAL | CREAD);
386 m_commState.c_cflag &= ~(CSIZE|PARENB);
387 m_commState.c_cflag |= CS8;
388 m_commState.c_cflag |= CSTOPB;
390 m_commState.c_cflag &= ~CRTSCTS;
391 m_commState.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
393 m_commState.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
395 m_commState.c_oflag &= ~OPOST;
397 m_commState.c_cc[VMIN] = 0;
398 m_commState.c_cc[VTIME] = (
m_timeout+99)/100;
401 if (tcsetattr(
m_handle,TCSANOW, &m_commState) != 0)
404 termios checkCommState;
405 if (tcgetattr(
m_handle, &checkCommState) != 0)
408 if ((m_commState.c_cflag != checkCommState.c_cflag) ||
409 (m_commState.c_iflag != checkCommState.c_iflag) ||
410 (m_commState.c_oflag != checkCommState.c_oflag) ||
411 (m_commState.c_cc[VMIN] != checkCommState.c_cc[VMIN]) ||
412 (m_commState.c_cc[VTIME] != checkCommState.c_cc[VTIME]))
414 JLDEBUG(gJournal,
"commstates do not match, which is OK for USB connected MkIV devices");
424 if (ioctl(
m_handle, TIOCMGET, &cmbits) < 0)
426 JLDEBUG(gJournal,
"TIOCMGET failed, which is OK for USB connected MkIV devices");
429 cmbits |= TIOCM_RTS|TIOCM_DTR;
431 if (ioctl(
m_handle, TIOCMSET, &cmbits) < 0)
433 JLDEBUG(gJournal,
"TIOCMSET failed, which is OK for USB connected MkIV devices");
437 JLDEBUG(gJournal,
"Port " << portInfo.portName().toStdString() <<
" opened");
445 FILE* pf =
fopen(filename,
"r");
461 strcpy(basename, filename);
462 basename[strlen(basename) - 4] = 0;
469 sprintf(filename2,
"%s_n.log", basename);
474 strcpy(filename, filename2);
496 JLTRACE(gJournal,
"ReadFile result " << rres <<
", length " <<
length);
500 DWORD wErr = ::GetLastError();
501 JLALERT(gJournal,
"ReadFile returned windows error " << wErr);
502 if (wErr >= ERROR_INVALID_FUNCTION && wErr <= ERROR_INVALID_HANDLE)
519 timeout.tv_usec = (
m_timeout - (timeout.tv_sec * 1000)) * 1000;
521 int res = select(FD_SETSIZE, &fd, NULL, &err, &timeout);
526 }
else if (
res == 0) {
553 #ifdef LOG_RX_TX_FLUSH
573 JLDEBUG(gJournal,
"Setting timeout to " << ms <<
" ms");
578 COMMTIMEOUTS commTimeouts;
580 if (!GetCommTimeouts(
m_handle,&commTimeouts))
586 commTimeouts.ReadIntervalTimeout = 0;
587 commTimeouts.ReadTotalTimeoutConstant =
m_timeout;
588 commTimeouts.ReadTotalTimeoutMultiplier = 0;
589 commTimeouts.WriteTotalTimeoutConstant =
m_timeout;
590 commTimeouts.WriteTotalTimeoutMultiplier = 0;
595 commTimeouts.ReadIntervalTimeout = MAXDWORD;
596 commTimeouts.ReadTotalTimeoutConstant = 0;
597 commTimeouts.ReadTotalTimeoutMultiplier = 0;
598 commTimeouts.WriteTotalTimeoutConstant = 0;
599 commTimeouts.WriteTotalTimeoutMultiplier = 0;
602 if (!SetCommTimeouts(
m_handle, &commTimeouts))
606 m_commState.c_cc[VMIN] = 0;
607 m_commState.c_cc[VTIME] = (
m_timeout+99)/100;
611 tcsetattr(
m_handle,TCSANOW, &m_commState);
669 if (WriteFile(
m_handle,
data.data(), (DWORD)
data.size(), &lwritten, NULL) == 0)
698 #ifdef LOG_RX_TX_FLUSH
XsBaudRate getBaudrate(void) const
Return the baudrate that is currently being used by the port.
XsResultValue escape(XsControlLine mask, XsControlLine state)
Manipulate the Serial control lines.
SerialInterface()
Default constructor, initializes all members to their default values.
uint16_t getPortNumber(void) const
Retrieve the port number that was last successfully opened.
XsIoHandle m_handle
The serial port handle, also indicates if the port is open or not.
bool isOpen(void) const
Return whether the communication port is open or not.
XsResultValue getLastResult(void) const
Return the error code of the last operation.
char m_portname[32]
The name of the open serial port.
XsResultValue waitForData(XsSize maxLength, XsByteArray &data)
Wait for data to arrive or a timeout to occur.
XsResultValue close(void)
Close the serial communication port.
void cancelIo(void) const
Cancel any pending io requests.
XsIoHandle getHandle(void) const
Return the handle of the port.
XsResultValue closeLive(void)
Close the serial communication port.
virtual ~SerialInterface()
Destructor, de-initializes, frees memory allocated for buffers, etc.
uint16_t m_port
The opened COM port nr.
XsResultValue open(const XsPortInfo &portInfo, uint32_t readBufSize=XS_DEFAULT_READ_BUFFER_SIZE, uint32_t writeBufSize=XS_DEFAULT_WRITE_BUFFER_SIZE)
Open a communication channel to the given port info.
uint32_t m_endTime
The time at which an operation will end in ms, used by several functions.
uint32_t getTimeout(void) const
Return the current timeout value.
void getPortName(XsString &portname) const
Retrieve the port name that was last successfully opened.
XsBaudRate m_baudrate
The baudrate that was last set to be used by the port.
XsResultValue m_lastResult
The last result of an operation.
XsResultValue flushData(void)
Flush all data in the buffers to and from the device.
XsResultValue writeData(const XsByteArray &data, XsSize *written=0)
Write the data contained in data to the device.
XsResultValue readData(XsSize maxLength, XsByteArray &data)
Read data from the serial port and put it into the data buffer.
XsResultValue setTimeout(uint32_t ms)
Set the default timeout value to use in blocking operations.
GLsizei GLsizei GLenum GLenum const GLvoid * data
GLuint GLsizei GLsizei * length
typedef void(APIENTRYP PFNGLBLENDCOLORPROC)(GLclampf red
GLsizei const GLchar ** string
XsResultValue
Xsens result values.
XsControlLine
Serial control lines.
@ XCL_RTS
pin 7: Request To Send
@ XCL_DTR
pin 4: Data Terminal Ready
char BASE_IMPEXP * strcpy(char *dest, size_t destSize, const char *source) MRPT_NO_THROWS
An OS-independent version of strcpy.
int BASE_IMPEXP sprintf(char *buf, size_t bufSize, const char *format,...) MRPT_NO_THROWS MRPT_printf_format_check(3
An OS-independent version of sprintf (Notice the bufSize param, which may be ignored in some compiler...
FILE BASE_IMPEXP * fopen(const char *fileName, const char *mode) MRPT_NO_THROWS
An OS-independent version of fopen.
int BASE_IMPEXP void BASE_IMPEXP fclose(FILE *f)
An OS-independent version of fclose.
#define XS_MAX_FILENAME_LENGTH
unsigned __int16 uint16_t
unsigned __int32 uint32_t
void makeFilenameUnique(char *filename)
Helper function for making filename of log file unique.
bool doesFileExist(char *filename)
Helper function for making filename of log file unique.
XsBaudRate
Communication speed.
@ XBR_Invalid
Not a valid baud rate.
struct XsByteArray XsByteArray
@ XRV_ALREADYOPEN
An I/O device is already opened with this object.
@ XRV_INPUTCANNOTBEOPENED
The specified i/o device can not be opened.
@ XRV_NOPORTOPEN
No serial port opened for reading/writing.
@ XRV_NOFILEORPORTOPEN
No file or serial port opened for reading/writing.
@ XRV_ERROR
A generic error occurred.
@ XRV_TIMEOUT
A timeout occurred.
@ XRV_OK
Operation was performed successfully.
XSTYPES_DLL_API uint32_t XsTime_getTimeOfDay(struct tm *date_, time_t *secs_)
size_t XsSize
XsSize must be unsigned number!