37 #include <mrpt/config.h> 41 #include <sys/ioctl.h> 50 #elif defined(HAVE_MALLOC_MALLOC_H) 51 #include <malloc/malloc.h> 55 #include <sys/param.h> 64 #define PATH_MAX CMT_MAX_FILENAME_LENGTH 71 #ifndef _CRT_SECURE_NO_DEPRECATE 72 #define _CRT_SECURE_NO_DEPRECATE 74 #pragma warning(disable : 4996) 80 #define FSEEK(x) _fseeki64(m_handle, x, SEEK_SET) 81 #define FSEEK_R(x) _fseeki64(m_handle, x, SEEK_END) 82 #define FTELL() _ftelli64(m_handle) 84 #define FSEEK(x) fseek(m_handle, x, SEEK_SET) 85 #define FSEEK_R(x) fseek(m_handle, x, SEEK_END) 86 #define FTELL() ftell(m_handle) 89 #define FSEEK(x) fseeko(m_handle, x, SEEK_SET) 90 #define FSEEK_R(x) fseeko(m_handle, x, SEEK_END) 91 #define FTELL() ftello(m_handle) 98 int _wcsnicmp(
const wchar_t* s1,
const wchar_t* s2,
int count)
100 for (
int i = 0; i <
count; ++i, ++s1, ++s2)
106 else if (*s2 == L
'\0')
116 #if defined(_DEBUG) || defined(_LOG_ALWAYS) 117 #if !defined(_LOG_TO_DBVIEW) 118 #ifdef _LOG_TO_STDOUT 119 #else // !dbview && !stdout 120 FILE* debug_log_fp =
nullptr;
123 FILE* debug_qlog_fp =
nullptr;
129 void CMTLOG(
const char* str, ...)
131 #ifdef _LOG_TO_STDOUT 136 #ifdef _LOG_TO_DBVIEW 143 OutputDebugString(buf);
145 if (debug_log_valid == 0)
147 debug_log_fp =
fopen(
"debug_log_cmt.log",
"w");
148 if (debug_log_fp !=
nullptr)
151 debug_log_valid = -1;
153 if (debug_log_valid == 1)
159 int32_t sz = vsprintf_s(buf, str, ptr);
162 fprintf(debug_log_fp,
"%5u.%03u %s", nw / 1000, nw % 1000, buf);
164 fflush(debug_log_fp);
173 #define CMT1LOG CMTLOG 208 if (rx_log !=
nullptr)
fclose(rx_log);
209 if (tx_log !=
nullptr)
fclose(tx_log);
223 cto.ReadIntervalTimeout = MAXDWORD;
224 cto.ReadTotalTimeoutConstant = 0;
225 cto.ReadTotalTimeoutMultiplier = 0;
254 rv = EscapeCommFunction(
m_handle, SETDTR);
256 rv = EscapeCommFunction(
m_handle, CLRDTR);
262 rv = EscapeCommFunction(
m_handle, SETRTS);
264 rv = EscapeCommFunction(
m_handle, CLRRTS);
312 PurgeComm(
m_handle, PURGE_TXCLEAR | PURGE_RXCLEAR);
330 CMT1LOG(
"L1: Open port %s at %d baud\n", portName, baudRate);
334 CMT1LOG(
"L1: Port already open\n");
340 char winPortName[32];
343 sprintf(winPortName,
"\\\\.\\%s", portName);
345 winPortName, GENERIC_READ | GENERIC_WRITE, 0,
nullptr, OPEN_EXISTING, 0,
347 if (
m_handle == INVALID_HANDLE_VALUE)
349 CMT1LOG(
"L1: Port cannot be opened\n");
381 m_port = atoi(&portName[3]);
387 EscapeCommFunction(
m_handle, SETRTS);
389 EscapeCommFunction(
m_handle, SETDTR);
390 SetupComm(
m_handle, readBufSize, writeBufSize);
396 PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR);
430 m_commState.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
433 ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
450 if (ioctl(
m_handle, TIOCMGET, &cmbits) < 0)
455 cmbits |= TIOCM_RTS | TIOCM_DTR;
457 if (ioctl(
m_handle, TIOCMSET, &cmbits) < 0)
474 char comFileName[32];
477 sprintf(comFileName,
"COM%u", portNumber);
479 return Cmt1s::open(comFileName, baudRate, readBufSize, writeBufSize);
502 #ifdef _LOG_CALLBACKS 504 "C1: onBytesReceived(%d,(%d,%d),%p)\n",
515 CMT1LOG(
"L1: readData, ReadFile returned error %u\n", ::GetLastError());
525 if (rx_log ==
nullptr)
529 rx_log =
fopen(fname,
"wb");
536 (
length[0] ?
"L1: readData returned success, read %u of %u bytes, " 538 :
"L1: readData returned success, read %u bytes\n"),
563 CMT1LOG(
"L1: Setting timeout to %u ms\n", ms);
568 COMMTIMEOUTS commTimeouts;
570 GetCommTimeouts(
m_handle, &commTimeouts);
575 commTimeouts.ReadIntervalTimeout = 0;
576 commTimeouts.ReadTotalTimeoutConstant =
m_timeout;
577 commTimeouts.ReadTotalTimeoutMultiplier = 0;
578 commTimeouts.WriteTotalTimeoutConstant =
m_timeout;
579 commTimeouts.WriteTotalTimeoutMultiplier = 0;
584 commTimeouts.ReadIntervalTimeout = MAXDWORD;
585 commTimeouts.ReadTotalTimeoutConstant = 0;
586 commTimeouts.ReadTotalTimeoutMultiplier = 0;
587 commTimeouts.WriteTotalTimeoutConstant = 0;
588 commTimeouts.WriteTotalTimeoutMultiplier = 0;
591 SetCommTimeouts(
m_handle, &commTimeouts);
623 "L1: waitForData result: read %u of %u bytes\n",
length[0],
maxLength);
637 if (written ==
nullptr) written = &bytes;
647 if (tx_log ==
nullptr)
651 tx_log =
fopen(fname,
"wb");
653 fwrite(
data, 1, *written, tx_log);
809 if (realpath(filename,
m_filename) ==
nullptr)
837 m_handle = _wfopen(filename, L
"w+b");
888 while (remaining > 0)
890 if (remaining >= 512)
916 if (rv != 0) eno = errno;
946 const char* needle = (
const char*)needleV;
961 while (readBytes > 0)
966 while (bufferPos < readBytes && needlePos < needleLength)
968 if (
buffer[bufferPos] == needle[needlePos])
977 else if (
buffer[bufferPos] == needle[0])
985 if (needlePos < needleLength)
1061 size_t read1, read2;
1064 char* buffer1 = (
char*)malloc(bsize);
1065 char* buffer2 = (
char*)malloc(bsize);
1072 read1 = fread(buffer1, 1, bsize,
m_handle);
1074 read1 = fread(buffer1, 1, (
size_t)remaining,
m_handle);
1079 while (remaining > 0)
1089 read1 = fread(buffer1, 1, bsize,
m_handle);
1091 read1 = fread(buffer1, 1, (
size_t)remaining,
m_handle);
1098 wPos += fwrite(buffer2, 1, read2,
m_handle);
1103 wPos += fwrite(buffer1, 1, read1,
m_handle);
1117 const char* filename,
const bool create,
const bool readOnly)
1148 if (realpath(filename,
m_filename) ==
nullptr)
1170 const wchar_t* filename,
const bool create,
const bool readOnly)
1179 m_handle = _wfopen(filename, L
"rb");
1181 m_handle = _wfopen(filename, L
"r+b");
1185 m_handle = _wfopen(filename, L
"w+b");
1188 m_handle = _wfopen(filename, L
"rb");
1250 char*
data = (
char*)dataV;
int32_t m_onBytesReceivedInstance
Custom, user supplied parameter for the OnBytesReceived callback function, passed as the first argume...
Operation was performed successfully.
XsensResultValue waitForData(const uint32_t maxLength, uint8_t *data, uint32_t *length=nullptr)
Wait for data to arrive or a timeout to occur.
GLuint GLuint GLsizei count
Cmt1s()
Default constructor, initializes all members to their default values.
XsensResultValue appendData(const uint32_t length, const void *data)
Write data to the end of the file.
XsensResultValue setReadPos(const CmtFilePos pos)
Set the new absolute read position.
XsensResultValue writeData(const uint32_t length, const uint8_t *data, uint32_t *written)
Write the data to the serial port.
XsensResultValue getName(char *filename) const
Retrieve the filename that was last successfully opened.
int void fclose(FILE *f)
An OS-independent version of fclose.
CmtCallbackFunction m_onBytesReceived
The bytes received function.
void gotoRead(void)
Change from writing to reading mode.
#define CMT_MAX_FILENAME_LENGTH
uint32_t m_baudrate
The baudrate that was last set to be used by the port.
XsensResultValue readData(const uint32_t maxLength, void *data, uint32_t *length)
Read data from the file and put it into the data buffer.
XsensResultValue writeData(const uint32_t length, const void *data)
Write data to the file.
XsensResultValue closeAndDelete(void)
Close the file and delete it.
XsensResultValue deleteData(const CmtFilePos start, const uint32_t length)
Delete the given data from the file. The function erases the given data from the file at the given wr...
XsensResultValue readData(const uint32_t maxLength, uint8_t *data, uint32_t *length=nullptr)
Read data from the serial port and put it into the data buffer.
CmtFilePos m_readPos
The last read position in the file.
XsensResultValue open(const char *filename, const bool create, const bool readOnly)
Open a file.
FILE * m_handle
The file handle.
XsensResultValue setTimeout(const uint32_t ms=CMT1_DEFAULT_TIMEOUT)
Set the default timeout value to use in blocking operations.
XsensResultValue insertData(const CmtFilePos start, const uint32_t length, const void *data)
Insert the given data into the file. The function writes the given data to the file at the current wr...
char m_filename[CMT_MAX_FILENAME_LENGTH]
Contains the name of the file that was last successfully opened.
XsensResultValue
Xsens return values.
XsensResultValue(__cdecl * CmtCallbackFunction)(int32_t, CmtCallbackSelector, void *, void *)
XsensResultValue find(const void *needle, const uint32_t needleLength, CmtFilePos &pos)
Find a string of bytes in the file.
XsensResultValue open(const char *portName, const uint32_t baudRate=CMT_DEFAULT_BAUD_RATE, uint32_t readBufSize=CMT_DEFAULT_READ_BUFFER_SIZE, uint32_t writeBufSize=CMT_DEFAULT_WRITE_BUFFER_SIZE)
Open a communcation channel to the given serial port name.
CmtFilePos m_fileSize
Contains the size of the file.
~Cmt1s()
Destructor, de-initializes, frees memory allocated for buffers, etc.
Cmt1f()
Default constructor, initializes all members to their default values.
wchar_t m_filename_w[CMT_MAX_FILENAME_LENGTH]
Contains the name of the file that was last successfully opened using unicode.
bool m_isOpen
Indicates if the file is open or not.
XsensResultValue close(void)
Close the file.
XsensResultValue escape(const CmtControlLine mask, const CmtControlLine state)
Manipulate the Serial control lines.
void gotoWrite(void)
Change from reading to writing mode.
bool m_unicode
Indicates if we're using the unicode filename or the regular filename.
int fprintf(FILE *fil, const char *format,...) noexcept MRPT_printf_format_check(2
An OS-independent version of fprintf.
#define CMT1_DEFAULT_TIMEOUT
The default timeout value for blocking CMT1s operations in ms.
bool m_isOpen
Indicates if the port is open or not.
CmtFilePos m_writePos
The last write position in the file.
XsensResultValue setCallbackFunction(CmtCallbackSelector tp, int32_t instance, CmtCallbackFunction func, void *param)
Set the callback function for when bytes have been received.
int int vsprintf(char *buf, size_t bufSize, const char *format, va_list args) noexcept
An OS-independent version of vsprintf (Notice the bufSize param, which may be ignored in some compile...
HANDLE m_handle
The serial port handle.
bool m_reading
Indicates whether the last operation was a read or write operation. This value is used to check wheth...
void * m_onBytesReceivedParam
Custom, user supplied parameter for the OnBytesReceived callback function, passed as the last argumen...
uint32_t m_endTime
The time at which an operation will end in ms, used by several functions.
GLuint GLsizei GLsizei * length
XsensResultValue m_lastResult
The last result of an operation.
The namespace of all Xsens software since 2006.
bool m_readOnly
Indicates if the file was opened in read-only mode.
FILE * fopen(const char *fileName, const char *mode) noexcept
An OS-independent version of fopen.
XsensResultValue create(const char *filename)
Open an empty file.
XsensResultValue close(void)
Close the serial communication port.
Callback function, called when bytes have been read from a port.
char * strcpy(char *dest, size_t destSize, const char *source) noexcept
An OS-independent version of strcpy.
uint8_t m_port
The opened COM port nr.
unsigned __int32 uint32_t
GLsizei GLsizei GLenum GLenum const GLvoid * data
uint32_t getTimeOfDay(tm *date_, time_t *secs_)
A platform-independent clock.
XsensResultValue setWritePos(const CmtFilePos pos=-1)
Set the new absolute write position.
uint8_t m_data[CMT_MAXMSGLEN]
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...
DCB m_commState
Stored settings about the serial port.
XsensResultValue flushData(void)
Flush all data to be written. This function writes any remaining data immediately and does not return...
XsensResultValue flushData(void)
Flush all data to be transmitted / received.
void memcpy(void *dest, size_t destSize, const void *src, size_t copyCount) noexcept
An OS and compiler independent version of "memcpy".
XsensResultValue m_lastResult
The last result of an operation.
#define MRPT_UNUSED_PARAM(a)
Determines whether this is an X86 or AMD64 platform.