Commit 16e14ae1aa820114537b47228240e45f194db175

Authored by Stéphane Raimbault
1 parent 1941fd4a

Fixed compilation break on platforms without TIOCSRS485

The serial mode and rts flags are now initialized in new function.
Original patch provided by Jaime Alberto Silva
<jaime@sgautomatizacion.com>.
src/modbus-rtu-private.h
... ... @@ -84,6 +84,8 @@ typedef struct _modbus_rtu {
84 84 #endif
85 85 #if HAVE_DECL_TIOCSRS485
86 86 int serial_mode;
  87 +#endif
  88 +#if HAVE_DECL_TIOCM_RTS
87 89 int rts;
88 90 #endif
89 91 } modbus_rtu_t;
... ...
src/modbus-rtu.c
... ... @@ -31,8 +31,11 @@
31 31 #include "modbus-rtu.h"
32 32 #include "modbus-rtu-private.h"
33 33  
34   -#if HAVE_DECL_TIOCSRS485
  34 +#if HAVE_DECL_TIOCSRS485 || HAVE_DECL_TIOCM_RTS
35 35 #include <sys/ioctl.h>
  36 +#endif
  37 +
  38 +#if HAVE_DECL_TIOCSRS485
36 39 #include <linux/serial.h>
37 40 #endif
38 41  
... ... @@ -279,6 +282,7 @@ ssize_t _modbus_rtu_send(modbus_t *ctx, const uint8_t *req, int req_length)
279 282 DWORD n_bytes = 0;
280 283 return (WriteFile(ctx_rtu->w_ser.fd, req, req_length, &n_bytes, NULL)) ? n_bytes : -1;
281 284 #else
  285 +#if HAVE_DECL_TIOCM_RTS
282 286 modbus_rtu_t *ctx_rtu = ctx->backend_data;
283 287 if (ctx_rtu->rts != MODBUS_RTU_RTS_NONE) {
284 288 ssize_t size;
... ... @@ -297,9 +301,12 @@ ssize_t _modbus_rtu_send(modbus_t *ctx, const uint8_t *req, int req_length)
297 301  
298 302 return size;
299 303 } else {
  304 +#endif
300 305 return write(ctx->s, req, req_length);
  306 +#if HAVE_DECL_TIOCM_RTS
301 307 }
302 308 #endif
  309 +#endif
303 310 }
304 311  
305 312 ssize_t _modbus_rtu_recv(modbus_t *ctx, uint8_t *rsp, int rsp_length)
... ... @@ -760,15 +767,6 @@ static int _modbus_rtu_connect(modbus_t *ctx)
760 767 }
761 768 #endif
762 769  
763   -#if HAVE_DECL_TIOCSRS485
764   - /* The RS232 mode has been set by default */
765   - ctx_rtu->serial_mode = MODBUS_RTU_RS232;
766   -
767   - /* The RTS use has been set by default */
768   - ctx_rtu->rts = MODBUS_RTU_RTS_NONE;
769   -
770   -#endif
771   -
772 770 return 0;
773 771 }
774 772  
... ... @@ -830,8 +828,8 @@ int modbus_rtu_get_serial_mode(modbus_t *ctx) {
830 828  
831 829 int modbus_rtu_set_rts(modbus_t *ctx, int mode)
832 830 {
833   -#if HAVE_DECL_TIOCM_RTS
834 831 if (ctx->backend->backend_type == _MODBUS_BACKEND_TYPE_RTU) {
  832 +#if HAVE_DECL_TIOCM_RTS
835 833 modbus_rtu_t *ctx_rtu = ctx->backend_data;
836 834  
837 835 if (mode == MODBUS_RTU_RTS_NONE || mode == MODBUS_RTU_RTS_UP ||
... ... @@ -843,8 +841,14 @@ int modbus_rtu_set_rts(modbus_t *ctx, int mode)
843 841  
844 842 return 0;
845 843 }
846   - }
  844 +#else
  845 + if (ctx->debug) {
  846 + fprintf(stderr, "This function isn't supported on your platform\n");
  847 + }
  848 + errno = ENOTSUP;
  849 + return -1;
847 850 #endif
  851 + }
848 852 /* Wrong backend or invalid mode specified */
849 853 errno = EINVAL;
850 854 return -1;
... ... @@ -852,8 +856,16 @@ int modbus_rtu_set_rts(modbus_t *ctx, int mode)
852 856  
853 857 int modbus_rtu_get_rts(modbus_t *ctx) {
854 858 if (ctx->backend->backend_type == _MODBUS_BACKEND_TYPE_RTU) {
  859 +#if HAVE_DECL_TIOCM_RTS
855 860 modbus_rtu_t *ctx_rtu = ctx->backend_data;
856 861 return ctx_rtu->rts;
  862 +#else
  863 + if (ctx->debug) {
  864 + fprintf(stderr, "This function isn't supported on your platform\n");
  865 + }
  866 + errno = ENOTSUP;
  867 + return -1;
  868 +#endif
857 869 } else {
858 870 errno = EINVAL;
859 871 return -1;
... ... @@ -1009,5 +1021,15 @@ modbus_t* modbus_new_rtu(const char *device,
1009 1021 ctx_rtu->data_bit = data_bit;
1010 1022 ctx_rtu->stop_bit = stop_bit;
1011 1023  
  1024 +#if HAVE_DECL_TIOCSRS485
  1025 + /* The RS232 mode has been set by default */
  1026 + ctx_rtu->serial_mode = MODBUS_RTU_RS232;
  1027 +#endif
  1028 +
  1029 +#if HAVE_DECL_TIOCM_RTS
  1030 + /* The RTS use has been set by default */
  1031 + ctx_rtu->rts = MODBUS_RTU_RTS_NONE;
  1032 +#endif
  1033 +
1012 1034 return ctx;
1013 1035 }
... ...