108 #ifdef _IMU_USE_XSENS_DEVICE_
112 static char THIS_FILE[] = __FILE__;
113 #define new DEBUG_NEW
119 #define CRTSCTS ((IHFLOW) | (OHFLOW))
170 #if (CLOCKS_PER_SEC != 1000)
171 clk /= (CLOCKS_PER_SEC / 1000);
177 gettimeofday(&tv, &tz);
178 clk = tv.tv_sec * 1000 + (tv.tv_usec / 1000);
207 char pchFileName[10];
209 sprintf(pchFileName,
"\\\\.\\COM%d", portNumber);
211 m_handle = CreateFile(pchFileName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
213 if (
m_handle == INVALID_HANDLE_VALUE)
226 dcb.BaudRate = baudrate;
227 dcb.Parity = NOPARITY;
229 dcb.StopBits = TWOSTOPBITS;
230 dcb.fDsrSensitivity = FALSE;
231 dcb.fOutxCtsFlow = FALSE;
232 dcb.fOutxDsrFlow = FALSE;
236 if (!SetCommState(
m_handle, (LPDCB)&dcb))
241 dcb.StopBits = ONESTOPBIT;
243 if (!SetCommState(
m_handle, (LPDCB)&dcb))
253 COMMTIMEOUTS CommTimeouts;
255 GetCommTimeouts(
m_handle, &CommTimeouts);
258 CommTimeouts.ReadTotalTimeoutConstant = 1;
259 CommTimeouts.ReadIntervalTimeout = MAXDWORD;
260 CommTimeouts.ReadTotalTimeoutMultiplier = MAXDWORD;
267 SetCommTimeouts(
m_handle, &CommTimeouts);
270 EscapeCommFunction(
m_handle, SETRTS);
271 SetupComm(
m_handle, inqueueSize, outqueueSize);
274 PurgeComm(
m_handle, PURGE_TXCLEAR | PURGE_RXCLEAR);
279 struct termios options;
282 sprintf(chPort,
"/dev/ttyS%d", (portNumber - 1));
283 m_handle = open(chPort, O_RDWR | O_NOCTTY);
303 cfsetispeed(&options, baudrate);
304 cfsetospeed(&options, baudrate);
307 options.c_cflag |= (CLOCAL | CREAD);
309 options.c_cflag &= ~(CSIZE | PARENB);
310 options.c_cflag |= CS8;
311 options.c_cflag |= CSTOPB;
313 options.c_cflag &= ~CRTSCTS;
314 options.c_lflag &= ~(
ECHO | ECHONL | ICANON | ISIG | IEXTEN);
316 options.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
318 options.c_oflag &= ~OPOST;
320 options.c_cc[VMIN] = 0;
321 options.c_cc[VTIME] = 5;
324 tcsetattr(
m_handle, TCSANOW, &options);
356 m_handle = CreateFile(portName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
358 if (
m_handle == INVALID_HANDLE_VALUE)
371 dcb.BaudRate = baudrate;
372 dcb.Parity = NOPARITY;
374 dcb.StopBits = TWOSTOPBITS;
375 dcb.fDsrSensitivity = FALSE;
376 dcb.fOutxCtsFlow = FALSE;
377 dcb.fOutxDsrFlow = FALSE;
381 if (!SetCommState(
m_handle, (LPDCB)&dcb))
386 dcb.StopBits = ONESTOPBIT;
388 if (!SetCommState(
m_handle, (LPDCB)&dcb))
398 COMMTIMEOUTS CommTimeouts;
400 GetCommTimeouts(
m_handle, &CommTimeouts);
403 CommTimeouts.ReadTotalTimeoutConstant = 1;
404 CommTimeouts.ReadIntervalTimeout = MAXDWORD;
405 CommTimeouts.ReadTotalTimeoutMultiplier = MAXDWORD;
411 SetCommTimeouts(
m_handle, &CommTimeouts);
414 EscapeCommFunction(
m_handle, SETRTS);
415 SetupComm(
m_handle, inqueueSize, outqueueSize);
418 PurgeComm(
m_handle, PURGE_TXCLEAR | PURGE_RXCLEAR);
422 struct termios options;
426 m_handle = open(portName, O_RDWR | O_NOCTTY);
446 cfsetispeed(&options, baudrate);
447 cfsetospeed(&options, baudrate);
450 options.c_cflag |= (CLOCAL | CREAD);
452 options.c_cflag &= ~(CSIZE | PARENB);
453 options.c_cflag |= CS8;
454 options.c_cflag |= CSTOPB;
456 options.c_cflag &= ~CRTSCTS;
457 options.c_lflag &= ~(
ECHO | ECHONL | ICANON | ISIG | IEXTEN);
459 options.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
461 options.c_oflag &= ~OPOST;
463 options.c_cc[VMIN] = 0;
464 options.c_cc[VTIME] = 5;
467 tcsetattr(
m_handle, TCSANOW, &options);
500 DWORD disposition = OPEN_ALWAYS;
502 if (createAlways ==
true)
504 disposition = CREATE_ALWAYS;
507 m_handle = CreateFile(fileName, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, disposition, 0, NULL);
509 if (
m_handle == INVALID_HANDLE_VALUE)
515 int openMode = O_RDWR | O_CREAT;
517 if (createAlways ==
true)
522 m_handle = open(fileName, openMode, S_IRWXU);
579 BOOL retval = ReadFile(
m_handle, msgBuffer, nBytesToRead, &nBytesRead, NULL);
592 const int nBytesRead =
read(
m_handle, msgBuffer, nBytesToRead);
669 WriteFile(
m_handle, msgBuffer, nBytesToWrite, &nBytesWritten, NULL);
670 return nBytesWritten;
688 PurgeComm(
m_handle, PURGE_TXCLEAR | PURGE_RXCLEAR);
709 EscapeCommFunction(
m_handle,
function);
724 SetupComm(
m_handle, inqueueSize, outqueueSize);
762 if (lseek(
m_handle, relPos, moveMethod) != -1)
789 if ((fileSize = GetFileSize(
m_handle, NULL)) != INVALID_FILE_SIZE)
803 fileSize = buf.st_size;
973 int nBytesToRead = 1;
978 unsigned char chCheckSum;
1007 if (nBytesRead > 0 && nBytesToRead > 0)
1009 if (nBytesToRead > nBytesRead)
1011 nOffset += nBytesRead;
1012 nBytesToRead -= nBytesRead;
1017 nOffset += nBytesToRead;
1018 nBytesRead -= nBytesToRead;
1024 if (nBytesToRead > 0)
1026 nBytesRead =
readData(msgBuffer + nOffset, nBytesToRead);
1033 nOffset += nBytesRead;
1034 nBytesToRead -= nBytesRead;
1057 if (msgBuffer[
IND_LEN] != 0xFF)
1060 nBytesToRead = (nMsgDataLen = msgBuffer[
IND_LEN]) + 1;
1082 memmove(msgBuffer, msgBuffer + i, nBytesRead);
1096 nMsgLen = nMsgDataLen + 5 + (msgBuffer[
IND_LEN] == 0xFF ? 2 : 0);
1098 for (
int i = 1 ; i < nMsgLen ; i++)
1100 chCheckSum += msgBuffer[i];
1103 if (chCheckSum == 0)
1113 *msgBufferLength = nMsgLen;
1120 for (
int i = 1 ; i < nMsgLen ; i++)
1124 nBytesRead = nMsgLen - i;
1125 memmove(msgBuffer, msgBuffer + i, nBytesRead);
1147 for (i = 1; i < nOffset ; i++)
1151 nBytesRead = nOffset - i - 1;
1152 memmove(msgBuffer + 1, msgBuffer + i + 1, nBytesRead);
1208 buffer[
IND_LEN] = dataValueLen;
1227 clock_t clkStart, clkOld;
1228 bool msgRead =
false;
1245 if (buffer[
IND_MID] == (mid + 1))
1309 buffer[
IND_LEN] = (
unsigned char) dataLen;
1315 buffer[
IND_LENEXTH] = (
unsigned char)(dataLen >> 8);
1316 buffer[
IND_LENEXTL] = (
unsigned char)(dataLen & 0x00FF);
1320 memcpy(&buffer[headerLength],
data, dataLen);
1333 bool msgRead =
false;
1334 clock_t clkStart, clkOld;
1351 if (buffer[
IND_MID] == (mid + 1))
1401 clock_t clkStart, clkOld;
1427 if (
data !=
nullptr && dataLen !=
nullptr)
1440 else if (dataLen !=
nullptr)
1506 if (buffer[
IND_MID] == (mid + 1))
1584 if (buffer[
IND_MID] == (mid + 1))
1659 if (buffer[
IND_MID] == (mid + 1))
1736 if (buffer[
IND_MID] == (mid + 1))
1814 if (buffer[
IND_MID] == (mid + 1))
1864 short CXsensMTiModule::reqSetting(
const unsigned char mid,
unsigned char dataIn[],
short dataInLen,
unsigned char dataOut[],
short& dataOutLen,
const unsigned char bid)
1886 buffer[
IND_LEN] = (
unsigned char) dataInLen;
1892 buffer[
IND_LENEXTH] = (
unsigned char)(dataInLen >> 8);
1893 buffer[
IND_LENEXTL] = (
unsigned char)(dataInLen & 0x00FF);
1897 memcpy(&buffer[headerLength], dataIn, dataInLen);
1909 if (buffer[
IND_MID] == (mid + 1))
1915 memcpy(dataOut, &buffer[
IND_DATA0], dataOutLen);
1998 if (buffer[
IND_MID] == (mid + 1))
2066 buffer[
IND_LEN] = (
unsigned char) valuelen;
2077 if (buffer[
IND_MID] == (mid + 1))
2134 buffer[
IND_LEN] = valuelen + 1;
2139 buffer[
IND_LEN] = (
unsigned char) valuelen;
2152 if (buffer[
IND_MID] == (mid + 1))
2212 if (buffer[
IND_MID] == (mid + 1))
2285 if (buffer[
IND_MID] == (mid + 1))
2360 if (buffer[
IND_MID] == (mid + 1))
2399 if (numDevices !=
nullptr)
2431 if (numDevices !=
nullptr)
2436 unsigned char masterDID[4];
2469 unsigned short _numDevices = 0;
2472 for (
unsigned int i = 0 ; i < _numDevices ; i++)
2485 if (numDevices !=
nullptr)
2487 *numDevices = _numDevices;
2567 unsigned long value;
2606 short CXsensMTiModule::getMode(
unsigned long& OutputMode,
unsigned long& OutputSettings,
unsigned short& dataLength,
const unsigned char bid)
2608 unsigned char nbid = (bid ==
BID_MASTER) ? 0 : bid;
2631 unsigned char nbid = bid;
2647 unsigned short dataLength = 0;
2746 unsigned char nbid = bid;
2828 unsigned char nbid = bid;
2863 for (
int i = 0 ; i < 3 ; i++)
2909 unsigned char nbid = bid;
3031 for (
int i = 0 ; i < nElements ; i++)
3040 for (
int i = 0 ; i < nElements ; i++)
3115 output[0] =
input[3];
3116 output[1] =
input[2];
3117 output[2] =
input[1];
3118 output[3] =
input[0];
3122 output[0] =
input[1];
3123 output[1] =
input[0];
3127 output[0] =
input[0];
3131 for (
int i = 0, j = length - 1 ; i < length ; i++, j--)
3133 output[j] =
input[i];
3147 unsigned char checkSum = 0;
3150 for (i = 1; i < msgBufferLength ; i++)
3152 checkSum += msgBuffer[i];
3155 msgBuffer[msgBufferLength] = -checkSum;
3167 unsigned char checkSum = 0;
3170 for (i = 1; i < msgBufferLength ; i++)
3172 checkSum += msgBuffer[i];