Skip to content

Commit c726c9d

Browse files
committed
Fix connection check for Windows RTU (closes #660, #662)
1 parent 515960c commit c726c9d

4 files changed

Lines changed: 24 additions & 1 deletion

File tree

src/modbus-private.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ typedef struct _modbus_backend {
8686
const uint8_t *rsp,
8787
int rsp_length);
8888
int (*connect)(modbus_t *ctx);
89+
unsigned int (*is_connected)(modbus_t *ctx);
8990
void (*close)(modbus_t *ctx);
9091
int (*flush)(modbus_t *ctx);
9192
int (*select)(modbus_t *ctx, fd_set *rset, struct timeval *tv, int msg_length);

src/modbus-rtu.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,19 @@ static int _modbus_rtu_connect(modbus_t *ctx)
725725
return 0;
726726
}
727727

728+
// FIXME Temporary solution before rewriting Windows RTU backend
729+
static unsigned int _modbus_rtu_is_connected(modbus_t *ctx)
730+
{
731+
#if defined(_WIN32)
732+
modbus_rtu_t *ctx_rtu = ctx->backend_data;
733+
734+
/* Check if file handle is valid */
735+
return ctx_rtu->w_ser.fd != INVALID_HANDLE_VALUE;
736+
#else
737+
return ctx->s >= 0;
738+
#endif
739+
}
740+
728741
int modbus_rtu_set_serial_mode(modbus_t *ctx, int mode)
729742
{
730743
if (ctx == NULL) {
@@ -1043,6 +1056,7 @@ const modbus_backend_t _modbus_rtu_backend = {
10431056
_modbus_rtu_check_integrity,
10441057
_modbus_rtu_pre_check_confirmation,
10451058
_modbus_rtu_connect,
1059+
_modbus_rtu_is_connected,
10461060
_modbus_rtu_close,
10471061
_modbus_rtu_flush,
10481062
_modbus_rtu_select,

src/modbus-tcp.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,11 @@ static int _modbus_tcp_pi_connect(modbus_t *ctx)
449449
return 0;
450450
}
451451

452+
static unsigned int _modbus_tcp_is_connected(modbus_t *ctx)
453+
{
454+
return ctx->s >= 0;
455+
}
456+
452457
/* Closes the network connection and socket in TCP mode */
453458
static void _modbus_tcp_close(modbus_t *ctx)
454459
{
@@ -816,6 +821,7 @@ const modbus_backend_t _modbus_tcp_backend = {
816821
_modbus_tcp_check_integrity,
817822
_modbus_tcp_pre_check_confirmation,
818823
_modbus_tcp_connect,
824+
_modbus_tcp_is_connected,
819825
_modbus_tcp_close,
820826
_modbus_tcp_flush,
821827
_modbus_tcp_select,
@@ -838,6 +844,7 @@ const modbus_backend_t _modbus_tcp_pi_backend = {
838844
_modbus_tcp_check_integrity,
839845
_modbus_tcp_pre_check_confirmation,
840846
_modbus_tcp_pi_connect,
847+
_modbus_tcp_is_connected,
841848
_modbus_tcp_close,
842849
_modbus_tcp_flush,
843850
_modbus_tcp_select,

src/modbus.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ int _modbus_receive_msg(modbus_t *ctx, uint8_t *msg, msg_type_t msg_type)
368368
}
369369
}
370370

371-
if (ctx->s < 0) {
371+
if (!ctx->backend->is_connected(ctx)) {
372372
if (ctx->debug) {
373373
fprintf(stderr, "ERROR The connection is not established.\n");
374374
}
@@ -1748,6 +1748,7 @@ int modbus_set_error_recovery(modbus_t *ctx, modbus_error_recovery_mode error_re
17481748
return 0;
17491749
}
17501750

1751+
// FIXME Doesn't work under Windows RTU
17511752
int modbus_set_socket(modbus_t *ctx, int s)
17521753
{
17531754
if (ctx == NULL) {

0 commit comments

Comments
 (0)