Commit cf6449145a13f53aef969e89599be86f888539f3
Committed by
Stéphane Raimbault
1 parent
541804b3
Calculate RTS activation time by send length
Showing
2 changed files
with
5 additions
and
1 deletions
src/modbus-rtu-private.h
| @@ -81,6 +81,7 @@ typedef struct _modbus_rtu { | @@ -81,6 +81,7 @@ typedef struct _modbus_rtu { | ||
| 81 | #endif | 81 | #endif |
| 82 | #if HAVE_DECL_TIOCM_RTS | 82 | #if HAVE_DECL_TIOCM_RTS |
| 83 | int rts; | 83 | int rts; |
| 84 | + int onebyte_time; | ||
| 84 | #endif | 85 | #endif |
| 85 | /* To handle many slaves on the same link */ | 86 | /* To handle many slaves on the same link */ |
| 86 | int confirmation_to_ignore; | 87 | int confirmation_to_ignore; |
src/modbus-rtu.c
| @@ -296,7 +296,7 @@ ssize_t _modbus_rtu_send(modbus_t *ctx, const uint8_t *req, int req_length) | @@ -296,7 +296,7 @@ ssize_t _modbus_rtu_send(modbus_t *ctx, const uint8_t *req, int req_length) | ||
| 296 | 296 | ||
| 297 | size = write(ctx->s, req, req_length); | 297 | size = write(ctx->s, req, req_length); |
| 298 | 298 | ||
| 299 | - usleep(_MODBUS_RTU_TIME_BETWEEN_RTS_SWITCH); | 299 | + usleep(ctx_rtu->onebyte_time * req_length + _MODBUS_RTU_TIME_BETWEEN_RTS_SWITCH); |
| 300 | _modbus_rtu_ioctl_rts(ctx->s, ctx_rtu->rts != MODBUS_RTU_RTS_UP); | 300 | _modbus_rtu_ioctl_rts(ctx->s, ctx_rtu->rts != MODBUS_RTU_RTS_UP); |
| 301 | 301 | ||
| 302 | return size; | 302 | return size; |
| @@ -1060,6 +1060,9 @@ modbus_t* modbus_new_rtu(const char *device, | @@ -1060,6 +1060,9 @@ modbus_t* modbus_new_rtu(const char *device, | ||
| 1060 | #if HAVE_DECL_TIOCM_RTS | 1060 | #if HAVE_DECL_TIOCM_RTS |
| 1061 | /* The RTS use has been set by default */ | 1061 | /* The RTS use has been set by default */ |
| 1062 | ctx_rtu->rts = MODBUS_RTU_RTS_NONE; | 1062 | ctx_rtu->rts = MODBUS_RTU_RTS_NONE; |
| 1063 | + | ||
| 1064 | + /* Calculate estimated time in micro second to send one byte */ | ||
| 1065 | + ctx_rtu->onebyte_time = (1000 * 1000) * (1 + data_bit + (parity == 'N' ? 0 : 1) + stop_bit) / baud; | ||
| 1063 | #endif | 1066 | #endif |
| 1064 | 1067 | ||
| 1065 | ctx_rtu->confirmation_to_ignore = FALSE; | 1068 | ctx_rtu->confirmation_to_ignore = FALSE; |