libmodbus icon indicating copy to clipboard operation
libmodbus copied to clipboard

RFC: Use more enums

Open TheBiggerGuy opened this issue 6 years ago • 1 comments

Convert modbus_rtu_mode to a enum instead of two #define. This then extends the idea by enforcing compiler checking that these values are more correctly used.

Questions:

  • Return Types? MODBUS_API int modbus_rtu_get_serial_mode(modbus_t *ctx); vs MODBUS_API modbus_rtu_mode modbus_rtu_get_serial_mode(modbus_t *ctx); As the return value is both modbus_rtu_mode and -1 (for error) should it be typed to modbus_rtu_mode or int. Both are valid answers in C but I believe modbus_rtu_mode would lead to incorrect assumptions. Would a union be a more clear response?

  • Is the idea sounds? If so I believe that there is more examples that would work well as an enum. Example

#define MODBUS_RTU_RTS_NONE   0
#define MODBUS_RTU_RTS_UP     1
#define MODBUS_RTU_RTS_DOWN   2

Does it make the API better? Will it cause API issues in the feature to add/remove values?

  • Enum naming? Should the name include _t? modbus.h: modbus_error_recovery_mode modbus-private.h: modbus_backend_type_t

TheBiggerGuy avatar Apr 01 '19 12:04 TheBiggerGuy

I don't want to change the return types (when error or enum could be returned), I think we should accept that as a limitation of the C language (Rust rocks here...) but I like the idea to leverage the compiler to help us.

stephane avatar Aug 02 '19 11:08 stephane