108 #ifdef _IMU_USE_XSENS_DEVICE_
112 static char THIS_FILE[] = __FILE__;
113 #define new DEBUG_NEW
119 #define CRTSCTS ((IHFLOW) | (OHFLOW))
171 #if (CLOCKS_PER_SEC != 1000)
172 clk /= (CLOCKS_PER_SEC / 1000);
178 gettimeofday(&tv, &tz);
179 clk = tv.tv_sec * 1000 + (tv.tv_usec / 1000);
200 const unsigned long baudrate,
201 const unsigned long ,
202 const unsigned long )
212 char pchFileName[10];
214 sprintf(pchFileName,
"\\\\.\\COM%d", portNumber);
217 CreateFile(pchFileName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
219 if (
m_handle == INVALID_HANDLE_VALUE)
232 dcb.BaudRate = baudrate;
233 dcb.Parity = NOPARITY;
235 dcb.StopBits = TWOSTOPBITS;
236 dcb.fDsrSensitivity = FALSE;
237 dcb.fOutxCtsFlow = FALSE;
238 dcb.fOutxDsrFlow = FALSE;
242 if (!SetCommState(
m_handle, (LPDCB)&dcb))
247 dcb.StopBits = ONESTOPBIT;
249 if (!SetCommState(
m_handle, (LPDCB)&dcb))
259 COMMTIMEOUTS CommTimeouts;
261 GetCommTimeouts(
m_handle, &CommTimeouts);
264 CommTimeouts.ReadTotalTimeoutConstant = 1;
265 CommTimeouts.ReadIntervalTimeout = MAXDWORD;
266 CommTimeouts.ReadTotalTimeoutMultiplier = MAXDWORD;
273 SetCommTimeouts(
m_handle, &CommTimeouts);
276 EscapeCommFunction(
m_handle, SETRTS);
277 SetupComm(
m_handle, inqueueSize, outqueueSize);
280 PurgeComm(
m_handle, PURGE_TXCLEAR | PURGE_RXCLEAR);
285 struct termios options;
288 sprintf(chPort,
"/dev/ttyS%d", (portNumber - 1));
289 m_handle = open(chPort, O_RDWR | O_NOCTTY);
309 cfsetispeed(&options, baudrate);
310 cfsetospeed(&options, baudrate);
313 options.c_cflag |= (CLOCAL | CREAD);
315 options.c_cflag &= ~(CSIZE | PARENB);
316 options.c_cflag |= CS8;
317 options.c_cflag |= CSTOPB;
319 options.c_cflag &= ~CRTSCTS;
320 options.c_lflag &= ~(
ECHO | ECHONL | ICANON | ISIG | IEXTEN);
322 options.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
324 options.c_oflag &= ~OPOST;
326 options.c_cc[VMIN] = 0;
327 options.c_cc[VTIME] = 5;
330 tcsetattr(
m_handle, TCSANOW, &options);
354 const unsigned long baudrate,
355 const unsigned long ,
356 const unsigned long )
367 CreateFile(portName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
369 if (
m_handle == INVALID_HANDLE_VALUE)
382 dcb.BaudRate = baudrate;
383 dcb.Parity = NOPARITY;
385 dcb.StopBits = TWOSTOPBITS;
386 dcb.fDsrSensitivity = FALSE;
387 dcb.fOutxCtsFlow = FALSE;
388 dcb.fOutxDsrFlow = FALSE;
392 if (!SetCommState(
m_handle, (LPDCB)&dcb))
397 dcb.StopBits = ONESTOPBIT;
399 if (!SetCommState(
m_handle, (LPDCB)&dcb))
409 COMMTIMEOUTS CommTimeouts;
411 GetCommTimeouts(
m_handle, &CommTimeouts);
414 CommTimeouts.ReadTotalTimeoutConstant = 1;
415 CommTimeouts.ReadIntervalTimeout = MAXDWORD;
416 CommTimeouts.ReadTotalTimeoutMultiplier = MAXDWORD;
422 SetCommTimeouts(
m_handle, &CommTimeouts);
425 EscapeCommFunction(
m_handle, SETRTS);
426 SetupComm(
m_handle, inqueueSize, outqueueSize);
429 PurgeComm(
m_handle, PURGE_TXCLEAR | PURGE_RXCLEAR);
433 struct termios options;
437 m_handle = open(portName, O_RDWR | O_NOCTTY);
457 cfsetispeed(&options, baudrate);
458 cfsetospeed(&options, baudrate);
461 options.c_cflag |= (CLOCAL | CREAD);
463 options.c_cflag &= ~(CSIZE | PARENB);
464 options.c_cflag |= CS8;
465 options.c_cflag |= CSTOPB;
467 options.c_cflag &= ~CRTSCTS;
468 options.c_lflag &= ~(
ECHO | ECHONL | ICANON | ISIG | IEXTEN);
470 options.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
472 options.c_oflag &= ~OPOST;
474 options.c_cc[VMIN] = 0;
475 options.c_cc[VTIME] = 5;
478 tcsetattr(
m_handle, TCSANOW, &options);
512 DWORD disposition = OPEN_ALWAYS;
514 if (createAlways ==
true)
516 disposition = CREATE_ALWAYS;
520 fileName, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, disposition, 0, NULL);
522 if (
m_handle == INVALID_HANDLE_VALUE)
528 int openMode = O_RDWR | O_CREAT;
530 if (createAlways ==
true)
535 m_handle = open(fileName, openMode, S_IRWXU);
594 BOOL retval = ReadFile(
m_handle, msgBuffer, nBytesToRead, &nBytesRead, NULL);
607 const int nBytesRead =
read(
m_handle, msgBuffer, nBytesToRead);
685 WriteFile(
m_handle, msgBuffer, nBytesToWrite, &nBytesWritten, NULL);
686 return nBytesWritten;
705 PurgeComm(
m_handle, PURGE_TXCLEAR | PURGE_RXCLEAR);
727 EscapeCommFunction(
m_handle,
function);
739 const unsigned long )
744 SetupComm(
m_handle, inqueueSize, outqueueSize);
783 if (lseek(
m_handle, relPos, moveMethod) != -1)
811 if ((fileSize = GetFileSize(
m_handle, NULL)) != INVALID_FILE_SIZE)
825 fileSize = buf.st_size;
894 unsigned char data[],
1002 int nBytesToRead = 1;
1005 int nMsgDataLen = 0;
1007 unsigned char chCheckSum;
1008 bool Synced =
false;
1036 if (nBytesRead > 0 && nBytesToRead > 0)
1038 if (nBytesToRead > nBytesRead)
1040 nOffset += nBytesRead;
1041 nBytesToRead -= nBytesRead;
1046 nOffset += nBytesToRead;
1047 nBytesRead -= nBytesToRead;
1053 if (nBytesToRead > 0)
1055 nBytesRead =
readData(msgBuffer + nOffset, nBytesToRead);
1062 nOffset += nBytesRead;
1063 nBytesToRead -= nBytesRead;
1086 if (msgBuffer[
IND_LEN] != 0xFF)
1090 (nMsgDataLen = msgBuffer[
IND_LEN]) + 1;
1102 nBytesToRead = (nMsgDataLen = msgBuffer[
IND_LENEXTH] * 256 +
1114 memmove(msgBuffer, msgBuffer + i, nBytesRead);
1128 nMsgLen = nMsgDataLen + 5 + (msgBuffer[
IND_LEN] == 0xFF ? 2 : 0);
1130 for (
int i = 1; i < nMsgLen; i++)
1132 chCheckSum += msgBuffer[i];
1135 if (chCheckSum == 0)
1145 *msgBufferLength = nMsgLen;
1153 int i = 1; i < nMsgLen;
1158 nBytesRead = nMsgLen - i;
1159 memmove(msgBuffer, msgBuffer + i, nBytesRead);
1180 for (i = 1; i < nOffset; i++)
1184 nBytesRead = nOffset - i - 1;
1185 memmove(msgBuffer + 1, msgBuffer + i + 1, nBytesRead);
1230 const unsigned long dataValue,
1231 const unsigned char dataValueLen,
1232 const unsigned char bid)
1245 buffer[
IND_LEN] = dataValueLen;
1264 clock_t clkStart, clkOld;
1265 bool msgRead =
false;
1282 if (buffer[
IND_MID] == (mid + 1))
1330 const unsigned char data[],
1331 const unsigned short& dataLen,
1332 const unsigned char bid)
1350 buffer[
IND_LEN] = (
unsigned char)dataLen;
1356 buffer[
IND_LENEXTH] = (
unsigned char)(dataLen >> 8);
1357 buffer[
IND_LENEXTL] = (
unsigned char)(dataLen & 0x00FF);
1361 memcpy(&buffer[headerLength],
data, dataLen);
1374 bool msgRead =
false;
1375 clock_t clkStart, clkOld;
1392 if (buffer[
IND_MID] == (mid + 1))
1439 unsigned char data[],
1446 clock_t clkStart, clkOld;
1472 if (
data !=
nullptr && dataLen !=
nullptr)
1485 else if (dataLen !=
nullptr)
1525 unsigned long&
value,
1526 const unsigned char bid)
1554 if (buffer[
IND_MID] == (mid + 1))
1596 const unsigned char param,
1597 unsigned long&
value,
1598 const unsigned char bid)
1636 if (buffer[
IND_MID] == (mid + 1))
1712 if (buffer[
IND_MID] == (mid + 1))
1753 const unsigned char param,
1755 const unsigned char bid)
1793 if (buffer[
IND_MID] == (mid + 1))
1843 unsigned char data[],
1845 const unsigned char bid)
1875 if (buffer[
IND_MID] == (mid + 1))
1927 unsigned char dataIn[],
1929 unsigned char dataOut[],
1931 const unsigned char bid)
1953 buffer[
IND_LEN] = (
unsigned char)dataInLen;
1959 buffer[
IND_LENEXTH] = (
unsigned char)(dataInLen >> 8);
1960 buffer[
IND_LENEXTL] = (
unsigned char)(dataInLen & 0x00FF);
1964 memcpy(&buffer[headerLength], dataIn, dataInLen);
1976 if (buffer[
IND_MID] == (mid + 1))
1982 memcpy(dataOut, &buffer[
IND_DATA0], dataOutLen);
2027 const unsigned char param,
2028 unsigned char data[],
2030 const unsigned char bid)
2070 if (buffer[
IND_MID] == (mid + 1))
2121 const unsigned long value,
2122 const unsigned short valuelen,
2123 const unsigned char bid)
2142 buffer[
IND_LEN] = (
unsigned char)valuelen;
2153 if (buffer[
IND_MID] == (mid + 1))
2189 const unsigned char param,
2190 const unsigned long value,
2191 const unsigned short valuelen,
2192 const unsigned char bid)
2215 buffer[
IND_LEN] = valuelen + 1;
2220 buffer[
IND_LEN] = (
unsigned char)valuelen;
2233 if (buffer[
IND_MID] == (mid + 1))
2294 if (buffer[
IND_MID] == (mid + 1))
2328 const unsigned char param,
2330 const unsigned char bid)
2371 if (buffer[
IND_MID] == (mid + 1))
2406 const unsigned char param,
2409 const unsigned char bid)
2451 if (buffer[
IND_MID] == (mid + 1))
2491 if (numDevices !=
nullptr)
2527 if (numDevices !=
nullptr)
2532 unsigned char masterDID[4];
2565 unsigned short _numDevices = 0;
2569 for (
unsigned int i = 0; i < _numDevices; i++)
2585 if (numDevices !=
nullptr)
2587 *numDevices = _numDevices;
2631 unsigned long OutputSettings,
2632 const unsigned char bid)
2671 unsigned long value;
2713 unsigned long& OutputSettings,
2714 unsigned short& dataLength,
2715 const unsigned char bid)
2717 unsigned char nbid = (bid ==
BID_MASTER) ? 0 : bid;
2740 unsigned long OutputSettings,
2741 const unsigned char bid)
2743 unsigned char nbid = bid;
2759 unsigned short dataLength = 0;
2860 unsigned short&
value,
2861 const unsigned char data[],
2862 const unsigned char bid)
2865 unsigned char nbid = bid;
2902 (
unsigned char*)&
value,
2953 unsigned short value[],
2954 const unsigned char data[],
2955 const unsigned char bid)
2958 unsigned char nbid = bid;
2997 for (
int i = 0; i < 3; i++)
3044 const unsigned char data[],
3045 const unsigned char bid)
3049 unsigned char nbid = bid;
3146 unsigned long orientmode =
3188 for (
int i = 0; i < nElements; i++)
3199 for (
int i = 0; i < nElements; i++)
3274 unsigned char output[],
3280 output[0] =
input[3];
3281 output[1] =
input[2];
3282 output[2] =
input[1];
3283 output[3] =
input[0];
3287 output[0] =
input[1];
3288 output[1] =
input[0];
3292 output[0] =
input[0];
3296 for (
int i = 0, j = length - 1; i < length; i++, j--)
3298 output[j] =
input[i];
3313 unsigned char checkSum = 0;
3316 for (i = 1; i < msgBufferLength; i++)
3318 checkSum += msgBuffer[i];
3321 msgBuffer[msgBufferLength] = -checkSum;
3334 unsigned char checkSum = 0;
3337 for (i = 1; i < msgBufferLength; i++)
3339 checkSum += msgBuffer[i];