Commit c726c9de78c918ca47aafce6c2a466cb5218f690
1 parent
515960cc
Fix connection check for Windows RTU (closes #660, #662)
Showing
4 changed files
with
24 additions
and
1 deletions
src/modbus-private.h
| ... | ... | @@ -86,6 +86,7 @@ typedef struct _modbus_backend { |
| 86 | 86 | const uint8_t *rsp, |
| 87 | 87 | int rsp_length); |
| 88 | 88 | int (*connect)(modbus_t *ctx); |
| 89 | + unsigned int (*is_connected)(modbus_t *ctx); | |
| 89 | 90 | void (*close)(modbus_t *ctx); |
| 90 | 91 | int (*flush)(modbus_t *ctx); |
| 91 | 92 | int (*select)(modbus_t *ctx, fd_set *rset, struct timeval *tv, int msg_length); | ... | ... |
src/modbus-rtu.c
| ... | ... | @@ -725,6 +725,19 @@ static int _modbus_rtu_connect(modbus_t *ctx) |
| 725 | 725 | return 0; |
| 726 | 726 | } |
| 727 | 727 | |
| 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 | + | |
| 728 | 741 | int modbus_rtu_set_serial_mode(modbus_t *ctx, int mode) |
| 729 | 742 | { |
| 730 | 743 | if (ctx == NULL) { |
| ... | ... | @@ -1043,6 +1056,7 @@ const modbus_backend_t _modbus_rtu_backend = { |
| 1043 | 1056 | _modbus_rtu_check_integrity, |
| 1044 | 1057 | _modbus_rtu_pre_check_confirmation, |
| 1045 | 1058 | _modbus_rtu_connect, |
| 1059 | + _modbus_rtu_is_connected, | |
| 1046 | 1060 | _modbus_rtu_close, |
| 1047 | 1061 | _modbus_rtu_flush, |
| 1048 | 1062 | _modbus_rtu_select, | ... | ... |
src/modbus-tcp.c
| ... | ... | @@ -449,6 +449,11 @@ static int _modbus_tcp_pi_connect(modbus_t *ctx) |
| 449 | 449 | return 0; |
| 450 | 450 | } |
| 451 | 451 | |
| 452 | +static unsigned int _modbus_tcp_is_connected(modbus_t *ctx) | |
| 453 | +{ | |
| 454 | + return ctx->s >= 0; | |
| 455 | +} | |
| 456 | + | |
| 452 | 457 | /* Closes the network connection and socket in TCP mode */ |
| 453 | 458 | static void _modbus_tcp_close(modbus_t *ctx) |
| 454 | 459 | { |
| ... | ... | @@ -816,6 +821,7 @@ const modbus_backend_t _modbus_tcp_backend = { |
| 816 | 821 | _modbus_tcp_check_integrity, |
| 817 | 822 | _modbus_tcp_pre_check_confirmation, |
| 818 | 823 | _modbus_tcp_connect, |
| 824 | + _modbus_tcp_is_connected, | |
| 819 | 825 | _modbus_tcp_close, |
| 820 | 826 | _modbus_tcp_flush, |
| 821 | 827 | _modbus_tcp_select, |
| ... | ... | @@ -838,6 +844,7 @@ const modbus_backend_t _modbus_tcp_pi_backend = { |
| 838 | 844 | _modbus_tcp_check_integrity, |
| 839 | 845 | _modbus_tcp_pre_check_confirmation, |
| 840 | 846 | _modbus_tcp_pi_connect, |
| 847 | + _modbus_tcp_is_connected, | |
| 841 | 848 | _modbus_tcp_close, |
| 842 | 849 | _modbus_tcp_flush, |
| 843 | 850 | _modbus_tcp_select, | ... | ... |
src/modbus.c
No preview for this file type