10 #include "../tools/toolbox.hpp" 11 #include "../tools/errorhandler.hpp" 26 if ((c >=
'0') && (c <=
'9'))
30 else if ((c >=
'A') && (c <=
'F'))
37 throw std::out_of_range(
"Unknown character where 0..9 or A..F was expected: '" + std::string(c, 1) +
"'");
54 throw std::out_of_range(
"Tried to convert value > 0x0f into hex-nibble: " +
toString((
INT16)value));
63 c = (
'A' + (value - 10));
82 sendBuffer[pos++] = 0x02;
83 sendBuffer[pos++] =
's';
86 memcpy(&(sendBuffer[pos]), cmdBuffer, *len);
95 sendBuffer[pos++] = 0x03;
139 bool firstZero =
true;
155 while (stellenwert > 0)
157 c = value / stellenwert;
158 if ((c != 0) || (firstZero ==
false) || (stellenwert == 1))
161 buffer[pos++] = (
'0' + c);
168 value -= c * stellenwert;
198 bool firstZero =
true;
202 for (
INT16 i = 7; i >= 0; i -= 1)
204 nibble = (value >> (i * 4)) & 0x0F;
205 if ((nibble != 0) || (firstZero ==
false) || (i == 0))
221 UINT16 len = text.length();
224 strcpy ((
char*)buffer, text.c_str());
231 std::string tmp(
toString(value, 6));
232 printWarning(
"detail::writeToBuffer: Warning - Writing of floating-point values has not been cross-checked in Cola-A format!");
243 typedef std::string::size_type size_type;
246 size_type tokenStart = rxData->find_first_not_of(
' ');
247 size_type tokenEnd = rxData->find(
' ', tokenStart);
248 size_type tokenLength = tokenEnd - tokenStart;
251 std::string token = rxData->substr(tokenStart, tokenLength);
254 *rxData = rxData->substr(tokenEnd + 1);
274 while (rxData[pos] != 0x00)
276 if (rxData[pos] !=
' ')
284 if (rxData[pos] != 0x00)
288 while ((rxData[pos] != 0x00) && (rxData[pos] !=
' '))
290 token += rxData[pos];
300 bool GetNibble(
unsigned char data,
unsigned char& rNibble)
303 bool validHexData =
true;
304 if ((data >=
'0') && (data <=
'9'))
306 rNibble = (
unsigned char)(data -
'0');
308 else if ((data >=
'A') && (data <=
'F'))
310 rNibble = (
unsigned char)(data -
'A' + 10);
312 else if ((data >=
'a') && (data <=
'f'))
314 rNibble = (
unsigned char)(data -
'a' + 10);
318 validHexData =
false;
329 double value = std::numeric_limits<double>::quiet_NaN();
331 if (text.empty() ==
false)
334 if ((text[0] ==
'+') || (text[0] ==
'-'))
337 value = atof(text.c_str());
348 memset(&converter, 0,
sizeof(converter));
350 if (text.length() == 8)
355 for (
int i = 7; i >= 0; --i)
357 unsigned char nibble;
359 converter.c[hexIndex] |= (nibble << shift);
360 hexIndex += (shift >> 2);
380 double value = std::numeric_limits<double>::quiet_NaN();
381 const std::string& text = rxData;
382 if (text.empty() ==
false)
385 if ((text[0] ==
'+') || (text[0] ==
'-'))
388 value = atof(text.c_str());
399 memset(&converter, 0,
sizeof(converter));
401 if (text.length() == 8)
406 for (
int i = 7; i >= 0; --i)
408 unsigned char nibble;
410 converter.c[hexIndex] |= (nibble << shift);
411 hexIndex += (shift >> 2);
440 if (number.at(0) ==
'+')
444 number = number.substr(1);
451 digits = number.length();
454 for (
INT16 d = digits - 1; d >= 0; d -= 1)
457 value += tempVal * factor;
458 factor *= baseFactor;
470 assert ((value >= -32768) && (value <= 32767));
483 INT32 baseFactor = 10;
491 if (number.at(0) ==
'+')
494 number = number.substr(1);
496 else if (number.at(0) ==
'-')
500 number = number.substr(1);
509 digits = number.length();
512 for (
INT16 d = digits - 1; d >= 0; d -= 1)
515 value += tempVal * factor;
516 factor *= baseFactor;
532 assert ((value >= -32768) && (value <= 32767));
545 INT32 baseFactor = 10;
551 const std::string number = rxData;
554 if (number.at(0) ==
'+')
560 else if (number.at(0) ==
'-')
574 digits = number.length();
577 for (
INT16 d = digits - 1; d >= offset; d -= 1)
580 value += tempVal * factor;
581 factor *= baseFactor;
611 text = rxData->substr(0, len);
613 *rxData = rxData->substr(len + 1);
637 assert (value < 0x10000);
650 assert (value < 256);
663 assert (value < 0x10000);
676 assert (value < 256);
695 const std::string& number = rxData;
698 if (number.at(0) ==
'+')
711 digits = number.length();
714 for (
INT16 d = digits - 1; d >= offset; d -= 1)
717 value += tempVal * factor;
718 factor *= baseFactor;
737 for (
UINT16 i = 0; i < len; i++)
740 assert (value < 256);
741 result += (value << (i * 8));
790 buffer[bufferLen-1] = 0x00;
791 std::string result = std::string((
char*)(&(buffer[2])));
UINT8 nibbleToAscii(UINT8 value)
std::string toString(const PositionWGS84::PositionWGS84SourceType &type)
std::string decodeString(std::string *rxData, UINT16 len)
INT16 decodeINT16(std::string *rxData)
std::string getNextStringToken(std::string *rxData)
std::string convertRxBufferToString(UINT8 *buffer, UINT16 bufferLen)
UINT16 addINT8ToBuffer(UINT8 *buffer, INT8 value)
UINT16 addUINT8ToBuffer(UINT8 *buffer, UINT8 value)
UINT8 decodeUINT8(std::string *rxData)
UINT32 decodeXByte(std::string *rxData, UINT16 len)
UINT16 addINT32ToBuffer(UINT8 *buffer, INT32 value)
void addFrameToBuffer(UINT8 *sendBuffer, UINT8 *cmdBuffer, UINT16 *len)
UINT16 addStringToBuffer(UINT8 *buffer, const std::string &text)
UINT16 writeToBuffer(BYTE *buffer, double value)
INT32 decodeINT32(std::string *rxData)
bool GetNibble(unsigned char data, unsigned char &rNibble)
UINT16 addUINT16ToBuffer(UINT8 *buffer, UINT16 value)
UINT16 addUINT32ToBuffer(UINT8 *buffer, UINT32 value)
double decodeReal(std::string *rxData)
void printWarning(std::string message)
UINT16 decodeUINT16(BYTE *buffer)
UINT32 decodeUINT32(std::string *rxData)
UINT16 getValueOfChar(UINT8 c)