71 "",
"<SOH>",
"<STX>",
"<ETX>",
"<EOT>",
"<ENQ>",
"<ACK>",
"<BEL>",
"\b",
"<HT>",
"\n",
"<VT>",
"<FF>",
"\n",
72 "<SO>",
"<SI>",
"<DLE>",
"<DC1>",
"<DC2>",
"<DC3>",
"<DC4>",
"<NAK>",
"<SYN>",
"<ETB>",
"<CAN>",
"<EM>",
"<SUB>",
"<ESC>",
"<FS>",
"<GS>",
"<RS>",
"<US>",
73 " ",
"!",
"\"",
"#",
"$",
"%",
"&",
"'",
"(",
")",
"*",
"+",
",",
"-",
".",
"/",
74 "0",
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
":",
";",
"<",
"=",
">",
"?",
"@",
75 "A",
"B",
"C",
"D",
"E",
"F",
"G",
"H",
"I",
"J",
"K",
"L",
"M",
"N",
"O",
"P",
"Q",
"R",
"S",
"T",
"U",
"V",
"W",
"X",
"Y",
"Z",
"[",
"\\",
"]",
"^",
"_",
"`",
76 "a",
"b",
"c",
"d",
"e",
"f",
"g",
"h",
"i",
"j",
"k",
"l",
"m",
"n",
"o",
"p",
"q",
"r",
"s",
"t",
"u",
"v",
"w",
"x",
"y",
"z",
"{",
"|",
"}",
"~",
"<DEL>",
77 "",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
78 "",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
79 "",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
80 "",
"",
"",
"",
"",
"",
"",
""
88 {
"<SOH>", 0x01}, {
"<STX>", 0x02}, {
"<ETX>", 0x03}, {
"<EOT>", 0x04}, {
"<ENQ>", 0x05}, {
"<ACK>", 0x06}, {
"<BEL>", 0x07}, {
"\b", 0x08}, {
"<HT>", 0x09},
89 {
"\n", 0x0A}, {
"<VT>", 0x0B}, {
"<FF>", 0x0C}, {
"\n", 0x0D}, {
"<SO>", 0x0E}, {
"<SI>", 0x0F}, {
"<DLE>", 0x10}, {
"<DC1>", 0x11}, {
"<DC2>", 0x12},
90 {
"<DC3>", 0x13}, {
"<DC4>", 0x14}, {
"<NAK>", 0x15}, {
"<SYN>", 0x16}, {
"<ETB>", 0x17}, {
"<CAN>", 0x18}, {
"<EM>", 0x19}, {
"<SUB>", 0x1A}, {
"<ESC>", 0x1B},
91 {
"<FS>", 0x1C}, {
"<GS>", 0x1D}, {
"<RS>", 0x1E}, {
"<US>", 0x1F}, {
" ", 0x20}, {
"!", 0x21}, {
"\"", 0x22}, {
"#", 0x23}, {
"$", 0x24}, {
"%", 0x25}, {
"&", 0x26},
92 {
"'", 0x27}, {
"(", 0x28}, {
")", 0x29}, {
"*", 0x2A}, {
"+", 0x2B}, {
",", 0x2C}, {
"-", 0x2D}, {
".", 0x2E}, {
"/", 0x2F},
93 {
"0", 0x30}, {
"1", 0x31}, {
"2", 0x32}, {
"3", 0x33}, {
"4", 0x34}, {
"5", 0x35}, {
"6", 0x36}, {
"7", 0x37}, {
"8", 0x38}, {
"9", 0x39}, {
":", 0x3A}, {
";", 0x3B}, {
"=", 0x3D}, {
"?", 0x3F}, {
"@", 0x40},
94 {
"A", 0x41}, {
"B", 0x42}, {
"C", 0x43}, {
"D", 0x44}, {
"E", 0x45}, {
"F", 0x46}, {
"G", 0x47}, {
"H", 0x48}, {
"I", 0x49}, {
"J", 0x4A}, {
"K", 0x4B}, {
"L", 0x4C}, {
"M", 0x4D}, {
"N", 0x4E}, {
"O", 0x4F},
95 {
"P", 0x50}, {
"Q", 0x51}, {
"R", 0x52}, {
"S", 0x53}, {
"T", 0x54}, {
"U", 0x55}, {
"V", 0x56}, {
"W", 0x57}, {
"X", 0x58}, {
"Y", 0x59}, {
"Z", 0x5A}, {
"[", 0x5B}, {
"\\", 0x5C}, {
"]", 0x5D}, {
"^", 0x5E}, {
"_", 0x5F}, {
"`", 0x60},
96 {
"a", 0x61}, {
"b", 0x62}, {
"c", 0x63}, {
"d", 0x64}, {
"e", 0x65}, {
"f", 0x66}, {
"g", 0x67}, {
"h", 0x68}, {
"i", 0x69}, {
"j", 0x6A}, {
"k", 0x6B}, {
"l", 0x6C}, {
"m", 0x6D}, {
"n", 0x6E}, {
"o", 0x6F},
97 {
"p", 0x70}, {
"q", 0x71}, {
"r", 0x72}, {
"s", 0x73}, {
"t", 0x74}, {
"u", 0x75}, {
"v", 0x76}, {
"w", 0x77}, {
"x", 0x78}, {
"y", 0x79}, {
"z", 0x7A}, {
"{", 0x7B}, {
"|", 0x7C}, {
"}", 0x7D}, {
"~", 0x7E}, {
"<DEL>", 0x7F}
108 for(std::vector<uint8_t>::const_iterator iter = cola_telegram.cbegin(); iter != cola_telegram.cend(); iter++)
126 for(
size_t char_cnt = 0; char_cnt < cola_telegram.size(); char_cnt++)
130 for(std::map<std::string, uint8_t>::const_iterator map_iter = s_ascii_map.cbegin(); map_iter != s_ascii_map.cend(); map_iter++)
132 if(strncmp(map_iter->first.c_str(), cola_telegram.c_str() + char_cnt, map_iter->first.size()) == 0)
135 char_cnt += (map_iter->first.size() - 1);
143 static uint8_t
CRC8XOR(uint8_t* msgBlock,
size_t len)
145 uint8_t xorVal = 0x00;
146 for (
size_t i = 0; i < len; i++)
147 xorVal ^= (msgBlock[i]);
169 ROS_ERROR_STREAM(
"## ERROR in ColaAsciiToColaBinary(): invalid SOPAS command type " << cola_msg.command_type);
170 return std::vector<uint8_t>();
172 return ColaTelegramToColaBinary(cola_msg, parameter_is_ascii);
187 std::vector<uint8_t> cola_binary;
188 cola_binary.reserve(64*1024);
189 const uint8_t binary_stx = 0x02;
190 const uint8_t binary_separator = 0x20;
194 std::vector<uint8_t> binary_payload;
195 binary_payload.reserve(64*1024);
196 for(
size_t n = 0; n < command_type.size(); n++)
197 binary_payload.push_back(command_type[n]);
198 binary_payload.push_back(binary_separator);
199 for(
size_t n = 0; n < cola_msg.command_name.size(); n++)
200 binary_payload.push_back(cola_msg.command_name[n]);
201 if(cola_msg.parameter.size() > 0)
203 binary_payload.push_back(binary_separator);
204 for(
size_t n = 0; n < cola_msg.parameter.size(); n++)
206 std::string parameter = cola_msg.parameter[n];
207 if(parameter_is_ascii != 0)
209 if ((parameter.size() % 2) != 0)
210 parameter =
"0" + cola_msg.parameter[n];
211 for (
size_t m = 1; m < parameter.size(); m += 2)
215 uint8_t val =
static_cast<uint8_t
>(std::stoul(parameter.substr(m - 1, 2), 0, 16) & 0xFF);
216 binary_payload.push_back(val);
218 catch (
const std::exception & exc)
220 if(parameter_is_ascii < 0)
222 return ColaTelegramToColaBinary(cola_msg, 0);
226 ROS_ERROR_STREAM(
"## ERROR in ColaTelegramToColaBinary(): can't convert parameter value " << parameter.substr(m - 1, 2) <<
" to hex, exception " << exc.what());
235 binary_payload.push_back(binary_separator);
236 for (
size_t m = 0; m < parameter.size(); m++)
238 uint8_t val =
static_cast<uint8_t
>(parameter[m] & 0xFF);
239 binary_payload.push_back(val);
246 for(
size_t n = 0; n < 4; n++)
247 cola_binary.push_back(binary_stx);
248 uint32_t payload_length = binary_payload.size();
249 for(
int n = 24; n >= 0; n-=8)
250 cola_binary.push_back((payload_length >> n) & 0xFF);
251 for(
size_t n = 0; n < binary_payload.size(); n++)
252 cola_binary.push_back(binary_payload[n]);
253 uint8_t checksum =
CRC8XOR(binary_payload.data(), binary_payload.size());
254 cola_binary.push_back(checksum);
269 const uint8_t binary_separator = 0x20;
272 if(!IsColaBinary(cola_telegram))
274 ROS_ERROR_STREAM(
"## ERROR in ColaBinaryToColaAscii(): cola telegram is not Cola-Binary");
277 uint32_t telegram_length = ColaBinaryTelegramLength(cola_telegram);
278 if(telegram_length != cola_telegram.size())
280 ROS_ERROR_STREAM(
"## ERROR in ColaBinaryToColaAscii(): invalid cola telegram length (" << cola_telegram.size() <<
" byte, expected " << telegram_length <<
" byte)");
284 std::vector<uint8_t> binary_payload;
285 binary_payload.reserve(cola_telegram.size());
286 for(
size_t n = 8; n < telegram_length - 1; n++)
287 binary_payload.push_back(cola_telegram[n]);
289 uint8_t checksum =
CRC8XOR(binary_payload.data(), binary_payload.size());
290 if(checksum != cola_telegram.back())
292 ROS_ERROR_STREAM(
"## ERROR in ColaBinaryToColaAscii(): invalid checksum (expected: " << checksum <<
", received: " << cola_telegram.back() <<
")");
296 size_t parameter_start_idx = 0;
297 while(parameter_start_idx < binary_payload.size() && binary_payload[parameter_start_idx] != binary_separator)
298 parameter_start_idx++;
299 parameter_start_idx++;
300 while(parameter_start_idx < binary_payload.size() && binary_payload[parameter_start_idx] != binary_separator)
301 parameter_start_idx++;
302 parameter_start_idx++;
308 for(
size_t n = 0; n < stx.size(); n++)
310 if(parameter_to_ascii)
313 for (
size_t n = 0; n < binary_payload.size() && n < parameter_start_idx; n++)
316 for (
size_t n = parameter_start_idx; n < binary_payload.size(); n++)
318 std::stringstream hexstring;
319 hexstring << std::hex << (uint32_t) binary_payload[n];
320 for (
size_t m = 0; m < hexstring.str().size(); m++)
327 for (
size_t n = 0; n < binary_payload.size(); n++)
331 for(
size_t n = 0; n < etx.size(); n++)
344 const uint8_t binary_stx = 0x02;
345 return cola_telegram.size() >= 4 && cola_telegram[0] == binary_stx && cola_telegram[1] == binary_stx
346 && cola_telegram[2] == binary_stx && cola_telegram[3] == binary_stx;
356 if(cola_telegram.size() >= 8 && IsColaBinary(cola_telegram))
358 uint32_t payload_length = 0;
359 for(
size_t n = 4; n < 8; n++)
361 payload_length = payload_length << 8;
362 payload_length |= ((cola_telegram[n]) & 0xFF);
364 return payload_length + 9;