Skip to content

Commit

Permalink
Fix connection check for Windows RTU (closes stephane#660, stephane#662)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephane committed Nov 28, 2022
1 parent 7c8e58a commit e84f555
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/modbus-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ typedef struct _modbus_backend {
const uint8_t *rsp,
int rsp_length);
int (*connect)(modbus_t *ctx);
unsigned int (*is_connected)(modbus_t *ctx);
void (*close)(modbus_t *ctx);
int (*flush)(modbus_t *ctx);
int (*select)(modbus_t *ctx, fd_set *rset, struct timeval *tv, int msg_length);
Expand Down
14 changes: 14 additions & 0 deletions src/modbus-rtu.c
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,19 @@ static int _modbus_rtu_connect(modbus_t *ctx)
return 0;
}

// FIXME Temporary solution before rewriting Windows RTU backend
static unsigned int _modbus_rtu_is_connected(modbus_t *ctx)
{
#if defined(_WIN32)
modbus_rtu_t *ctx_rtu = ctx->backend_data;

/* Check if file handle is valid */
return ctx_rtu->w_ser.fd != INVALID_HANDLE_VALUE;
#else
return ctx->s >= 0;
#endif
}

int modbus_rtu_set_serial_mode(modbus_t *ctx, int mode)
{
if (ctx == NULL) {
Expand Down Expand Up @@ -1043,6 +1056,7 @@ const modbus_backend_t _modbus_rtu_backend = {
_modbus_rtu_check_integrity,
_modbus_rtu_pre_check_confirmation,
_modbus_rtu_connect,
_modbus_rtu_is_connected,
_modbus_rtu_close,
_modbus_rtu_flush,
_modbus_rtu_select,
Expand Down
7 changes: 7 additions & 0 deletions src/modbus-tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,11 @@ static int _modbus_tcp_pi_connect(modbus_t *ctx)
return 0;
}

static unsigned int _modbus_tcp_is_connected(modbus_t *ctx)
{
return ctx->s >= 0;
}

/* Closes the network connection and socket in TCP mode */
static void _modbus_tcp_close(modbus_t *ctx)
{
Expand Down Expand Up @@ -816,6 +821,7 @@ const modbus_backend_t _modbus_tcp_backend = {
_modbus_tcp_check_integrity,
_modbus_tcp_pre_check_confirmation,
_modbus_tcp_connect,
_modbus_tcp_is_connected,
_modbus_tcp_close,
_modbus_tcp_flush,
_modbus_tcp_select,
Expand All @@ -838,6 +844,7 @@ const modbus_backend_t _modbus_tcp_pi_backend = {
_modbus_tcp_check_integrity,
_modbus_tcp_pre_check_confirmation,
_modbus_tcp_pi_connect,
_modbus_tcp_is_connected,
_modbus_tcp_close,
_modbus_tcp_flush,
_modbus_tcp_select,
Expand Down
3 changes: 2 additions & 1 deletion src/modbus.c
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ int _modbus_receive_msg(modbus_t *ctx, uint8_t *msg, msg_type_t msg_type)
}
}

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

// FIXME Doesn't work under Windows RTU
int modbus_set_socket(modbus_t *ctx, int s)
{
if (ctx == NULL) {
Expand Down

0 comments on commit e84f555

Please sign in to comment.