Rev 985 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 985 | Rev 986 | ||
|---|---|---|---|
| Line 140... | Line 140... | ||
| 140 | } |
140 | } |
| 141 | else |
141 | else |
| 142 | { |
142 | { |
| 143 | return commandId; |
143 | return commandId; |
| 144 | } |
144 | } |
| - | 145 | } |
|
| - | 146 | } |
|
| - | 147 | ||
| - | 148 | return -1; |
|
| - | 149 | } |
|
| - | 150 | ||
| - | 151 | string CanIdTranslator::lookupNMTCommandName(int commandId) |
|
| - | 152 | { |
|
| - | 153 | XmlNode commandsNode = xmlNode.findChild("nmt_messages"); |
|
| - | 154 | vector<XmlNode> commandNodes = commandsNode.getChildren(); |
|
| - | 155 | ||
| - | 156 | for (int n = 0; n < commandNodes.size(); n++) |
|
| - | 157 | { |
|
| - | 158 | map<string, string> attributes = commandNodes[n].getAttributes(); |
|
| - | 159 | ||
| - | 160 | if (stoi(attributes["id"]) == commandId) |
|
| - | 161 | { |
|
| - | 162 | return attributes["name"]; |
|
| - | 163 | } |
|
| - | 164 | } |
|
| - | 165 | ||
| - | 166 | return ""; |
|
| - | 167 | } |
|
| - | 168 | ||
| - | 169 | int CanIdTranslator::resolveNMTCommandId(string commandName) |
|
| - | 170 | { |
|
| - | 171 | XmlNode commandsNode = xmlNode.findChild("nmt_messages"); |
|
| - | 172 | vector<XmlNode> commandNodes = commandsNode.getChildren(); |
|
| - | 173 | ||
| - | 174 | for (int n = 0; n < commandNodes.size(); n++) |
|
| - | 175 | { |
|
| - | 176 | map<string, string> attributes = commandNodes[n].getAttributes(); |
|
| - | 177 | ||
| - | 178 | if (attributes["name"].compare(commandName) == 0) |
|
| - | 179 | { |
|
| - | 180 | return stoi(attributes["id"]); |
|
| 145 | } |
181 | } |
| 146 | } |
182 | } |
| 147 | 183 | ||
| 148 | return -1; |
184 | return -1; |
| 149 | } |
185 | } |
| Line 221... | Line 257... | ||
| 221 | } |
257 | } |
| 222 | 258 | ||
| 223 | int CanIdTranslator::resolveModuleId(string moduleName) |
259 | int CanIdTranslator::resolveModuleId(string moduleName) |
| 224 | { |
260 | { |
| 225 | XmlNode modulesNode = xmlNode.findChild("modules"); |
261 | XmlNode modulesNode = xmlNode.findChild("modules"); |
| 226 | vector<XmlNode> moduleNodes = modulesNode.getChildren(); |
262 | vector<XmlNode> moduleNodes = modulesNode.getChildren(); |
| 227 | 263 | ||
| 228 | for (int n = 0; n < moduleNodes.size(); n++) |
264 | for (int n = 0; n < moduleNodes.size(); n++) |
| 229 | { |
265 | { |
| 230 | map<string, string> attributes = moduleNodes[n].getAttributes(); |
266 | map<string, string> attributes = moduleNodes[n].getAttributes(); |
| 231 | 267 | ||
| 232 | if (attributes["name"].compare(moduleName) == 0) |
268 | if (attributes["name"].compare(moduleName) == 0) |
| 233 | { |
269 | { |
| 234 | return stoi(attributes["id"]); |
270 | return stoi(attributes["id"]); |
| 235 | } |
271 | } |
| 236 | } |
272 | } |
| 237 | 273 | ||
| 238 | return -1; |
274 | return -1; |
| 239 | } |
275 | } |
| 240 | 276 | ||
| 241 | string CanIdTranslator::translateDataToHex(int commandId, string moduleName, map<string, CanVariable> data) |
277 | string CanIdTranslator::translateDataToHex(int commandId, string moduleName, map<string, CanVariable> data) |
| 242 | { |
278 | { |
| 243 | XmlNode commandsNode = xmlNode.findChild("commands"); |
279 | XmlNode commandsNode = xmlNode.findChild("commands"); |
| 244 | vector<XmlNode> commandNodes = commandsNode.getChildren(); |
280 | vector<XmlNode> commandNodes = commandsNode.getChildren(); |
| 245 | 281 | ||
| 246 | map<string, CanVariable> sortedData; |
282 | map<string, CanVariable> sortedData; |
| 247 | 283 | ||
| 248 | for (int n = 0; n < commandNodes.size(); n++) |
284 | for (int n = 0; n < commandNodes.size(); n++) |
| 249 | { |
285 | { |
| 250 | map<string, string> attributes = commandNodes[n].getAttributes(); |
286 | map<string, string> attributes = commandNodes[n].getAttributes(); |
| Line 252... | Line 288... | ||
| 252 | bool correct = false; |
288 | bool correct = false; |
| 253 | 289 | ||
| 254 | if (commandId >= 128) |
290 | if (commandId >= 128) |
| 255 | { |
291 | { |
| 256 | if (stoi(attributes["id"]) == commandId && attributes["module"] == moduleName) |
292 | if (stoi(attributes["id"]) == commandId && attributes["module"] == moduleName) |
| - | 293 | { |
|
| - | 294 | correct = true; |
|
| - | 295 | } |
|
| - | 296 | } |
|
| - | 297 | else if (stoi(attributes["id"]) == commandId) |
|
| - | 298 | { |
|
| - | 299 | correct = true; |
|
| - | 300 | } |
|
| - | 301 | ||
| - | 302 | if (correct) |
|
| - | 303 | { |
|
| - | 304 | XmlNode varablesNode = commandNodes[n].findChild("variables"); |
|
| - | 305 | ||
| - | 306 | vector<XmlNode> variableNodes = varablesNode.getChildren(); |
|
| - | 307 | ||
| - | 308 | for (int c = 0; c < variableNodes.size(); c++) |
|
| - | 309 | { |
|
| - | 310 | map<string, string> attributes = variableNodes[c].getAttributes(); |
|
| - | 311 | ||
| - | 312 | if (data.find(attributes["name"]) != data.end()) |
|
| - | 313 | { |
|
| - | 314 | int bitStart = stoi(attributes["start_bit"]);///FIXME: This is not a good way |
|
| - | 315 | int bitLength = stoi(attributes["bit_length"]); |
|
| - | 316 | ||
| - | 317 | sortedData[attributes["name"]].setType(attributes["type"]); |
|
| - | 318 | sortedData[attributes["name"]].setUnit(attributes["unit"]); |
|
| - | 319 | sortedData[attributes["name"]].setBitLength(bitLength); |
|
| - | 320 | sortedData[attributes["name"]].setBitLength(bitLength); |
|
| - | 321 | sortedData[attributes["name"]].setStartBit(bitStart); |
|
| - | 322 | sortedData[attributes["name"]].setName(attributes["name"]); |
|
| - | 323 | sortedData[attributes["name"]].setValue(data[attributes["name"]].getValue()); |
|
| - | 324 | } |
|
| - | 325 | } |
|
| - | 326 | } |
|
| - | 327 | } |
|
| - | 328 | ||
| - | 329 | string bin = "0000000000000000000000000000000000000000000000000000000000000000"; |
|
| - | 330 | int heighestBit = 0; |
|
| - | 331 | ||
| - | 332 | map<string, CanVariable>::iterator iter; |
|
| - | 333 | ||
| - | 334 | for (iter = sortedData.begin(); iter != sortedData.end(); iter++)///FIXME: We should check order and length |
|
| - | 335 | { |
|
| - | 336 | if (iter->second.getType() == "uint") |
|
| - | 337 | { |
|
| - | 338 | if (iter->second.getStartBit() + iter->second.getBitLength() > heighestBit) |
|
| - | 339 | heighestBit = iter->second.getStartBit() + iter->second.getBitLength(); |
|
| - | 340 | ||
| - | 341 | //cout << iter->first << ":" << stoi(iter->second.getValue()) << endl; |
|
| - | 342 | unsigned int a = stoi(iter->second.getValue()); |
|
| - | 343 | ||
| - | 344 | bin.replace(iter->second.getStartBit(), iter->second.getBitLength(), uint2bin(a, iter->second.getBitLength())); |
|
| - | 345 | } |
|
| - | 346 | else if (iter->second.getType() == "float") |
|
| - | 347 | { |
|
| - | 348 | if (iter->second.getStartBit() + iter->second.getBitLength() > heighestBit) |
|
| - | 349 | heighestBit = iter->second.getStartBit() + iter->second.getBitLength(); |
|
| - | 350 | ||
| - | 351 | bin.replace(iter->second.getStartBit(), iter->second.getBitLength(), float2bin(stof(iter->second.getValue()), iter->second.getBitLength())); |
|
| - | 352 | } |
|
| - | 353 | else if (iter->second.getType() == "ascii") |
|
| - | 354 | { |
|
| - | 355 | //cout << iter->second.getValue() << endl; |
|
| - | 356 | for (int n = 0; n < iter->second.getValue().length(); n++) |
|
| - | 357 | { |
|
| - | 358 | if (iter->second.getStartBit()+n*8 + 8 > heighestBit) |
|
| - | 359 | heighestBit = iter->second.getStartBit()+n*8 + 8; |
|
| - | 360 | ||
| - | 361 | //cout << iter->second.getValue()[n] << ":" << (unsigned int)iter->second.getValue()[n] << endl; |
|
| - | 362 | bin.replace(iter->second.getStartBit()+n*8, 8, uint2bin((unsigned int)iter->second.getValue()[n], 8)); |
|
| - | 363 | } |
|
| - | 364 | } |
|
| - | 365 | else if (iter->second.getType() == "hexstring") |
|
| - | 366 | { |
|
| - | 367 | for (int n = 0; n < iter->second.getValue().length(); n++) |
|
| - | 368 | { |
|
| - | 369 | if (iter->second.getStartBit()+n*4 + 4 > heighestBit) |
|
| - | 370 | heighestBit = iter->second.getStartBit()+n*4 + 4; |
|
| - | 371 | ||
| - | 372 | string character; |
|
| - | 373 | character += iter->second.getValue()[n]; |
|
| - | 374 | ||
| - | 375 | bin.replace(iter->second.getStartBit()+n*4, 4, hex2bin(character)); |
|
| - | 376 | } |
|
| - | 377 | } |
|
| - | 378 | } |
|
| - | 379 | ||
| - | 380 | bin = bin.substr(0, heighestBit); |
|
| - | 381 | ||
| - | 382 | //cout << bin2hex(bin) << endl; |
|
| - | 383 | ||
| - | 384 | return bin2hex(bin); |
|
| - | 385 | } |
|
| - | 386 | ||
| - | 387 | map<string, CanVariable> CanIdTranslator::translateData(int commandId, string moduleName, string dataHex) |
|
| - | 388 | { |
|
| - | 389 | map<string, CanVariable> variables; |
|
| - | 390 | XmlNode commandsNode = xmlNode.findChild("commands"); |
|
| - | 391 | vector<XmlNode> commandNodes = commandsNode.getChildren(); |
|
| - | 392 | ||
| - | 393 | string dataBin = hex2bin(dataHex); |
|
| - | 394 | ||
| - | 395 | for (int n = 0; n < commandNodes.size(); n++) |
|
| - | 396 | { |
|
| - | 397 | map<string, string> attributes = commandNodes[n].getAttributes(); |
|
| - | 398 | ||
| - | 399 | bool correct = false; |
|
| - | 400 | ||
| - | 401 | if (commandId >= 128) |
|
| - | 402 | { |
|
| - | 403 | if (stoi(attributes["id"]) == commandId && attributes["module"] == moduleName) |
|
| 257 | { |
404 | { |
| 258 | correct = true; |
405 | correct = true; |
| 259 | } |
406 | } |
| 260 | } |
407 | } |
| 261 | else if (stoi(attributes["id"]) == commandId) |
408 | else if (stoi(attributes["id"]) == commandId) |
| 262 | { |
409 | { |
| 263 | correct = true; |
410 | correct = true; |
| 264 | } |
411 | } |
| 265 | 412 | ||
| 266 | if (correct) |
413 | if (correct) |
| - | 414 | { |
|
| - | 415 | XmlNode varablesNode = commandNodes[n].findChild("variables"); |
|
| - | 416 | ||
| - | 417 | vector<XmlNode> variableNodes = varablesNode.getChildren(); |
|
| - | 418 | ||
| - | 419 | for (int c = 0; c < variableNodes.size(); c++) |
|
| - | 420 | { |
|
| - | 421 | map<string, string> attributes = variableNodes[c].getAttributes(); |
|
| - | 422 | ||
| - | 423 | int bitStart = stoi(attributes["start_bit"]); |
|
| - | 424 | int bitLength = stoi(attributes["bit_length"]); |
|
| - | 425 | ||
| - | 426 | string bits = dataBin.substr(bitStart, bitLength); |
|
| - | 427 | ||
| - | 428 | CanVariable variable; |
|
| - | 429 | ||
| - | 430 | variable.setName(attributes["name"]); |
|
| - | 431 | variable.setType(attributes["type"]); |
|
| - | 432 | variable.setUnit(attributes["unit"]); |
|
| - | 433 | variable.setBitLength(bitLength); |
|
| - | 434 | ||
| - | 435 | if (attributes["type"] == "uint") |
|
| - | 436 | { |
|
| - | 437 | variable.setValue(itos(bin2uint(bits))); |
|
| - | 438 | } |
|
| - | 439 | else if (attributes["type"] == "float") |
|
| - | 440 | { |
|
| - | 441 | variable.setValue(ftos(bin2float(bits))); |
|
| - | 442 | } |
|
| - | 443 | else if (attributes["type"] == "ascii") |
|
| - | 444 | { |
|
| - | 445 | string str; |
|
| - | 446 | ||
| - | 447 | for (int k = 0; k < bits.length(); k += 8) |
|
| - | 448 | { |
|
| - | 449 | str += (char)bin2uint(bits.substr(k, 8)); |
|
| - | 450 | } |
|
| - | 451 | ||
| - | 452 | variable.setValue(str); |
|
| - | 453 | } |
|
| - | 454 | else if (attributes["type"] == "hexstring") |
|
| - | 455 | { |
|
| - | 456 | string str; |
|
| - | 457 | ||
| - | 458 | for (int k = 0; k < bits.length(); k += 4) |
|
| - | 459 | { |
|
| - | 460 | str += bin2hex(bits.substr(k, 4)); |
|
| - | 461 | } |
|
| - | 462 | ||
| - | 463 | variable.setValue(str); |
|
| - | 464 | } |
|
| - | 465 | ||
| - | 466 | variables[attributes["name"]] = variable; |
|
| - | 467 | } |
|
| - | 468 | ||
| - | 469 | return variables; |
|
| - | 470 | } |
|
| - | 471 | } |
|
| - | 472 | ||
| - | 473 | return variables; |
|
| - | 474 | } |
|
| - | 475 | ||
| - | 476 | map<string, CanVariable> CanIdTranslator::translateNMTData(int commandId, string dataHex) |
|
| - | 477 | { |
|
| - | 478 | map<string, CanVariable> variables; |
|
| - | 479 | XmlNode commandsNode = xmlNode.findChild("nmt_messages"); |
|
| - | 480 | vector<XmlNode> commandNodes = commandsNode.getChildren(); |
|
| - | 481 | ||
| - | 482 | string dataBin = hex2bin(dataHex); |
|
| - | 483 | ||
| - | 484 | for (int n = 0; n < commandNodes.size(); n++) |
|
| - | 485 | { |
|
| - | 486 | map<string, string> attributes = commandNodes[n].getAttributes(); |
|
| - | 487 | ||
| - | 488 | if (stoi(attributes["id"]) == commandId) |
|
| - | 489 | { |
|
| - | 490 | XmlNode varablesNode = commandNodes[n].findChild("variables"); |
|
| - | 491 | ||
| - | 492 | vector<XmlNode> variableNodes = varablesNode.getChildren(); |
|
| - | 493 | ||
| - | 494 | for (int c = 0; c < variableNodes.size(); c++) |
|
| - | 495 | { |
|
| - | 496 | map<string, string> attributes = variableNodes[c].getAttributes(); |
|
| - | 497 | ||
| - | 498 | int bitStart = stoi(attributes["start_bit"]); |
|
| - | 499 | int bitLength = stoi(attributes["bit_length"]); |
|
| - | 500 | ||
| - | 501 | string bits = dataBin.substr(bitStart, bitLength); |
|
| - | 502 | ||
| - | 503 | CanVariable variable; |
|
| - | 504 | ||
| - | 505 | variable.setName(attributes["name"]); |
|
| - | 506 | variable.setType(attributes["type"]); |
|
| - | 507 | variable.setUnit(attributes["unit"]); |
|
| - | 508 | variable.setBitLength(bitLength); |
|
| - | 509 | ||
| - | 510 | if (attributes["type"] == "uint") |
|
| - | 511 | { |
|
| - | 512 | variable.setValue(itos(bin2uint(bits))); |
|
| - | 513 | } |
|
| - | 514 | else if (attributes["type"] == "float") |
|
| - | 515 | { |
|
| - | 516 | variable.setValue(ftos(bin2float(bits))); |
|
| - | 517 | } |
|
| - | 518 | else if (attributes["type"] == "ascii") |
|
| - | 519 | { |
|
| - | 520 | string str; |
|
| - | 521 | ||
| - | 522 | for (int k = 0; k < bits.length(); k += 8) |
|
| - | 523 | { |
|
| - | 524 | str += (char)bin2uint(bits.substr(k, 8)); |
|
| - | 525 | } |
|
| - | 526 | ||
| - | 527 | variable.setValue(str); |
|
| - | 528 | } |
|
| - | 529 | else if (attributes["type"] == "hexstring") |
|
| - | 530 | { |
|
| - | 531 | string str; |
|
| - | 532 | ||
| - | 533 | for (int k = 0; k < bits.length(); k += 4) |
|
| - | 534 | { |
|
| - | 535 | str += bin2hex(bits.substr(k, 4)); |
|
| - | 536 | } |
|
| - | 537 | ||
| - | 538 | variable.setValue(str); |
|
| - | 539 | } |
|
| - | 540 | ||
| - | 541 | variables[attributes["name"]] = variable; |
|
| - | 542 | } |
|
| - | 543 | ||
| - | 544 | return variables; |
|
| - | 545 | } |
|
| - | 546 | } |
|
| - | 547 | ||
| - | 548 | //return dataString + " ]"; |
|
| - | 549 | return variables; |
|
| - | 550 | } |
|
| - | 551 | ||
| - | 552 | string CanIdTranslator::translateNMTDataToHex(int commandId, map<string, CanVariable> data) |
|
| - | 553 | { |
|
| - | 554 | XmlNode commandsNode = xmlNode.findChild("nmt_messages"); |
|
| - | 555 | vector<XmlNode> commandNodes = commandsNode.getChildren(); |
|
| - | 556 | ||
| - | 557 | map<string, CanVariable> sortedData; |
|
| - | 558 | ||
| - | 559 | for (int n = 0; n < commandNodes.size(); n++) |
|
| - | 560 | { |
|
| - | 561 | map<string, string> attributes = commandNodes[n].getAttributes(); |
|
| - | 562 | ||
| - | 563 | if (stoi(attributes["id"]) == commandId) |
|
| 267 | { |
564 | { |
| 268 | XmlNode varablesNode = commandNodes[n].findChild("variables"); |
565 | XmlNode varablesNode = commandNodes[n].findChild("variables"); |
| 269 | 566 | ||
| 270 | vector<XmlNode> variableNodes = varablesNode.getChildren(); |
567 | vector<XmlNode> variableNodes = varablesNode.getChildren(); |
| 271 | 568 | ||
| 272 | for (int c = 0; c < variableNodes.size(); c++) |
569 | for (int c = 0; c < variableNodes.size(); c++) |
| 273 | { |
570 | { |
| 274 | map<string, string> attributes = variableNodes[c].getAttributes(); |
571 | map<string, string> attributes = variableNodes[c].getAttributes(); |
| 275 | 572 | ||
| 276 | if (data.find(attributes["name"]) != data.end()) |
573 | if (data.find(attributes["name"]) != data.end()) |
| Line 286... | Line 583... | ||
| 286 | sortedData[attributes["name"]].setName(attributes["name"]); |
583 | sortedData[attributes["name"]].setName(attributes["name"]); |
| 287 | sortedData[attributes["name"]].setValue(data[attributes["name"]].getValue()); |
584 | sortedData[attributes["name"]].setValue(data[attributes["name"]].getValue()); |
| 288 | } |
585 | } |
| 289 | } |
586 | } |
| 290 | } |
587 | } |
| 291 | } |
588 | } |
| 292 | 589 | ||
| 293 | string bin = "0000000000000000000000000000000000000000000000000000000000000000"; |
590 | string bin = "0000000000000000000000000000000000000000000000000000000000000000"; |
| 294 | int heighestBit = 0; |
591 | int heighestBit = 0; |
| 295 | 592 | ||
| 296 | map<string, CanVariable>::iterator iter; |
593 | map<string, CanVariable>::iterator iter; |
| 297 | 594 | ||
| 298 | for (iter = sortedData.begin(); iter != sortedData.end(); iter++)///FIXME: We should check order and length |
595 | for (iter = sortedData.begin(); iter != sortedData.end(); iter++)///FIXME: We should check order and length |
| 299 | { |
596 | { |
| 300 | if (iter->second.getType() == "uint") |
597 | if (iter->second.getType() == "uint") |
| 301 | { |
598 | { |
| 302 | if (iter->second.getStartBit() + iter->second.getBitLength() > heighestBit) |
599 | if (iter->second.getStartBit() + iter->second.getBitLength() > heighestBit) |
| 303 | heighestBit = iter->second.getStartBit() + iter->second.getBitLength(); |
600 | heighestBit = iter->second.getStartBit() + iter->second.getBitLength(); |
| 304 | 601 | ||
| 305 | //cout << iter->first << ":" << stoi(iter->second.getValue()) << endl; |
602 | //cout << iter->first << ":" << stoi(iter->second.getValue()) << endl; |
| 306 | unsigned int a = stoi(iter->second.getValue()); |
603 | unsigned int a = stoi(iter->second.getValue()); |
| 307 | 604 | ||
| 308 | bin.replace(iter->second.getStartBit(), iter->second.getBitLength(), uint2bin(a, iter->second.getBitLength())); |
605 | bin.replace(iter->second.getStartBit(), iter->second.getBitLength(), uint2bin(a, iter->second.getBitLength())); |
| 309 | } |
606 | } |
| 310 | else if (iter->second.getType() == "float") |
607 | else if (iter->second.getType() == "float") |
| 311 | { |
608 | { |
| 312 | if (iter->second.getStartBit() + iter->second.getBitLength() > heighestBit) |
609 | if (iter->second.getStartBit() + iter->second.getBitLength() > heighestBit) |
| 313 | heighestBit = iter->second.getStartBit() + iter->second.getBitLength(); |
610 | heighestBit = iter->second.getStartBit() + iter->second.getBitLength(); |
| 314 | 611 | ||
| 315 | bin.replace(iter->second.getStartBit(), iter->second.getBitLength(), float2bin(stof(iter->second.getValue()), iter->second.getBitLength())); |
612 | bin.replace(iter->second.getStartBit(), iter->second.getBitLength(), float2bin(stof(iter->second.getValue()), iter->second.getBitLength())); |
| 316 | } |
613 | } |
| 317 | else if (iter->second.getType() == "ascii") |
614 | else if (iter->second.getType() == "ascii") |
| 318 | { |
615 | { |
| 319 | //cout << iter->second.getValue() << endl; |
616 | //cout << iter->second.getValue() << endl; |
| Line 323... | Line 620... | ||
| 323 | heighestBit = iter->second.getStartBit()+n*8 + 8; |
620 | heighestBit = iter->second.getStartBit()+n*8 + 8; |
| 324 | 621 | ||
| 325 | //cout << iter->second.getValue()[n] << ":" << (unsigned int)iter->second.getValue()[n] << endl; |
622 | //cout << iter->second.getValue()[n] << ":" << (unsigned int)iter->second.getValue()[n] << endl; |
| 326 | bin.replace(iter->second.getStartBit()+n*8, 8, uint2bin((unsigned int)iter->second.getValue()[n], 8)); |
623 | bin.replace(iter->second.getStartBit()+n*8, 8, uint2bin((unsigned int)iter->second.getValue()[n], 8)); |
| 327 | } |
624 | } |
| 328 | } |
625 | } |
| 329 | else if (iter->second.getType() == "hexstring") |
626 | else if (iter->second.getType() == "hexstring") |
| 330 | { |
627 | { |
| 331 | for (int n = 0; n < iter->second.getValue().length(); n++) |
628 | for (int n = 0; n < iter->second.getValue().length(); n++) |
| 332 | { |
629 | { |
| 333 | if (iter->second.getStartBit()+n*4 + 4 > heighestBit) |
630 | if (iter->second.getStartBit()+n*4 + 4 > heighestBit) |
| Line 337... | Line 634... | ||
| 337 | character += iter->second.getValue()[n]; |
634 | character += iter->second.getValue()[n]; |
| 338 | 635 | ||
| 339 | bin.replace(iter->second.getStartBit()+n*4, 4, hex2bin(character)); |
636 | bin.replace(iter->second.getStartBit()+n*4, 4, hex2bin(character)); |
| 340 | } |
637 | } |
| 341 | } |
638 | } |
| 342 | } |
639 | } |
| 343 | 640 | ||
| 344 | bin = bin.substr(0, heighestBit); |
641 | bin = bin.substr(0, heighestBit); |
| 345 | - | ||
| 346 | //cout << bin2hex(bin) << endl; |
- | |
| 347 | - | ||
| 348 | return bin2hex(bin); |
- | |
| 349 | } |
- | |
| 350 | - | ||
| 351 | map<string, CanVariable> CanIdTranslator::translateData(int commandId, string moduleName, string dataHex) |
- | |
| 352 | { |
- | |
| 353 | map<string, CanVariable> variables; |
- | |
| 354 | XmlNode commandsNode = xmlNode.findChild("commands"); |
- | |
| 355 | vector<XmlNode> commandNodes = commandsNode.getChildren(); |
- | |
| 356 | - | ||
| 357 | string dataBin = hex2bin(dataHex); |
- | |
| 358 | //string dataString = "{"; |
- | |
| 359 | - | ||
| 360 | for (int n = 0; n < commandNodes.size(); n++) |
- | |
| 361 | { |
- | |
| 362 | map<string, string> attributes = commandNodes[n].getAttributes(); |
- | |
| 363 | - | ||
| 364 | bool correct = false; |
- | |
| 365 | - | ||
| 366 | if (commandId >= 128) |
- | |
| 367 | { |
- | |
| 368 | if (stoi(attributes["id"]) == commandId && attributes["module"] == moduleName) |
- | |
| 369 | { |
- | |
| 370 | correct = true; |
- | |
| 371 | } |
- | |
| 372 | } |
- | |
| 373 | else if (stoi(attributes["id"]) == commandId) |
- | |
| 374 | { |
- | |
| 375 | correct = true; |
- | |
| 376 | } |
- | |
| 377 | - | ||
| 378 | if (correct) |
- | |
| 379 | { |
- | |
| 380 | XmlNode varablesNode = commandNodes[n].findChild("variables"); |
- | |
| 381 | - | ||
| 382 | vector<XmlNode> variableNodes = varablesNode.getChildren(); |
- | |
| 383 | - | ||
| 384 | for (int c = 0; c < variableNodes.size(); c++) |
- | |
| 385 | { |
- | |
| 386 | map<string, string> attributes = variableNodes[c].getAttributes(); |
- | |
| 387 | - | ||
| 388 | int bitStart = stoi(attributes["start_bit"]); |
- | |
| 389 | int bitLength = stoi(attributes["bit_length"]); |
- | |
| 390 | - | ||
| 391 | string bits = dataBin.substr(bitStart, bitLength); |
- | |
| 392 | - | ||
| 393 | CanVariable variable; |
- | |
| 394 | - | ||
| 395 | variable.setName(attributes["name"]); |
- | |
| 396 | variable.setType(attributes["type"]); |
- | |
| 397 | variable.setUnit(attributes["unit"]); |
- | |
| 398 | variable.setBitLength(bitLength); |
- | |
| 399 | - | ||
| 400 | //dataString += " " + attributes["name"] + " : { value : "; |
- | |
| 401 | //dataString += attributes["name"] + "="; |
- | |
| 402 | //dataString += attributes["name"] + ":" + attributes["type"] + ":" + attributes["bit_length"] + "=\""; |
- | |
| 403 | - | ||
| 404 | if (attributes["type"] == "uint") |
- | |
| 405 | { |
- | |
| 406 | variable.setValue(itos(bin2uint(bits))); |
- | |
| 407 | //dataString += itos(bin2uint(bits)); |
- | |
| 408 | } |
- | |
| 409 | else if (attributes["type"] == "float") |
- | |
| 410 | { |
- | |
| 411 | variable.setValue(ftos(bin2float(bits))); |
- | |
| 412 | //dataString += ftos(bin2float(bits)); |
- | |
| 413 | } |
- | |
| 414 | else if (attributes["type"] == "ascii") |
- | |
| 415 | { |
- | |
| 416 | string str; |
- | |
| 417 | //dataString += "'"; |
- | |
| 418 | - | ||
| 419 | for (int k = 0; k < bits.length(); k += 8) |
- | |
| 420 | { |
- | |
| 421 | str += (char)bin2uint(bits.substr(k, 8)); |
- | |
| 422 | //dataString += (char)bin2uint(bits.substr(k, 4)); |
- | |
| 423 | } |
- | |
| 424 | - | ||
| 425 | variable.setValue(str); |
- | |
| 426 | //cout << "Parsed ascii: " << str << "\n"; |
- | |
| 427 | //dataString += "'"; |
- | |
| 428 | } |
- | |
| 429 | else if (attributes["type"] == "hexstring") |
- | |
| 430 | { |
- | |
| 431 | string str; |
- | |
| 432 | - | ||
| 433 | for (int k = 0; k < bits.length(); k += 4) |
- | |
| 434 | { |
- | |
| 435 | str += bin2hex(bits.substr(k, 4)); |
- | |
| 436 | //dataString += (char)bin2uint(bits.substr(k, 4)); |
- | |
| 437 | } |
- | |
| 438 | - | ||
| 439 | variable.setValue(str); |
- | |
| 440 | } |
- | |
| 441 | - | ||
| 442 | variables[attributes["name"]] = variable; |
- | |
| 443 | - | ||
| 444 | //dataString += ", type : '" + attributes["type"] + "', bits : " + attributes["bit_length"] + ", unit : '" + attributes["unit"] + "' },"; |
- | |
| 445 | - | ||
| 446 | //dataString += ","; |
- | |
| 447 | //dataString += "\","; |
- | |
| 448 | } |
- | |
| 449 | - | ||
| 450 | //dataString = trim(dataString, ','); |
- | |
| 451 | - | ||
| 452 | //dataString += " }"; |
- | |
| 453 | - | ||
| 454 | //return dataString; |
- | |
| 455 | - | ||
| 456 | - | ||
| 457 | return variables; |
- | |
| 458 | } |
- | |
| 459 | } |
- | |
| 460 | - | ||
| 461 | //return dataString + " ]"; |
- | |
| 462 | return variables; |
- | |
| 463 | } |
- | |
| 464 | - | ||
| 465 | map<string, CanVariable> CanIdTranslator::translateHeartbeatData(string dataHex) |
- | |
| 466 | { |
- | |
| 467 | map<string, CanVariable> variables; |
- | |
| 468 | - | ||
| 469 | string dataBin = hex2bin(dataHex); |
- | |
| 470 | string bits = dataBin.substr(0, 32); |
- | |
| 471 | - | ||
| 472 | CanVariable canVariable("HardwareId", itos(bin2uint(bits)), "uint", ""); |
- | |
| 473 | canVariable.setBitLength(32); |
- | |
| 474 | - | ||
| 475 | variables["HardwareId"] = canVariable; |
- | |
| 476 | 642 | ||
| 477 |
|
643 | //cout << bin2hex(bin) << endl; |
| 478 | 644 | ||
| 479 | //return "{ HardwareId : { value : " + itos(bin2uint(bits)) + ", type : 'uint', bits : 32, unit : '' }"; |
- | |
| 480 |
|
645 | return bin2hex(bin); |
| 481 | } |
646 | } |
| - | 647 | ||