Go to the documentation of this file.00001
00006
00007
00008
00009
00010 #define _CRT_SECURE_NO_WARNINGS
00011 #include "sick_scan/tcp/colab.hpp"
00012 #include <cstring>
00013 #include <cassert>
00014 #include <stdexcept>
00015 #include <stdlib.h>
00016 #include <limits>
00017
00018 namespace colab
00019 {
00020
00021 void addStringToBuffer(UINT8* buffer, UINT16& pos, const std::string& stringValue)
00022 {
00023 UINT16 length = (UINT16)stringValue.length();
00024 strcpy((char*) &buffer[pos], stringValue.c_str());
00025 pos += length;
00026 }
00027
00028
00029
00030 void addStringToBuffer(BYTE* buffer, const std::string& stringValue)
00031 {
00032
00033 strcpy((char*) buffer, stringValue.c_str());
00034
00035 }
00036
00037
00038
00039 std::string getStringFromBuffer(UINT8* buffer, UINT16& pos, UINT16 length)
00040 {
00041 UINT16 start = pos;
00042 pos += length;
00043 return std::string((char *) &buffer[start], length);
00044 }
00045
00046
00047
00048 std::string getStringFromBuffer(BYTE*& buffer, UINT16 length)
00049 {
00050 std::string str((char *) &buffer[0], length);
00051 buffer += length;
00052 return str;
00053 }
00054
00055
00056 std::string getCommandStringFromBuffer(UINT8* buffer)
00057 {
00058 return std::string((char*) &buffer[9], 2);
00059 }
00060
00061 std::string getIdentifierFromBuffer(UINT8* buffer, UINT16& nextData, UINT16 bufferLength)
00062 {
00063 UINT16 start;
00064 UINT16 length;
00065
00066 if (buffer[11] == 0x20)
00067 {
00068 start = 12;
00069 }
00070 else
00071 {
00072 start = 11;
00073 }
00074
00075 int i = start;
00076 do
00077 {
00078 if (i == bufferLength - 2)
00079 {
00080
00081 nextData = 0;
00082 break;
00083 }
00084 if (buffer[i] == 0x20)
00085 {
00086
00087 nextData = i + 1;
00088 break;
00089 }
00090 i++;
00091 }
00092 while (true);
00093
00094 length = i - start;
00095
00096 return std::string((char*) &buffer[start], length);
00097 }
00098
00099 void addFrameToBuffer(UINT8* sendBuffer, UINT8* cmdBuffer, UINT16* len)
00100 {
00101 UINT16 pos = 0;
00102 UINT32 length = *len;
00103
00104
00105 sendBuffer[pos++] = 0x02;
00106 sendBuffer[pos++] = 0x02;
00107 sendBuffer[pos++] = 0x02;
00108 sendBuffer[pos++] = 0x02;
00109
00110 colab::addIntegerToBuffer<UINT32>(sendBuffer, pos, length + 1);
00111 sendBuffer[pos++] = 's';
00112
00113
00114 memcpy(&(sendBuffer[pos]), cmdBuffer, length);
00115 pos += length;
00116
00117
00118 UINT8 checksum = sendBuffer[8];
00119 for (int i = 9; i < pos; i++)
00120 {
00121 checksum = checksum ^ sendBuffer[i];
00122 }
00123 colab::addIntegerToBuffer<UINT8>(sendBuffer, pos, checksum);
00124
00125 *len = pos;
00126 }
00127
00128
00129 double getDoubleFromBuffer(UINT8* buffer, UINT16& pos)
00130 {
00131 UINT16 width = sizeof(double);
00132
00133
00134 double* valuePtr = (double*)buffer;
00135 double value = *valuePtr;
00136
00137 pos += width;
00138 return value;
00139 }
00140
00141
00142 UINT16 decodeUINT16(BYTE* buffer)
00143 {
00144 UINT16 value = (((UINT16)buffer[0]) << 8) +
00145 ((UINT16)buffer[1]);
00146 return value;
00147 }
00148
00149
00150 }