colab.cpp
Go to the documentation of this file.
1 //
2 // colab.cpp
3 //
4 // (c) 2010 SICK AG, Hamburg, Germany
5 
6 #include "colab.hpp"
7 #include <cstring>
8 #include <cassert>
9 #include <stdexcept>
10 #include <stdlib.h>
11 #include <limits>
12 
13 namespace colab
14 {
15 
16 void addStringToBuffer(UINT8* buffer, UINT16& pos, const std::string& stringValue)
17 {
18  UINT16 length = stringValue.length();
19  strcpy((char*) &buffer[pos], stringValue.c_str());
20  pos += length;
21 }
22 
23 
24 
25 void addStringToBuffer(BYTE* buffer, const std::string& stringValue)
26 {
27 // UINT16 length = stringValue.length();
28  strcpy((char*) buffer, stringValue.c_str());
29 // buffer += length;
30 }
31 
32 
33 
34 std::string getStringFromBuffer(UINT8* buffer, UINT16& pos, UINT16 length)
35 {
36  UINT16 start = pos;
37  pos += length;
38  return std::string((char *) &buffer[start], length);
39 }
40 
41 
42 
43 std::string getStringFromBuffer(BYTE*& buffer, UINT16 length)
44 {
45  std::string str((char *) &buffer[0], length);
46  buffer += length;
47  return str;
48 }
49 
50 
51 
52 std::string getCommandStringFromBuffer(UINT8* buffer)
53 {
54  return std::string((char*) &buffer[9], 2);
55 }
56 
57 
58 
59 std::string getIdentifierFromBuffer(UINT8* buffer, UINT16& nextData, UINT16 bufferLength)
60 {
61  UINT16 start;
62  UINT16 length;
63 
64  if (buffer[11] == 0x20)
65  {
66  start = 12;
67  }
68  else
69  {
70  start = 11;
71  }
72 
73  int i = start;
74  do
75  {
76  if (i == bufferLength - 2)
77  {
78  // found checksum field -> end of buffer reached.
79  nextData = 0; // indicates that there is no more data
80  break;
81  }
82  if (buffer[i] == 0x20)
83  {
84  // found identifier delimiter
85  nextData = i + 1; // points to next data field
86  break;
87  }
88  i++;
89  }
90  while (true);
91 
92  length = i - start; // last byte of identifier
93 
94  return std::string((char*) &buffer[start], length);
95 }
96 
97 
98 
99 void addFrameToBuffer(UINT8* sendBuffer, UINT8* cmdBuffer, UINT16* len)
100 {
101  UINT16 pos = 0;
102  UINT32 length = *len;
103 
104  // write header
105  sendBuffer[pos++] = 0x02;
106  sendBuffer[pos++] = 0x02;
107  sendBuffer[pos++] = 0x02;
108  sendBuffer[pos++] = 0x02;
109  // Write payload length to buffer
110  colab::addIntegerToBuffer<UINT32>(sendBuffer, pos, length + 1); // s counts to the payload length
111  sendBuffer[pos++] = 's';
112 
113  // write telegram
114  memcpy(&(sendBuffer[pos]), cmdBuffer, length);
115  pos += length;
116 
117  // write checksum (of payload)
118  UINT8 checksum = sendBuffer[8];
119  for (int i = 9; i < pos; i++)
120  {
121  checksum = checksum ^ sendBuffer[i]; // XOR
122  }
123  colab::addIntegerToBuffer<UINT8>(sendBuffer, pos, checksum);
124 
125  *len = pos;
126 }
127 
128 //
129 // Returns the requested value. pos points then to the first byte of the next data field.
130 //
131 double getDoubleFromBuffer(UINT8* buffer, UINT16& pos)
132 {
133  UINT16 width = sizeof(double); // 8
134 // UINT8* buffer2 = buffer;
135 // T floatValue = memread<T>(buffer2);
136  double* valuePtr = (double*)buffer;
137  double value = *valuePtr;
138 
139  pos += width;
140  return value;
141 }
142 
143 
145 {
146  UINT16 value = (((UINT16)buffer[0]) << 8) +
147  ((UINT16)buffer[1]);
148  return value;
149 }
150 
151 
152 } // END namespace colab
unsigned char BYTE
uint16_t UINT16
UINT16 decodeUINT16(BYTE *buffer)
Definition: colab.cpp:144
std::string getStringFromBuffer(UINT8 *buffer, UINT16 &pos, UINT16 length)
Definition: colab.cpp:34
uint32_t UINT32
void addStringToBuffer(UINT8 *buffer, UINT16 &pos, const std::string &stringValue)
Definition: colab.cpp:16
Definition: colab.cpp:13
void addFrameToBuffer(UINT8 *sendBuffer, UINT8 *cmdBuffer, UINT16 *len)
Definition: colab.cpp:99
std::string getCommandStringFromBuffer(UINT8 *buffer)
Definition: colab.cpp:52
std::string getIdentifierFromBuffer(UINT8 *buffer, UINT16 &nextData, UINT16 bufferLength)
Definition: colab.cpp:59
double getDoubleFromBuffer(UINT8 *buffer, UINT16 &pos)
Definition: colab.cpp:131
uint8_t UINT8


libsick_ldmrs
Author(s): SICK AG , Martin Günther , Jochen Sprickerhof
autogenerated on Mon Oct 26 2020 03:27:29