Commit 997231b7991f08bf2b50111017db92638512210f
1 parent
e6b64888
New functions to set/get swapped floats (LSB)
Showing
3 changed files
with
50 additions
and
4 deletions
src/modbus-data.c
| @@ -24,6 +24,7 @@ | @@ -24,6 +24,7 @@ | ||
| 24 | #endif | 24 | #endif |
| 25 | #include <string.h> | 25 | #include <string.h> |
| 26 | #include <assert.h> | 26 | #include <assert.h> |
| 27 | +#include <byteswap.h> | ||
| 27 | 28 | ||
| 28 | #include "modbus.h" | 29 | #include "modbus.h" |
| 29 | 30 | ||
| @@ -75,7 +76,7 @@ uint8_t modbus_get_byte_from_bits(const uint8_t *src, int index, | @@ -75,7 +76,7 @@ uint8_t modbus_get_byte_from_bits(const uint8_t *src, int index, | ||
| 75 | return value; | 76 | return value; |
| 76 | } | 77 | } |
| 77 | 78 | ||
| 78 | -/* Get a float from 4 bytes in Modbus format */ | 79 | +/* Get a float from 4 bytes in Modbus format (ABCD) */ |
| 79 | float modbus_get_float(const uint16_t *src) | 80 | float modbus_get_float(const uint16_t *src) |
| 80 | { | 81 | { |
| 81 | float f; | 82 | float f; |
| @@ -87,7 +88,19 @@ float modbus_get_float(const uint16_t *src) | @@ -87,7 +88,19 @@ float modbus_get_float(const uint16_t *src) | ||
| 87 | return f; | 88 | return f; |
| 88 | } | 89 | } |
| 89 | 90 | ||
| 90 | -/* Set a float to 4 bytes in Modbus format */ | 91 | +/* Get a float from 4 bytes in swapped Modbus format (DCBA) */ |
| 92 | +float modbus_get_float_swapped(const uint16_t *src) | ||
| 93 | +{ | ||
| 94 | + float f; | ||
| 95 | + uint32_t i; | ||
| 96 | + | ||
| 97 | + i = bswap_32((((uint32_t)src[1]) << 16) + src[0]); | ||
| 98 | + memcpy(&f, &i, sizeof(float)); | ||
| 99 | + | ||
| 100 | + return f; | ||
| 101 | +} | ||
| 102 | + | ||
| 103 | +/* Set a float to 4 bytes in Modbus format (ABCD) */ | ||
| 91 | void modbus_set_float(float f, uint16_t *dest) | 104 | void modbus_set_float(float f, uint16_t *dest) |
| 92 | { | 105 | { |
| 93 | uint32_t i; | 106 | uint32_t i; |
| @@ -96,3 +109,14 @@ void modbus_set_float(float f, uint16_t *dest) | @@ -96,3 +109,14 @@ void modbus_set_float(float f, uint16_t *dest) | ||
| 96 | dest[0] = (uint16_t)i; | 109 | dest[0] = (uint16_t)i; |
| 97 | dest[1] = (uint16_t)(i >> 16); | 110 | dest[1] = (uint16_t)(i >> 16); |
| 98 | } | 111 | } |
| 112 | + | ||
| 113 | +/* Set a float to 4 bytes in swapped Modbus format (DCBA) */ | ||
| 114 | +void modbus_set_float_swapped(float f, uint16_t *dest) | ||
| 115 | +{ | ||
| 116 | + uint32_t i; | ||
| 117 | + | ||
| 118 | + memcpy(&i, &f, sizeof(uint32_t)); | ||
| 119 | + i = bswap_32(i); | ||
| 120 | + dest[0] = (uint16_t)i; | ||
| 121 | + dest[1] = (uint16_t)(i >> 16); | ||
| 122 | +} |
src/modbus.h
| @@ -226,7 +226,9 @@ EXPORT void modbus_set_bits_from_bytes(uint8_t *dest, int index, unsigned int nb | @@ -226,7 +226,9 @@ EXPORT void modbus_set_bits_from_bytes(uint8_t *dest, int index, unsigned int nb | ||
| 226 | const uint8_t *tab_byte); | 226 | const uint8_t *tab_byte); |
| 227 | EXPORT uint8_t modbus_get_byte_from_bits(const uint8_t *src, int index, unsigned int nb_bits); | 227 | EXPORT uint8_t modbus_get_byte_from_bits(const uint8_t *src, int index, unsigned int nb_bits); |
| 228 | EXPORT float modbus_get_float(const uint16_t *src); | 228 | EXPORT float modbus_get_float(const uint16_t *src); |
| 229 | +EXPORT float modbus_get_float_swapped(const uint16_t *src); | ||
| 229 | EXPORT void modbus_set_float(float f, uint16_t *dest); | 230 | EXPORT void modbus_set_float(float f, uint16_t *dest); |
| 231 | +EXPORT void modbus_set_float_swapped(float f, uint16_t *dest); | ||
| 230 | 232 | ||
| 231 | #include "modbus-tcp.h" | 233 | #include "modbus-tcp.h" |
| 232 | #include "modbus-rtu.h" | 234 | #include "modbus-rtu.h" |
tests/unit-test-client.c
| @@ -319,7 +319,7 @@ int main(int argc, char *argv[]) | @@ -319,7 +319,7 @@ int main(int argc, char *argv[]) | ||
| 319 | 319 | ||
| 320 | printf("\nTEST FLOATS\n"); | 320 | printf("\nTEST FLOATS\n"); |
| 321 | /** FLOAT **/ | 321 | /** FLOAT **/ |
| 322 | - printf("1/2 Set float: "); | 322 | + printf("1/4 Set float: "); |
| 323 | modbus_set_float(UT_REAL, tab_rp_registers); | 323 | modbus_set_float(UT_REAL, tab_rp_registers); |
| 324 | if (tab_rp_registers[1] == (UT_IREAL >> 16) && | 324 | if (tab_rp_registers[1] == (UT_IREAL >> 16) && |
| 325 | tab_rp_registers[0] == (UT_IREAL & 0xFFFF)) { | 325 | tab_rp_registers[0] == (UT_IREAL & 0xFFFF)) { |
| @@ -333,7 +333,7 @@ int main(int argc, char *argv[]) | @@ -333,7 +333,7 @@ int main(int argc, char *argv[]) | ||
| 333 | goto close; | 333 | goto close; |
| 334 | } | 334 | } |
| 335 | 335 | ||
| 336 | - printf("2/2 Get float: "); | 336 | + printf("2/4 Get float: "); |
| 337 | real = modbus_get_float(tab_rp_registers); | 337 | real = modbus_get_float(tab_rp_registers); |
| 338 | if (real == UT_REAL) { | 338 | if (real == UT_REAL) { |
| 339 | printf("OK\n"); | 339 | printf("OK\n"); |
| @@ -342,6 +342,26 @@ int main(int argc, char *argv[]) | @@ -342,6 +342,26 @@ int main(int argc, char *argv[]) | ||
| 342 | goto close; | 342 | goto close; |
| 343 | } | 343 | } |
| 344 | 344 | ||
| 345 | + printf("3/4 Set float swapped: "); | ||
| 346 | + modbus_set_float_swapped(UT_REAL, tab_rp_registers); | ||
| 347 | + if (tab_rp_registers[1] == (UT_IREAL_SWAPPED >> 16) && | ||
| 348 | + tab_rp_registers[0] == (UT_IREAL_SWAPPED & 0xFFFF)) { | ||
| 349 | + printf("OK\n"); | ||
| 350 | + } else { | ||
| 351 | + ireal = (uint32_t) tab_rp_registers[0] & 0xFFFF; | ||
| 352 | + ireal |= (uint32_t) tab_rp_registers[1] << 16; | ||
| 353 | + printf("FAILED (%x != %x)\n", ireal, UT_IREAL_SWAPPED); | ||
| 354 | + goto close; | ||
| 355 | + } | ||
| 356 | + | ||
| 357 | + printf("4/4 Get float swapped: "); | ||
| 358 | + real = modbus_get_float_swapped(tab_rp_registers); | ||
| 359 | + if (real == UT_REAL) { | ||
| 360 | + printf("OK\n"); | ||
| 361 | + } else { | ||
| 362 | + printf("FAILED (%f != %f)\n", real, UT_REAL); | ||
| 363 | + goto close; | ||
| 364 | + } | ||
| 345 | printf("\nAt this point, error messages doesn't mean the test has failed\n"); | 365 | printf("\nAt this point, error messages doesn't mean the test has failed\n"); |
| 346 | 366 | ||
| 347 | /** ILLEGAL DATA ADDRESS **/ | 367 | /** ILLEGAL DATA ADDRESS **/ |