Rev 1599 | Rev 1605 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 1599 | Rev 1600 | ||
|---|---|---|---|
| Line 303... | Line 303... | ||
| 303 | bitset.Write(start_bit, bit_length, raw_bit_value); |
303 | bitset.Write(start_bit, bit_length, raw_bit_value); |
| 304 | } |
304 | } |
| 305 | 305 | ||
| 306 | std::string Protocol::DecodeFloat(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length) |
306 | std::string Protocol::DecodeFloat(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length) |
| 307 | { |
307 | { |
| 308 | float raw_value = 0; |
- | |
| 309 | bool negative = false;; |
- | |
| 310 | - | ||
| 311 | if (bitset.Get(start_bit)) |
- | |
| 312 | { |
- | |
| 313 | negative = true; |
- | |
| 314 | } |
- | |
| 315 | - | ||
| 316 | for (int c = bit_length - 1; c > 0; c--) |
- | |
| 317 | { |
- | |
| 318 | if (bitset.Get(start_bit + c) != negative) |
- | |
| 319 | { |
- | |
| 320 |
|
308 | float float_value = boost::lexical_cast<float>(this->DecodeInt(bitset, start_bit, bit_length)) / 64.0f; |
| 321 | } |
- | |
| 322 | } |
- | |
| 323 | - | ||
| 324 | raw_value /= 64.0f; |
- | |
| 325 | - | ||
| 326 | if (negative) |
- | |
| 327 | { |
- | |
| 328 | raw_value = -raw_value; |
- | |
| 329 | } |
- | |
| 330 | 309 | ||
| 331 | return boost::lexical_cast<std::string>( |
310 | return boost::lexical_cast<std::string>(float_value); |
| 332 | } |
311 | } |
| 333 | 312 | ||
| 334 | void Protocol::EncodeFloat(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length, std::string value) |
313 | void Protocol::EncodeFloat(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length, std::string value) |
| 335 | { |
314 | { |
| - | 315 | int int_value = boost::lexical_cast<float>(value) * 64.0f; |
|
| 336 |
|
316 | |
| 337 |
|
317 | this->EncodeInt(bitset, start_bit, bit_length, boost::lexical_cast<std::string>(int_value)); |
| 338 | } |
318 | } |
| 339 | 319 | ||
| 340 | std::string Protocol::DecodeAscii(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length) |
320 | std::string Protocol::DecodeAscii(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length) |
| 341 | { |
321 | { |
| 342 | std::string value; |
322 | std::string value; |
| 343 | char c = 0; |
- | |
| 344 | 323 | ||
| 345 | for (unsigned int |
324 | for (unsigned int n = 0; n < bit_length; n += 8) |
| 346 | { |
325 | { |
| 347 | unsigned long |
326 | unsigned long raw_value = bitset.Read(start_bit + n, 8); |
| 348 | memcpy(&c, &raw_bit_value, sizeof(c)); |
- | |
| 349 | value += |
327 | value += (char)raw_value; |
| 350 | } |
328 | } |
| 351 | 329 | ||
| 352 | return value; |
330 | return value; |
| 353 | } |
- | |
| 354 | - | ||
| 355 | void Protocol::EncodeAscii(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length, std::string value) |
- | |
| 356 | { |
- | |
| 357 | // TODO |
- | |
| 358 | LOG.Error("EncodeAscii() is not implemented yet!"); |
- | |
| 359 | } |
331 | } |
| 360 | 332 | ||
| 361 |
|
333 | void Protocol::EncodeAscii(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length, std::string value) |
| 362 | { |
334 | { |
| 363 |
|
335 | for (unsigned int n = 0; n < bit_length; n += 8) |
| - | 336 | { |
|
| 364 |
|
337 | bitset.Write(start_bit + n, 8, value[n / 8]); |
| - | 338 | } |
|
| - | 339 | } |
|
| 365 | 340 | ||
| - | 341 | std::string Protocol::DecodeHexstring(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length) |
|
| - | 342 | { |
|
| - | 343 | std::string value; |
|
| - | 344 | char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; |
|
| - | 345 | ||
| 366 | for (unsigned int |
346 | for (unsigned int n = 0; n < bit_length; n += 4) |
| 367 | { |
347 | { |
| 368 | unsigned long raw_bit_value = bitset.Read(start_bit + |
348 | unsigned long raw_bit_value = bitset.Read(start_bit + n, 4); |
| 369 | 349 | ||
| 370 | if (raw_bit_value >= sizeof(hex)) |
350 | if (raw_bit_value >= sizeof(hex)) |
| 371 | { |
351 | { |
| 372 | value += '-'; |
352 | value += '-'; |
| 373 | } |
353 | } |
| 374 | else |
354 | else |
| 375 | { |
355 | { |
| 376 | value += hex[raw_bit_value]; |
356 | value += hex[raw_bit_value]; |
| 377 | } |
357 | } |
| 378 | } |
358 | } |
| 379 | 359 | ||
| 380 | return value; |
360 | return value; |
| 381 | } |
361 | } |
| 382 | 362 | ||
| 383 | void Protocol::EncodeHexstring(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length, std::string value) |
363 | void Protocol::EncodeHexstring(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length, std::string value) |
| 384 | { |
364 | { |
| - | 365 | int ascii_value; |
|
| - | 366 | ||
| - | 367 | for (unsigned int n = 0; n < bit_length; n += 4) |
|
| - | 368 | { |
|
| - | 369 | ascii_value = boost::lexical_cast<int>((int)std::toupper(value[n / 4])); |
|
| - | 370 | ||
| - | 371 | if (48 <= ascii_value && ascii_value <= 57) |
|
| - | 372 | { |
|
| - | 373 | bitset.Write(start_bit + n, 4, ascii_value - 48); |
|
| - | 374 | } |
|
| - | 375 | else if (65 <= ascii_value && ascii_value <= 70) |
|
| - | 376 | { |
|
| - | 377 | bitset.Write(start_bit + n, 4, ascii_value - 65); |
|
| - | 378 | } |
|
| 385 |
|
379 | else |
| - | 380 | { |
|
| 386 |
|
381 | throw std::runtime_error("This is not an hex string, " + value); |
| - | 382 | } |
|
| - | 383 | } |
|
| 387 | } |
384 | } |
| 388 | 385 | ||
| 389 | }; // namespace can |
386 | }; // namespace can |
| 390 | }; // namespace atom |
387 | }; // namespace atom |