Rev 986 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 986 | Rev 991 | ||
|---|---|---|---|
| Line 272... | Line 272... | ||
| 272 | } |
272 | } |
| 273 | 273 | ||
| 274 | return -1; |
274 | return -1; |
| 275 | } |
275 | } |
| 276 | 276 | ||
| 277 |
|
277 | void CanIdTranslator::makeNMTDataValid(int commandId, map<string, CanVariable> &data) |
| 278 | { |
278 | { |
| 279 | XmlNode commandsNode = xmlNode.findChild("commands"); |
- | |
| 280 | vector<XmlNode> commandNodes = |
279 | vector<XmlNode> commandNodes = xmlNode.findChild("nmt_messages").getChildren(); |
| 281 | 280 | ||
| - | 281 | for (int n = 0; n < commandNodes.size(); n++) |
|
| - | 282 | { |
|
| 282 | map |
283 | map<string, string> attributes = commandNodes[n].getAttributes(); |
| - | 284 | ||
| - | 285 | if (stoi(attributes["id"]) == commandId) |
|
| - | 286 | { |
|
| - | 287 | XmlNode varablesNode = commandNodes[n].findChild("variables"); |
|
| - | 288 | ||
| - | 289 | makeDataValid(varablesNode.getChildren(), data); |
|
| 283 | 290 | ||
| 284 | for (int n = 0; n < commandNodes.size(); n++) |
- | |
| 285 | { |
- | |
| 286 | map<string, string> attributes = commandNodes[n].getAttributes(); |
- | |
| 287 | - | ||
| 288 | bool correct = false; |
- | |
| 289 | - | ||
| 290 | if (commandId >= 128) |
- | |
| 291 | { |
- | |
| 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) |
- | |
| 404 | { |
- | |
| 405 | correct = true; |
- | |
| 406 | } |
- | |
| 407 | } |
- | |
| 408 | else if (stoi(attributes["id"]) == commandId) |
- | |
| 409 | { |
- | |
| 410 |
|
291 | return; |
| 411 | } |
292 | } |
| 412 | - | ||
| 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 | } |
293 | } |
| 471 | } |
294 | } |
| 472 | 295 | ||
| 473 | return variables; |
- | |
| 474 | } |
- | |
| 475 | - | ||
| 476 |
|
296 | void CanIdTranslator::makeDataValid(int commandId, string moduleName, map<string, CanVariable> &data) |
| 477 | { |
297 | { |
| 478 | map<string, CanVariable> variables; |
- | |
| 479 | XmlNode commandsNode = xmlNode.findChild("nmt_messages"); |
- | |
| 480 | vector<XmlNode> commandNodes = |
298 | vector<XmlNode> commandNodes = xmlNode.findChild("commands").getChildren(); |
| 481 | - | ||
| 482 | string dataBin = hex2bin(dataHex); |
- | |
| 483 | 299 | ||
| 484 | for (int n = 0; n < commandNodes.size(); n++) |
300 | for (int n = 0; n < commandNodes.size(); n++) |
| 485 | { |
301 | { |
| 486 | map<string, string> attributes = commandNodes[n].getAttributes(); |
302 | map<string, string> attributes = commandNodes[n].getAttributes(); |
| 487 | 303 | ||
| 488 | if (stoi( |
304 | if ((stoi(attributes["id"]) == commandId) || (commandId >= 128 && (stoi(attributes["id"]) == commandId && attributes["module"] == moduleName))) |
| 489 | { |
305 | { |
| 490 | XmlNode varablesNode = commandNodes[n].findChild("variables"); |
306 | XmlNode varablesNode = commandNodes[n].findChild("variables"); |
| 491 | 307 | ||
| 492 |
|
308 | makeDataValid(varablesNode.getChildren(), data); |
| 493 | 309 | ||
| 494 |
|
310 | return; |
| 495 |
|
311 | } |
| 496 | map<string, string> attributes = variableNodes[c].getAttributes(); |
- | |
| 497 | 312 | } |
|
| 498 | int bitStart = stoi(attributes["start_bit"]); |
- | |
| 499 | int bitLength = stoi(attributes["bit_length"]); |
- | |
| 500 | 313 | } |
|
| 501 | string bits = dataBin.substr(bitStart, bitLength); |
- | |
| 502 | 314 | ||
| 503 |
|
315 | void CanIdTranslator::makeDataValid(vector<XmlNode> variableNodes, map<string, CanVariable> &data) |
| 504 | 316 | { |
|
| 505 |
|
317 | ///FIXME: Remove variables that do not exist in the xmlfile |
| 506 |
|
318 | for (int c = 0; c < variableNodes.size(); c++) |
| - | 319 | { |
|
| 507 |
|
320 | map<string, string> attributes = variableNodes[c].getAttributes(); |
| 508 | variable.setBitLength(bitLength); |
- | |
| 509 | 321 | ||
| 510 |
|
322 | if (data.find(attributes["name"]) != data.end()) |
| 511 | { |
323 | { |
| 512 |
|
324 | data[attributes["name"]].setType(attributes["type"]); |
| 513 | } |
- | |
| 514 |
|
325 | data[attributes["name"]].setUnit(attributes["unit"]); |
| 515 | { |
- | |
| - | 326 | data[attributes["name"]].setBitLength(stoi(attributes["bit_length"])); |
|
| 516 |
|
327 | data[attributes["name"]].setStartBit(stoi(attributes["start_bit"])); |
| 517 | } |
328 | } |
| 518 | else if (attributes["type"] == "ascii") |
- | |
| 519 |
|
329 | } |
| 520 |
|
330 | } |
| 521 | 331 | ||
| 522 |
|
332 | string CanIdTranslator::translateDataToHex(int commandId, string moduleName, map<string, CanVariable> &data) |
| 523 | { |
333 | { |
| 524 |
|
334 | makeDataValid(commandId, moduleName, data); |
| 525 | } |
- | |
| 526 | 335 | ||
| 527 |
|
336 | return translateValidDataToHex(data); |
| 528 | } |
337 | } |
| - | 338 | ||
| - | 339 | ||
| 529 |
|
340 | map<string, CanVariable> CanIdTranslator::translateData(int commandId, string moduleName, string dataHex) |
| 530 | { |
341 | { |
| 531 |
|
342 | vector<XmlNode> commandNodes = xmlNode.findChild("commands").getChildren(); |
| 532 | 343 | ||
| 533 |
|
344 | for (int n = 0; n < commandNodes.size(); n++) |
| 534 | { |
345 | { |
| 535 |
|
346 | map<string, string> attributes = commandNodes[n].getAttributes(); |
| 536 | } |
- | |
| 537 | 347 | ||
| 538 |
|
348 | if ((stoi(attributes["id"]) == commandId) || (commandId >= 128 && (stoi(attributes["id"]) == commandId && attributes["module"] == moduleName))) |
| 539 |
|
349 | { |
| - | 350 | XmlNode varablesNode = commandNodes[n].findChild("variables"); |
|
| 540 | 351 | ||
| 541 |
|
352 | return translateData(varablesNode.getChildren(), dataHex); |
| 542 | } |
353 | } |
| - | 354 | } |
|
| 543 | 355 | ||
| - | 356 | map<string, CanVariable> variables; |
|
| 544 | return variables; |
357 | return variables; |
| 545 | } |
358 | } |
| 546 | } |
- | |
| 547 | 359 | ||
| 548 | //return dataString + " ]"; |
- | |
| 549 | return variables; |
- | |
| 550 | } |
- | |
| 551 | - | ||
| 552 |
|
360 | map<string, CanVariable> CanIdTranslator::translateNMTData(int commandId, string dataHex) |
| 553 | { |
361 | { |
| 554 | XmlNode |
362 | vector<XmlNode> commandNodes = xmlNode.findChild("nmt_messages").getChildren(); |
| 555 | vector<XmlNode> commandNodes = commandsNode.getChildren(); |
- | |
| 556 | - | ||
| 557 | map<string, CanVariable> sortedData; |
- | |
| 558 | 363 | ||
| 559 | for (int n = 0; n < commandNodes.size(); n++) |
364 | for (int n = 0; n < commandNodes.size(); n++) |
| 560 | { |
365 | { |
| 561 | map<string, string> attributes = commandNodes[n].getAttributes(); |
366 | map<string, string> attributes = commandNodes[n].getAttributes(); |
| 562 | 367 | ||
| 563 | if (stoi(attributes["id"]) == commandId) |
368 | if (stoi(attributes["id"]) == commandId) |
| 564 | { |
369 | { |
| 565 | XmlNode varablesNode = commandNodes[n].findChild("variables"); |
370 | XmlNode varablesNode = commandNodes[n].findChild("variables"); |
| 566 | 371 | ||
| 567 |
|
372 | return translateData(varablesNode.getChildren(), dataHex); |
| 568 | - | ||
| 569 | for (int c = 0; c < variableNodes.size(); c++) |
- | |
| 570 | { |
- | |
| 571 | map<string, string> attributes = variableNodes[c].getAttributes(); |
- | |
| 572 | - | ||
| 573 | if (data.find(attributes["name"]) != data.end()) |
- | |
| 574 | { |
- | |
| 575 | int bitStart = stoi(attributes["start_bit"]);///FIXME: This is not a good way |
- | |
| 576 | int bitLength = stoi(attributes["bit_length"]); |
- | |
| 577 | - | ||
| 578 | sortedData[attributes["name"]].setType(attributes["type"]); |
- | |
| 579 | sortedData[attributes["name"]].setUnit(attributes["unit"]); |
- | |
| 580 | sortedData[attributes["name"]].setBitLength(bitLength); |
- | |
| 581 | sortedData[attributes["name"]].setBitLength(bitLength); |
- | |
| 582 | sortedData[attributes["name"]].setStartBit(bitStart); |
- | |
| 583 | sortedData[attributes["name"]].setName(attributes["name"]); |
- | |
| 584 | sortedData[attributes["name"]].setValue(data[attributes["name"]].getValue()); |
- | |
| 585 | } |
- | |
| 586 | } |
373 | } |
| 587 | } |
374 | } |
| - | 375 | ||
| - | 376 | map<string, CanVariable> variables; |
|
| - | 377 | return variables; |
|
| 588 | } |
378 | } |
| 589 | 379 | ||
| - | 380 | string CanIdTranslator::translateNMTDataToHex(int commandId, map<string, CanVariable> &data) |
|
| - | 381 | { |
|
| - | 382 | makeNMTDataValid(commandId, data); |
|
| - | 383 | ||
| - | 384 | return translateValidDataToHex(data); |
|
| - | 385 | } |
|
| - | 386 | ||
| - | 387 | string CanIdTranslator::translateValidDataToHex(map<string, CanVariable> &data) |
|
| - | 388 | { |
|
| 590 | string bin = "0000000000000000000000000000000000000000000000000000000000000000"; |
389 | string bin = "0000000000000000000000000000000000000000000000000000000000000000"; |
| 591 | int |
390 | int highestBit = 0; |
| 592 | - | ||
| 593 | map<string, CanVariable>::iterator iter; |
- | |
| 594 | 391 | ||
| 595 | for ( |
392 | for (map<string, CanVariable>::iterator iter = data.begin(); iter != data.end(); iter++) |
| 596 | { |
393 | { |
| 597 | if (iter->second.getType() == "uint") |
394 | if (iter->second.getType() == "uint") |
| 598 | { |
395 | { |
| 599 | if (iter->second.getStartBit() + iter->second.getBitLength() > |
396 | if (iter->second.getStartBit() + iter->second.getBitLength() > highestBit) |
| 600 |
|
397 | highestBit = iter->second.getStartBit() + iter->second.getBitLength(); |
| 601 | 398 | ||
| 602 | //cout << iter->first << ":" << stoi(iter->second.getValue()) << endl; |
- | |
| 603 | unsigned int a = stoi(iter->second.getValue()); |
399 | unsigned int a = stoi(iter->second.getValue()); |
| 604 | 400 | ||
| 605 | bin.replace(iter->second.getStartBit(), iter->second.getBitLength(), uint2bin(a, iter->second.getBitLength())); |
401 | bin.replace(iter->second.getStartBit(), iter->second.getBitLength(), uint2bin(a, iter->second.getBitLength())); |
| 606 | } |
402 | } |
| 607 | else if (iter->second.getType() == "float") |
403 | else if (iter->second.getType() == "float") |
| 608 | { |
404 | { |
| 609 | if (iter->second.getStartBit() + iter->second.getBitLength() > |
405 | if (iter->second.getStartBit() + iter->second.getBitLength() > highestBit) |
| 610 |
|
406 | highestBit = iter->second.getStartBit() + iter->second.getBitLength(); |
| 611 | 407 | ||
| 612 | bin.replace(iter->second.getStartBit(), iter->second.getBitLength(), float2bin(stof(iter->second.getValue()), iter->second.getBitLength())); |
408 | bin.replace(iter->second.getStartBit(), iter->second.getBitLength(), float2bin(stof(iter->second.getValue()), iter->second.getBitLength())); |
| 613 | } |
409 | } |
| 614 | else if (iter->second.getType() == "ascii") |
410 | else if (iter->second.getType() == "ascii") |
| 615 | { |
411 | { |
| 616 | //cout << iter->second.getValue() << endl; |
- | |
| 617 | for (int n = 0; n < iter->second.getValue().length(); n++) |
412 | for (int n = 0; n < iter->second.getValue().length(); n++) |
| 618 | { |
413 | { |
| 619 | if (iter->second.getStartBit()+n*8 + 8 > |
414 | if (iter->second.getStartBit()+n*8 + 8 > highestBit) |
| 620 |
|
415 | highestBit = iter->second.getStartBit()+n*8 + 8; |
| 621 | 416 | ||
| 622 | //cout << iter->second.getValue()[n] << ":" << (unsigned int)iter->second.getValue()[n] << endl; |
- | |
| 623 | bin.replace(iter->second.getStartBit()+n*8, 8, uint2bin((unsigned int)iter->second.getValue()[n], 8)); |
417 | bin.replace(iter->second.getStartBit()+n*8, 8, uint2bin((unsigned int)iter->second.getValue()[n], 8)); |
| 624 | } |
418 | } |
| 625 | } |
419 | } |
| 626 | else if (iter->second.getType() == "hexstring") |
420 | else if (iter->second.getType() == "hexstring") |
| 627 | { |
421 | { |
| 628 | for (int n = 0; n < iter->second.getValue().length(); n++) |
422 | for (int n = 0; n < iter->second.getValue().length(); n++) |
| 629 | { |
423 | { |
| 630 | if (iter->second.getStartBit()+n*4 + 4 > |
424 | if (iter->second.getStartBit()+n*4 + 4 > highestBit) |
| 631 |
|
425 | highestBit = iter->second.getStartBit()+n*4 + 4; |
| 632 | 426 | ||
| 633 | string character; |
427 | string character; |
| 634 | character += iter->second.getValue()[n]; |
428 | character += iter->second.getValue()[n]; |
| 635 | 429 | ||
| 636 | bin.replace(iter->second.getStartBit()+n*4, 4, hex2bin(character)); |
430 | bin.replace(iter->second.getStartBit()+n*4, 4, hex2bin(character)); |
| 637 | } |
431 | } |
| 638 | } |
432 | } |
| 639 | } |
433 | } |
| 640 | 434 | ||
| 641 | bin = bin.substr(0, |
435 | bin = bin.substr(0, highestBit); |
| - | 436 | ||
| - | 437 | return bin2hex(bin); |
|
| - | 438 | } |
|
| - | 439 | ||
| - | 440 | map<string, CanVariable> CanIdTranslator::translateData(vector<XmlNode> variableNodes, string dataHex) |
|
| - | 441 | { |
|
| - | 442 | map<string, CanVariable> variables; |
|
| - | 443 | string dataBin = hex2bin(dataHex); |
|
| - | 444 | ||
| - | 445 | for (int c = 0; c < variableNodes.size(); c++) |
|
| - | 446 | { |
|
| - | 447 | map<string, string> attributes = variableNodes[c].getAttributes(); |
|
| - | 448 | ||
| - | 449 | int bitStart = stoi(attributes["start_bit"]); |
|
| - | 450 | int bitLength = stoi(attributes["bit_length"]); |
|
| - | 451 | ||
| - | 452 | string bits = dataBin.substr(bitStart, bitLength); |
|
| 642 | 453 | ||
| - | 454 | CanVariable variable; |
|
| - | 455 | ||
| - | 456 | variable.setName(attributes["name"]); |
|
| - | 457 | variable.setType(attributes["type"]); |
|
| - | 458 | variable.setUnit(attributes["unit"]); |
|
| 643 |
|
459 | variable.setStartBit(bitStart); |
| - | 460 | variable.setBitLength(bitLength); |
|
| 644 | 461 | ||
| - | 462 | if (attributes["type"] == "uint") |
|
| - | 463 | { |
|
| - | 464 | variable.setValue(itos(bin2uint(bits))); |
|
| - | 465 | } |
|
| - | 466 | else if (attributes["type"] == "float") |
|
| - | 467 | { |
|
| - | 468 | variable.setValue(ftos(bin2float(bits))); |
|
| - | 469 | } |
|
| - | 470 | else if (attributes["type"] == "ascii") |
|
| - | 471 | { |
|
| 645 |
|
472 | string str; |
| - | 473 | ||
| - | 474 | for (int k = 0; k < bits.length(); k += 8) |
|
| - | 475 | { |
|
| - | 476 | str += (char)bin2uint(bits.substr(k, 8)); |
|
| 646 | } |
477 | } |
| - | 478 | ||
| - | 479 | variable.setValue(str); |
|
| - | 480 | } |
|
| - | 481 | else if (attributes["type"] == "hexstring") |
|
| - | 482 | { |
|
| - | 483 | string str; |
|
| - | 484 | ||
| - | 485 | for (int k = 0; k < bits.length(); k += 4) |
|
| - | 486 | { |
|
| - | 487 | str += bin2hex(bits.substr(k, 4)); |
|
| - | 488 | } |
|
| - | 489 | ||
| - | 490 | variable.setValue(str); |
|
| - | 491 | } |
|
| - | 492 | ||
| - | 493 | variables[attributes["name"]] = variable; |
|
| - | 494 | } |
|
| - | 495 | ||
| - | 496 | return variables; |
|
| - | 497 | } |
|
| - | 498 | ||
| 647 | 499 | ||