From cf6449145a13f53aef969e89599be86f888539f3 Mon Sep 17 00:00:00 2001 From: Habu Date: Wed, 25 Jul 2012 17:21:44 +0900 Subject: [PATCH] Calculate RTS activation time by send length --- src/modbus-rtu-private.h | 1 + src/modbus-rtu.c | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/modbus-rtu-private.h b/src/modbus-rtu-private.h index fafa1cd..533ddbd 100644 --- a/src/modbus-rtu-private.h +++ b/src/modbus-rtu-private.h @@ -81,6 +81,7 @@ typedef struct _modbus_rtu { #endif #if HAVE_DECL_TIOCM_RTS int rts; + int onebyte_time; #endif /* To handle many slaves on the same link */ int confirmation_to_ignore; diff --git a/src/modbus-rtu.c b/src/modbus-rtu.c index 40a9bb7..786a20b 100644 --- a/src/modbus-rtu.c +++ b/src/modbus-rtu.c @@ -296,7 +296,7 @@ ssize_t _modbus_rtu_send(modbus_t *ctx, const uint8_t *req, int req_length) size = write(ctx->s, req, req_length); - usleep(_MODBUS_RTU_TIME_BETWEEN_RTS_SWITCH); + usleep(ctx_rtu->onebyte_time * req_length + _MODBUS_RTU_TIME_BETWEEN_RTS_SWITCH); _modbus_rtu_ioctl_rts(ctx->s, ctx_rtu->rts != MODBUS_RTU_RTS_UP); return size; @@ -1060,6 +1060,9 @@ modbus_t* modbus_new_rtu(const char *device, #if HAVE_DECL_TIOCM_RTS /* The RTS use has been set by default */ ctx_rtu->rts = MODBUS_RTU_RTS_NONE; + + /* Calculate estimated time in micro second to send one byte */ + ctx_rtu->onebyte_time = (1000 * 1000) * (1 + data_bit + (parity == 'N' ? 0 : 1) + stop_bit) / baud; #endif ctx_rtu->confirmation_to_ignore = FALSE; -- libgit2 0.21.4