diff --git a/src/modbus-rtu-private.h b/src/modbus-rtu-private.h index d0bc615..f314971 100644 --- a/src/modbus-rtu-private.h +++ b/src/modbus-rtu-private.h @@ -84,6 +84,8 @@ typedef struct _modbus_rtu { #endif #if HAVE_DECL_TIOCSRS485 int serial_mode; +#endif +#if HAVE_DECL_TIOCM_RTS int rts; #endif } modbus_rtu_t; diff --git a/src/modbus-rtu.c b/src/modbus-rtu.c index ea192d1..276c344 100644 --- a/src/modbus-rtu.c +++ b/src/modbus-rtu.c @@ -31,8 +31,11 @@ #include "modbus-rtu.h" #include "modbus-rtu-private.h" -#if HAVE_DECL_TIOCSRS485 +#if HAVE_DECL_TIOCSRS485 || HAVE_DECL_TIOCM_RTS #include +#endif + +#if HAVE_DECL_TIOCSRS485 #include #endif @@ -279,6 +282,7 @@ ssize_t _modbus_rtu_send(modbus_t *ctx, const uint8_t *req, int req_length) DWORD n_bytes = 0; return (WriteFile(ctx_rtu->w_ser.fd, req, req_length, &n_bytes, NULL)) ? n_bytes : -1; #else +#if HAVE_DECL_TIOCM_RTS modbus_rtu_t *ctx_rtu = ctx->backend_data; if (ctx_rtu->rts != MODBUS_RTU_RTS_NONE) { ssize_t size; @@ -297,9 +301,12 @@ ssize_t _modbus_rtu_send(modbus_t *ctx, const uint8_t *req, int req_length) return size; } else { +#endif return write(ctx->s, req, req_length); +#if HAVE_DECL_TIOCM_RTS } #endif +#endif } 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) } #endif -#if HAVE_DECL_TIOCSRS485 - /* The RS232 mode has been set by default */ - ctx_rtu->serial_mode = MODBUS_RTU_RS232; - - /* The RTS use has been set by default */ - ctx_rtu->rts = MODBUS_RTU_RTS_NONE; - -#endif - return 0; } @@ -830,8 +828,8 @@ int modbus_rtu_get_serial_mode(modbus_t *ctx) { int modbus_rtu_set_rts(modbus_t *ctx, int mode) { -#if HAVE_DECL_TIOCM_RTS if (ctx->backend->backend_type == _MODBUS_BACKEND_TYPE_RTU) { +#if HAVE_DECL_TIOCM_RTS modbus_rtu_t *ctx_rtu = ctx->backend_data; 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) return 0; } - } +#else + if (ctx->debug) { + fprintf(stderr, "This function isn't supported on your platform\n"); + } + errno = ENOTSUP; + return -1; #endif + } /* Wrong backend or invalid mode specified */ errno = EINVAL; return -1; @@ -852,8 +856,16 @@ int modbus_rtu_set_rts(modbus_t *ctx, int mode) int modbus_rtu_get_rts(modbus_t *ctx) { if (ctx->backend->backend_type == _MODBUS_BACKEND_TYPE_RTU) { +#if HAVE_DECL_TIOCM_RTS modbus_rtu_t *ctx_rtu = ctx->backend_data; return ctx_rtu->rts; +#else + if (ctx->debug) { + fprintf(stderr, "This function isn't supported on your platform\n"); + } + errno = ENOTSUP; + return -1; +#endif } else { errno = EINVAL; return -1; @@ -1009,5 +1021,15 @@ modbus_t* modbus_new_rtu(const char *device, ctx_rtu->data_bit = data_bit; ctx_rtu->stop_bit = stop_bit; +#if HAVE_DECL_TIOCSRS485 + /* The RS232 mode has been set by default */ + ctx_rtu->serial_mode = MODBUS_RTU_RS232; +#endif + +#if HAVE_DECL_TIOCM_RTS + /* The RTS use has been set by default */ + ctx_rtu->rts = MODBUS_RTU_RTS_NONE; +#endif + return ctx; }