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,6 +84,8 @@ typedef struct _modbus_rtu {
84 #endif 84 #endif
85 #if HAVE_DECL_TIOCSRS485 85 #if HAVE_DECL_TIOCSRS485
86 int serial_mode; 86 int serial_mode;
  87 +#endif
  88 +#if HAVE_DECL_TIOCM_RTS
87 int rts; 89 int rts;
88 #endif 90 #endif
89 } modbus_rtu_t; 91 } modbus_rtu_t;
src/modbus-rtu.c
@@ -31,8 +31,11 @@ @@ -31,8 +31,11 @@
31 #include "modbus-rtu.h" 31 #include "modbus-rtu.h"
32 #include "modbus-rtu-private.h" 32 #include "modbus-rtu-private.h"
33 33
34 -#if HAVE_DECL_TIOCSRS485 34 +#if HAVE_DECL_TIOCSRS485 || HAVE_DECL_TIOCM_RTS
35 #include <sys/ioctl.h> 35 #include <sys/ioctl.h>
  36 +#endif
  37 +
  38 +#if HAVE_DECL_TIOCSRS485
36 #include <linux/serial.h> 39 #include <linux/serial.h>
37 #endif 40 #endif
38 41
@@ -279,6 +282,7 @@ ssize_t _modbus_rtu_send(modbus_t *ctx, const uint8_t *req, int req_length) @@ -279,6 +282,7 @@ ssize_t _modbus_rtu_send(modbus_t *ctx, const uint8_t *req, int req_length)
279 DWORD n_bytes = 0; 282 DWORD n_bytes = 0;
280 return (WriteFile(ctx_rtu->w_ser.fd, req, req_length, &n_bytes, NULL)) ? n_bytes : -1; 283 return (WriteFile(ctx_rtu->w_ser.fd, req, req_length, &n_bytes, NULL)) ? n_bytes : -1;
281 #else 284 #else
  285 +#if HAVE_DECL_TIOCM_RTS
282 modbus_rtu_t *ctx_rtu = ctx->backend_data; 286 modbus_rtu_t *ctx_rtu = ctx->backend_data;
283 if (ctx_rtu->rts != MODBUS_RTU_RTS_NONE) { 287 if (ctx_rtu->rts != MODBUS_RTU_RTS_NONE) {
284 ssize_t size; 288 ssize_t size;
@@ -297,9 +301,12 @@ ssize_t _modbus_rtu_send(modbus_t *ctx, const uint8_t *req, int req_length) @@ -297,9 +301,12 @@ ssize_t _modbus_rtu_send(modbus_t *ctx, const uint8_t *req, int req_length)
297 301
298 return size; 302 return size;
299 } else { 303 } else {
  304 +#endif
300 return write(ctx->s, req, req_length); 305 return write(ctx->s, req, req_length);
  306 +#if HAVE_DECL_TIOCM_RTS
301 } 307 }
302 #endif 308 #endif
  309 +#endif
303 } 310 }
304 311
305 ssize_t _modbus_rtu_recv(modbus_t *ctx, uint8_t *rsp, int rsp_length) 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,15 +767,6 @@ static int _modbus_rtu_connect(modbus_t *ctx)
760 } 767 }
761 #endif 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 return 0; 770 return 0;
773 } 771 }
774 772
@@ -830,8 +828,8 @@ int modbus_rtu_get_serial_mode(modbus_t *ctx) { @@ -830,8 +828,8 @@ int modbus_rtu_get_serial_mode(modbus_t *ctx) {
830 828
831 int modbus_rtu_set_rts(modbus_t *ctx, int mode) 829 int modbus_rtu_set_rts(modbus_t *ctx, int mode)
832 { 830 {
833 -#if HAVE_DECL_TIOCM_RTS  
834 if (ctx->backend->backend_type == _MODBUS_BACKEND_TYPE_RTU) { 831 if (ctx->backend->backend_type == _MODBUS_BACKEND_TYPE_RTU) {
  832 +#if HAVE_DECL_TIOCM_RTS
835 modbus_rtu_t *ctx_rtu = ctx->backend_data; 833 modbus_rtu_t *ctx_rtu = ctx->backend_data;
836 834
837 if (mode == MODBUS_RTU_RTS_NONE || mode == MODBUS_RTU_RTS_UP || 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,8 +841,14 @@ int modbus_rtu_set_rts(modbus_t *ctx, int mode)
843 841
844 return 0; 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 #endif 850 #endif
  851 + }
848 /* Wrong backend or invalid mode specified */ 852 /* Wrong backend or invalid mode specified */
849 errno = EINVAL; 853 errno = EINVAL;
850 return -1; 854 return -1;
@@ -852,8 +856,16 @@ int modbus_rtu_set_rts(modbus_t *ctx, int mode) @@ -852,8 +856,16 @@ int modbus_rtu_set_rts(modbus_t *ctx, int mode)
852 856
853 int modbus_rtu_get_rts(modbus_t *ctx) { 857 int modbus_rtu_get_rts(modbus_t *ctx) {
854 if (ctx->backend->backend_type == _MODBUS_BACKEND_TYPE_RTU) { 858 if (ctx->backend->backend_type == _MODBUS_BACKEND_TYPE_RTU) {
  859 +#if HAVE_DECL_TIOCM_RTS
855 modbus_rtu_t *ctx_rtu = ctx->backend_data; 860 modbus_rtu_t *ctx_rtu = ctx->backend_data;
856 return ctx_rtu->rts; 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 } else { 869 } else {
858 errno = EINVAL; 870 errno = EINVAL;
859 return -1; 871 return -1;
@@ -1009,5 +1021,15 @@ modbus_t* modbus_new_rtu(const char *device, @@ -1009,5 +1021,15 @@ modbus_t* modbus_new_rtu(const char *device,
1009 ctx_rtu->data_bit = data_bit; 1021 ctx_rtu->data_bit = data_bit;
1010 ctx_rtu->stop_bit = stop_bit; 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 return ctx; 1034 return ctx;
1013 } 1035 }