colab.cpp
Go to the documentation of this file.
00001 
00006 //
00007 // colab.cpp
00008 //
00009 // (c) 2010 SICK AG, Hamburg, Germany
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 //      UINT16 length = stringValue.length();
00033         strcpy((char*) buffer, stringValue.c_str());
00034 //      buffer += length;
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                         // found checksum field -> end of buffer reached.
00081                         nextData = 0; // indicates that there is no more data
00082                         break;
00083                 }
00084                 if (buffer[i] == 0x20)
00085                 {
00086                         // found identifier delimiter
00087                         nextData = i + 1; // points to next data field
00088                         break;
00089                 }
00090                 i++;
00091         }
00092         while (true);
00093 
00094         length = i - start; // last byte of identifier
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         // write header
00105         sendBuffer[pos++] = 0x02;
00106         sendBuffer[pos++] = 0x02;
00107         sendBuffer[pos++] = 0x02;
00108         sendBuffer[pos++] = 0x02;
00109         // Write payload length to buffer
00110         colab::addIntegerToBuffer<UINT32>(sendBuffer, pos, length + 1); // s counts to the payload length
00111         sendBuffer[pos++] = 's';
00112 
00113         // write telegram
00114         memcpy(&(sendBuffer[pos]), cmdBuffer, length);
00115         pos += length;
00116 
00117         // write checksum (of payload)
00118         UINT8 checksum = sendBuffer[8];
00119         for (int i = 9; i < pos; i++)
00120         {
00121                 checksum = checksum ^ sendBuffer[i]; // XOR
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);  // 8
00132 //      UINT8* buffer2 = buffer;
00133 //      T floatValue = memread<T>(buffer2);
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 } // END namespace colab


sick_scan
Author(s): Michael Lehning , Jochen Sprickerhof , Martin Günther
autogenerated on Tue Jul 9 2019 05:05:34