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>
61 # define PATH_MAX CMT_MAX_FILENAME_LENGTH
68 #ifndef _CRT_SECURE_NO_DEPRECATE
69 # define _CRT_SECURE_NO_DEPRECATE
71 # pragma warning(disable:4996)
77 # define FSEEK(x) _fseeki64(m_handle, x, SEEK_SET)
78 # define FSEEK_R(x) _fseeki64(m_handle, x, SEEK_END)
79 # define FTELL() _ftelli64(m_handle)
81 # define FSEEK(x) fseek(m_handle, x, SEEK_SET)
82 # define FSEEK_R(x) fseek(m_handle, x, SEEK_END)
83 # define FTELL() ftell(m_handle)
86 # define FSEEK(x) fseeko(m_handle, x, SEEK_SET)
87 # define FSEEK_R(x) fseeko(m_handle, x, SEEK_END)
88 # define FTELL() ftello(m_handle)
95 int _wcsnicmp(
const wchar_t* s1,
const wchar_t* s2,
int count)
97 for (
int i = 0; i <
count; ++i, ++s1, ++s2)
116 #if defined(_DEBUG) || defined(_LOG_ALWAYS)
117 #if !defined(_LOG_TO_DBVIEW)
118 #ifdef _LOG_TO_STDOUT
120 FILE* debug_log_fp = NULL;
123 FILE* debug_qlog_fp = NULL;
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 != NULL)
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
185 m_onBytesReceived(NULL)
228 cto.ReadIntervalTimeout = MAXDWORD;
229 cto.ReadTotalTimeoutConstant = 0;
230 cto.ReadTotalTimeoutMultiplier = 0;
258 rv = EscapeCommFunction(
m_handle,SETDTR);
260 rv = EscapeCommFunction(
m_handle,CLRDTR);
266 rv = EscapeCommFunction(
m_handle,SETRTS);
268 rv = EscapeCommFunction(
m_handle,CLRRTS);
282 else status &= ~TIOCM_DTR;
293 else status &= ~TIOCM_RTS;
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);
344 m_handle = CreateFileA(winPortName, GENERIC_READ | GENERIC_WRITE, 0, NULL,
345 OPEN_EXISTING, 0, NULL);
346 if (
m_handle == INVALID_HANDLE_VALUE)
348 CMT1LOG(
"L1: Port cannot be opened\n");
378 m_port = atoi(&portName[3]);
384 EscapeCommFunction(
m_handle, SETRTS);
386 EscapeCommFunction(
m_handle, SETDTR);
387 SetupComm(
m_handle,readBufSize,writeBufSize);
391 PurgeComm(
m_handle, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR);
423 m_commState.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
425 m_commState.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
442 if (ioctl(
m_handle, TIOCMGET, &cmbits) < 0)
447 cmbits |= TIOCM_RTS|TIOCM_DTR;
449 if (ioctl(
m_handle, TIOCMSET, &cmbits) < 0)
467 char comFileName[32];
470 sprintf(comFileName,
"COM%u", portNumber);
472 return Cmt1s::open(comFileName, baudRate, readBufSize, writeBufSize);
497 #ifdef _LOG_CALLBACKS
505 CMT1LOG(
"L1: readData, ReadFile returned error %u\n",::GetLastError());
519 rx_log =
fopen(fname,
"wb");
525 CMT1LOG((
length[0]?
"L1: readData returned success, read %u of %u bytes, first: %02x\n":
"L1: readData returned success, read %u bytes\n"),
length[0],
maxLength,
data[0]);
547 CMT1LOG(
"L1: Setting timeout to %u ms\n",ms);
552 COMMTIMEOUTS commTimeouts;
554 GetCommTimeouts(
m_handle,&commTimeouts);
559 commTimeouts.ReadIntervalTimeout = 0;
560 commTimeouts.ReadTotalTimeoutConstant =
m_timeout;
561 commTimeouts.ReadTotalTimeoutMultiplier = 0;
562 commTimeouts.WriteTotalTimeoutConstant =
m_timeout;
563 commTimeouts.WriteTotalTimeoutMultiplier = 0;
568 commTimeouts.ReadIntervalTimeout = MAXDWORD;
569 commTimeouts.ReadTotalTimeoutConstant = 0;
570 commTimeouts.ReadTotalTimeoutMultiplier = 0;
571 commTimeouts.WriteTotalTimeoutConstant = 0;
572 commTimeouts.WriteTotalTimeoutMultiplier = 0;
575 SetCommTimeouts(
m_handle, &commTimeouts);
638 tx_log =
fopen(fname,
"wb");
640 fwrite(
data,1,*written,tx_log);
830 m_handle = _wfopen(filename, L
"w+b");
884 while (remaining > 0)
886 if (remaining >= 512)
943 const char* needle = (
const char*) needleV;
957 while (readBytes > 0)
962 while (bufferPos < readBytes && needlePos < needleLength)
964 if (
buffer[bufferPos] == needle[needlePos])
974 if (
buffer[bufferPos] == needle[0])
982 if (needlePos < needleLength)
1060 size_t read1, read2;
1063 char* buffer1 = (
char*) malloc(bsize);
1064 char* buffer2 = (
char*) malloc(bsize);
1071 read1 = fread(buffer1,1,bsize,
m_handle);
1073 read1 = fread(buffer1,1,(
size_t) remaining,
m_handle);
1078 while(remaining > 0)
1082 btemp = buffer1; buffer1 = buffer2; buffer2 = btemp;
1086 read1 = fread(buffer1,1,bsize,
m_handle);
1088 read1 = fread(buffer1,1,(
size_t) remaining,
m_handle);
1095 wPos += fwrite(buffer2, 1, read2,
m_handle);
1100 wPos += fwrite(buffer1, 1, read1,
m_handle);
1175 m_handle = _wfopen(filename, L
"rb");
1177 m_handle = _wfopen(filename, L
"r+b");
1181 m_handle = _wfopen(filename, L
"w+b");
1184 m_handle = _wfopen(filename, L
"rb");
1251 char*
data = (
char*) dataV;
void gotoRead(void)
Change from writing to reading mode.
XsensResultValue insertData(const CmtFilePos start, const uint32_t length, const void *data)
Insert the given data into the file.
XsensResultValue setReadPos(const CmtFilePos pos)
Set the new absolute read position.
char m_filename[CMT_MAX_FILENAME_LENGTH]
Contains the name of the file that was last successfully opened.
XsensResultValue deleteData(const CmtFilePos start, const uint32_t length)
Delete the given data from the file.
bool m_isOpen
Indicates if the file is open or not.
bool m_unicode
Indicates if we're using the unicode filename or the regular filename.
void gotoWrite(void)
Change from reading to writing mode.
bool m_reading
Indicates whether the last operation was a read or write operation.
XsensResultValue appendData(const uint32_t length, const void *data)
Write data to the end of the file.
wchar_t m_filename_w[CMT_MAX_FILENAME_LENGTH]
Contains the name of the file that was last successfully opened using unicode.
XsensResultValue closeAndDelete(void)
Close the file and delete it.
Cmt1f()
Default constructor, initializes all members to their default values.
XsensResultValue setWritePos(const CmtFilePos pos=-1)
Set the new absolute write position.
XsensResultValue m_lastResult
The last result of an operation.
XsensResultValue close(void)
Close the file.
FILE * m_handle
The file handle.
XsensResultValue writeData(const uint32_t length, const void *data)
Write data to the file.
XsensResultValue getName(char *filename) const
Retrieve the filename that was last successfully opened.
XsensResultValue open(const char *filename, const bool create, const bool readOnly)
Open a file.
bool m_readOnly
Indicates if the file was opened in read-only mode.
XsensResultValue find(const void *needle, const uint32_t needleLength, CmtFilePos &pos)
Find a string of bytes in the file.
CmtFilePos m_fileSize
Contains the size of the file.
XsensResultValue readData(const uint32_t maxLength, void *data, uint32_t *length)
Read data from the file and put it into the data buffer.
CmtFilePos m_readPos
The last read position in the file.
XsensResultValue flushData(void)
Flush all data to be written. This function writes any remaining data immediately and does not return...
CmtFilePos m_writePos
The last write position in the file.
XsensResultValue create(const char *filename)
Open an empty file.
Cmt1s()
Default constructor, initializes all members to their default values.
uint32_t m_endTime
The time at which an operation will end in ms, used by several functions.
XsensResultValue readData(const uint32_t maxLength, uint8_t *data, uint32_t *length=NULL)
Read data from the serial port and put it into the data buffer.
XsensResultValue close(void)
Close the serial communication port.
XsensResultValue setCallbackFunction(CmtCallbackSelector tp, int32_t instance, CmtCallbackFunction func, void *param)
Set the callback function for when bytes have been received.
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.
int32_t m_onBytesReceivedInstance
Custom, user supplied parameter for the OnBytesReceived callback function, passed as the first argume...
void * m_onBytesReceivedParam
Custom, user supplied parameter for the OnBytesReceived callback function, passed as the last argumen...
CmtCallbackFunction m_onBytesReceived
The bytes received function.
XsensResultValue escape(const CmtControlLine mask, const CmtControlLine state)
Manipulate the Serial control lines.
uint32_t m_baudrate
The baudrate that was last set to be used by the port.
XsensResultValue writeData(const uint32_t length, const uint8_t *data, uint32_t *written)
Write the data to the serial port.
XsensResultValue waitForData(const uint32_t maxLength, uint8_t *data, uint32_t *length=NULL)
Wait for data to arrive or a timeout to occur.
DCB m_commState
Stored settings about the serial port.
XsensResultValue flushData(void)
Flush all data to be transmitted / received.
HANDLE m_handle
The serial port handle.
uint8_t m_port
The opened COM port nr.
~Cmt1s()
Destructor, de-initializes, frees memory allocated for buffers, etc.
XsensResultValue setTimeout(const uint32_t ms=CMT1_DEFAULT_TIMEOUT)
Set the default timeout value to use in blocking operations.
bool m_isOpen
Indicates if the port is open or not.
XsensResultValue m_lastResult
The last result of an operation.
XsensResultValue(__cdecl * CmtCallbackFunction)(int32_t, CmtCallbackSelector, void *, void *)
#define CMT1_DEFAULT_TIMEOUT
The default timeout value for blocking CMT1s operations in ms.
@ CMT_CALLBACK_ONBYTESRECEIVED
Callback function, called when bytes have been read from a port.
#define CMT_MAX_FILENAME_LENGTH
GLsizei GLsizei GLenum GLenum const GLvoid * data
GLuint GLsizei GLsizei * length
GLuint GLuint GLsizei count
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...
int BASE_IMPEXP fprintf(FILE *fil, const char *format,...) MRPT_NO_THROWS MRPT_printf_format_check(2
An OS-independent version of fprintf.
void BASE_IMPEXP memcpy(void *dest, size_t destSize, const void *src, size_t copyCount) MRPT_NO_THROWS
An OS and compiler independent version of "memcpy".
int BASE_IMPEXP int BASE_IMPEXP vsprintf(char *buf, size_t bufSize, const char *format, va_list args) MRPT_NO_THROWS
An OS-independent version of vsprintf (Notice the bufSize param, which may be ignored in some compile...
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 MRPT_UNUSED_PARAM(a)
Can be used to avoid "not used parameters" warnings from the compiler.
The namespace of all Xsens software since 2006.
uint32_t getTimeOfDay(tm *date_, time_t *secs_)
A platform-independent clock.
unsigned __int32 uint32_t
uint8_t m_data[CMT_MAXMSGLEN]
XsensResultValue
Xsens return values.
@ XRV_BUSY
Busy processing, try again later.
@ 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_ENDOFFILE
End of file is reached.
@ XRV_INVALIDINSTANCE
Invalid instance called.
@ XRV_NOFILEOPEN
No file opened for reading/writing.
@ XRV_OUTPUTCANNOTBEOPENED
The specified i/o device can not be opened.
@ XRV_ERROR
A generic error occurred.
@ XRV_OUTOFMEMORY
No internal memory available.
@ XRV_TIMEOUT
A timeout occurred.
@ XRV_OK
Operation was performed successfully.
@ XRV_INVALIDPARAM
An invalid parameter is supplied.
@ XRV_READONLY
Tried to change a read-only value.