|
@@ -31,8 +31,11 @@
|
|
#include "modbus-rtu.h"
|
|
#include "modbus-rtu.h"
|
|
#include "modbus-rtu-private.h"
|
|
#include "modbus-rtu-private.h"
|
|
|
|
|
|
-#if HAVE_DECL_TIOCSRS485
|
|
|
|
|
|
+#if HAVE_DECL_TIOCSRS485 || HAVE_DECL_TIOCM_RTS
|
|
#include <sys/ioctl.h>
|
|
#include <sys/ioctl.h>
|
|
|
|
+#endif
|
|
|
|
+
|
|
|
|
+#if HAVE_DECL_TIOCSRS485
|
|
#include <linux/serial.h>
|
|
#include <linux/serial.h>
|
|
#endif
|
|
#endif
|
|
|
|
|
|
@@ -279,6 +282,7 @@ ssize_t _modbus_rtu_send(modbus_t *ctx, const uint8_t *req, int req_length)
|
|
DWORD n_bytes = 0;
|
|
DWORD n_bytes = 0;
|
|
return (WriteFile(ctx_rtu->w_ser.fd, req, req_length, &n_bytes, NULL)) ? n_bytes : -1;
|
|
return (WriteFile(ctx_rtu->w_ser.fd, req, req_length, &n_bytes, NULL)) ? n_bytes : -1;
|
|
#else
|
|
#else
|
|
|
|
+#if HAVE_DECL_TIOCM_RTS
|
|
modbus_rtu_t *ctx_rtu = ctx->backend_data;
|
|
modbus_rtu_t *ctx_rtu = ctx->backend_data;
|
|
if (ctx_rtu->rts != MODBUS_RTU_RTS_NONE) {
|
|
if (ctx_rtu->rts != MODBUS_RTU_RTS_NONE) {
|
|
ssize_t size;
|
|
ssize_t size;
|
|
@@ -297,9 +301,12 @@ ssize_t _modbus_rtu_send(modbus_t *ctx, const uint8_t *req, int req_length)
|
|
|
|
|
|
return size;
|
|
return size;
|
|
} else {
|
|
} else {
|
|
|
|
+#endif
|
|
return write(ctx->s, req, req_length);
|
|
return write(ctx->s, req, req_length);
|
|
|
|
+#if HAVE_DECL_TIOCM_RTS
|
|
}
|
|
}
|
|
#endif
|
|
#endif
|
|
|
|
+#endif
|
|
}
|
|
}
|
|
|
|
|
|
ssize_t _modbus_rtu_recv(modbus_t *ctx, uint8_t *rsp, int rsp_length)
|
|
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
|
|
#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;
|
|
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)
|
|
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 (ctx->backend->backend_type == _MODBUS_BACKEND_TYPE_RTU) {
|
|
|
|
+#if HAVE_DECL_TIOCM_RTS
|
|
modbus_rtu_t *ctx_rtu = ctx->backend_data;
|
|
modbus_rtu_t *ctx_rtu = ctx->backend_data;
|
|
|
|
|
|
if (mode == MODBUS_RTU_RTS_NONE || mode == MODBUS_RTU_RTS_UP ||
|
|
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;
|
|
return 0;
|
|
}
|
|
}
|
|
- }
|
|
|
|
|
|
+#else
|
|
|
|
+ if (ctx->debug) {
|
|
|
|
+ fprintf(stderr, "This function isn't supported on your platform\n");
|
|
|
|
+ }
|
|
|
|
+ errno = ENOTSUP;
|
|
|
|
+ return -1;
|
|
#endif
|
|
#endif
|
|
|
|
+ }
|
|
/* Wrong backend or invalid mode specified */
|
|
/* Wrong backend or invalid mode specified */
|
|
errno = EINVAL;
|
|
errno = EINVAL;
|
|
return -1;
|
|
return -1;
|
|
@@ -852,8 +856,16 @@ int modbus_rtu_set_rts(modbus_t *ctx, int mode)
|
|
|
|
|
|
int modbus_rtu_get_rts(modbus_t *ctx) {
|
|
int modbus_rtu_get_rts(modbus_t *ctx) {
|
|
if (ctx->backend->backend_type == _MODBUS_BACKEND_TYPE_RTU) {
|
|
if (ctx->backend->backend_type == _MODBUS_BACKEND_TYPE_RTU) {
|
|
|
|
+#if HAVE_DECL_TIOCM_RTS
|
|
modbus_rtu_t *ctx_rtu = ctx->backend_data;
|
|
modbus_rtu_t *ctx_rtu = ctx->backend_data;
|
|
return ctx_rtu->rts;
|
|
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 {
|
|
} else {
|
|
errno = EINVAL;
|
|
errno = EINVAL;
|
|
return -1;
|
|
return -1;
|
|
@@ -1009,5 +1021,15 @@ modbus_t* modbus_new_rtu(const char *device,
|
|
ctx_rtu->data_bit = data_bit;
|
|
ctx_rtu->data_bit = data_bit;
|
|
ctx_rtu->stop_bit = stop_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;
|
|
return ctx;
|
|
}
|
|
}
|