From 22a68981987f6c42feba5d299fbf0f6ca3ed7cf7 Mon Sep 17 00:00:00 2001 From: Stéphane Raimbault Date: Tue, 8 Sep 2009 13:55:09 +0200 Subject: [PATCH] New API modbus_read_float() and modbus_write_float() for float values --- NEWS | 3 ++- src/modbus.c | 21 +++++++++++++++++++++ src/modbus.h | 6 ++++++ tests/unit-test-master.c | 27 +++++++++++++++++++++++++++ tests/unit-test.h | 3 +++ 5 files changed, 59 insertions(+), 1 deletion(-) diff --git a/NEWS b/NEWS index ed3a790..0cd6dde 100644 --- a/NEWS +++ b/NEWS @@ -1,5 +1,6 @@ -libmodbus 2.2.0 (2009-06-01) +libmodbus 2.2.0 (2009-XX-01) ============================ +- New API to read and write float values - New API for slave server (see MIGRATION) - New slave server able to handle multiple connections - Slave only replies to broadcast queries or queries with its slave ID diff --git a/src/modbus.c b/src/modbus.c index 4211f99..b4f437f 100644 --- a/src/modbus.c +++ b/src/modbus.c @@ -1964,3 +1964,24 @@ uint8_t get_byte_from_bits(const uint8_t *src, int address, int nb_bits) return value; } + +/* Read a float from 4 bytes in Modbus format */ +float modbus_read_float(const uint16_t *src) +{ + float real; + uint32_t ireal = (src[1] << 16) + src[0]; + real = *((float *)&ireal); + + return real; +} + +/* Write a float to 4 bytes in Modbus format */ +void modbus_write_float(float real, uint16_t *dest) +{ + uint32_t ireal; + + ireal = *((uint32_t *)&real); + /* Implicit mask 0xFFFF */ + dest[0] = ireal; + dest[1] = ireal >> 16; +} diff --git a/src/modbus.h b/src/modbus.h index 733c9d5..e90f830 100644 --- a/src/modbus.h +++ b/src/modbus.h @@ -344,6 +344,12 @@ void set_bits_from_bytes(uint8_t *dest, int address, int nb_bits, To obtain a full byte, set nb_bits to 8. */ uint8_t get_byte_from_bits(const uint8_t *src, int address, int nb_bits); +/* Read a float from 4 bytes in Modbus format */ +float modbus_read_float(const uint16_t *src); + +/* Write a float to 4 bytes in Modbus format */ +void modbus_write_float(float real, uint16_t *dest); + #ifdef __cplusplus } #endif diff --git a/tests/unit-test-master.c b/tests/unit-test-master.c index 2503df0..b00dd31 100644 --- a/tests/unit-test-master.c +++ b/tests/unit-test-master.c @@ -36,6 +36,7 @@ int main(void) int address; int nb_points; int ret; + float real; /* RTU parity : none, even, odd */ /* modbus_init_rtu(&mb_param, "/dev/ttyS0", 19200, "none", 8, 1, SLAVE); */ @@ -253,6 +254,30 @@ int main(void) printf("OK\n"); + printf("\nTEST FLOATS\n"); + /** FLOAT **/ + printf("1/2 Write float: "); + modbus_write_float(UT_REAL, tab_rp_registers); + if (tab_rp_registers[1] == (UT_IREAL >> 16) && + tab_rp_registers[0] == (UT_IREAL & 0xFFFF)) { + printf("OK\n"); + } else { + printf("FAILED (%x != %x)\n", + *((uint32_t *)tab_rp_registers), UT_IREAL); + goto close; + } + + printf("2/2 Read float: "); + real = modbus_read_float(tab_rp_registers); + if (real == UT_REAL) { + printf("OK\n"); + } else { + printf("FAILED (%f != %f)\n", real, UT_REAL); + goto close; + } + + printf("\nAt this point, error messages doesn't mean the test has failed\n"); + /** ILLEGAL DATA ADDRESS **/ printf("\nTEST ILLEGAL DATA ADDRESS:\n"); @@ -465,6 +490,8 @@ int main(void) } free(tab_rp_registers_bad); + printf("\nALL TESTS PASS WITH SUCCESS.\n"); + close: /* Free the memory */ free(tab_rp_status); diff --git a/tests/unit-test.h b/tests/unit-test.h index bc335b0..4984f95 100644 --- a/tests/unit-test.h +++ b/tests/unit-test.h @@ -42,4 +42,7 @@ const uint16_t UT_INPUT_REGISTERS_ADDRESS = 0x08; const uint16_t UT_INPUT_REGISTERS_NB_POINTS = 0x1; const uint16_t UT_INPUT_REGISTERS_TAB[] = { 0x000A }; +const float UT_REAL = 916.540649; +const uint32_t UT_IREAL = 0x4465229a; + #endif /* _UNIT_TEST_H_ */ -- libgit2 0.21.4