9 #define _CRT_SECURE_NO_WARNINGS 31 if ((c >=
'0') && (c <=
'9'))
35 else if ((c >=
'A') && (c <=
'F'))
42 throw std::out_of_range(
"Unknown character where 0..9 or A..F was expected: '" + std::string(c, 1) +
"'");
59 throw std::out_of_range(
"Tried to convert value > 0x0f into hex-nibble: " +
toString((
INT16)value));
68 c = (
'A' + (value - 10));
87 sendBuffer[pos++] = 0x02;
88 sendBuffer[pos++] =
's';
91 memcpy(&(sendBuffer[pos]), cmdBuffer, *len);
100 sendBuffer[pos++] = 0x03;
144 bool firstZero =
true;
160 while (stellenwert > 0)
162 c = value / stellenwert;
163 if ((c != 0) || (firstZero ==
false) || (stellenwert == 1))
166 buffer[pos++] = (
'0' + c);
173 value -= c * stellenwert;
203 bool firstZero =
true;
207 for (
INT16 i = 7; i >= 0; i -= 1)
209 nibble = (value >> (i * 4)) & 0x0F;
210 if ((nibble != 0) || (firstZero ==
false) || (i == 0))
229 strcpy ((
char*)buffer, text.c_str());
236 std::string tmp(
toString(value, 6));
237 printWarning(
"detail::writeToBuffer: Warning - Writing of floating-point values has not been cross-checked in Cola-A format!");
248 typedef std::string::size_type size_type;
251 size_type tokenStart = rxData->find_first_not_of(
' ');
252 size_type tokenEnd = rxData->find(
' ', tokenStart);
253 size_type tokenLength = tokenEnd - tokenStart;
256 std::string token = rxData->substr(tokenStart, tokenLength);
259 *rxData = rxData->substr(tokenEnd + 1);
279 while (rxData[pos] != 0x00)
281 if (rxData[pos] !=
' ')
289 if (rxData[pos] != 0x00)
293 while ((rxData[pos] != 0x00) && (rxData[pos] !=
' '))
295 token += rxData[pos];
305 bool GetNibble(
unsigned char data,
unsigned char& rNibble)
308 bool validHexData =
true;
309 if ((data >=
'0') && (data <=
'9'))
311 rNibble = (
unsigned char)(data -
'0');
313 else if ((data >=
'A') && (data <=
'F'))
315 rNibble = (
unsigned char)(data -
'A' + 10);
317 else if ((data >=
'a') && (data <=
'f'))
319 rNibble = (
unsigned char)(data -
'a' + 10);
323 validHexData =
false;
334 double value = std::numeric_limits<double>::quiet_NaN();
336 if (text.empty() ==
false)
339 if ((text[0] ==
'+') || (text[0] ==
'-'))
342 value = atof(text.c_str());
353 memset(&converter, 0,
sizeof(converter));
355 if (text.length() == 8)
360 for (
int i = 7; i >= 0; --i)
362 unsigned char nibble;
364 converter.c[hexIndex] |= (nibble << shift);
365 hexIndex += (shift >> 2);
385 double value = std::numeric_limits<double>::quiet_NaN();
386 const std::string& text = rxData;
387 if (text.empty() ==
false)
390 if ((text[0] ==
'+') || (text[0] ==
'-'))
393 value = atof(text.c_str());
404 memset(&converter, 0,
sizeof(converter));
406 if (text.length() == 8)
411 for (
int i = 7; i >= 0; --i)
413 unsigned char nibble;
415 converter.c[hexIndex] |= (nibble << shift);
416 hexIndex += (shift >> 2);
445 if (number.at(0) ==
'+')
449 number = number.substr(1);
456 digits = (
UINT16)number.length();
459 for (
INT16 d = digits - 1;
d >= 0;
d -= 1)
462 value += tempVal * factor;
463 factor *= baseFactor;
475 assert ((value >= -32768) && (value <= 32767));
488 INT32 baseFactor = 10;
496 if (number.at(0) ==
'+')
499 number = number.substr(1);
501 else if (number.at(0) ==
'-')
505 number = number.substr(1);
514 digits = (
UINT16)number.length();
517 for (
INT16 d = digits - 1;
d >= 0;
d -= 1)
520 value += tempVal * factor;
521 factor *= baseFactor;
537 assert ((value >= -32768) && (value <= 32767));
550 INT32 baseFactor = 10;
556 const std::string number = rxData;
559 if (number.at(0) ==
'+')
565 else if (number.at(0) ==
'-')
579 digits = (
UINT16)number.length();
582 for (
INT16 d = digits - 1;
d >= offset;
d -= 1)
585 value += tempVal * factor;
586 factor *= baseFactor;
616 text = rxData->substr(0, len);
618 *rxData = rxData->substr(len + 1);
642 assert (value < 0x10000);
655 assert (value < 256);
668 assert (value < 0x10000);
681 assert (value < 256);
700 const std::string& number = rxData;
703 if (number.at(0) ==
'+')
716 digits = (
UINT16)number.length();
719 for (
INT16 d = digits - 1;
d >= offset;
d -= 1)
722 value += tempVal * factor;
723 factor *= baseFactor;
742 for (
UINT16 i = 0; i < len; i++)
745 assert (value < 256);
746 result += (value << (i * 8));
795 buffer[bufferLen-1] = 0x00;
796 std::string result = std::string((
char*)(&(buffer[2])));
UINT8 nibbleToAscii(UINT8 value)
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)