TCP IPv4 modbus_connect() defective #165

Closed
ghost opened this Issue Nov 18, 2013 · 3 comments

Comments

Projects
None yet
2 participants
@ghost

ghost commented Nov 18, 2013

i'm on Win7 and use the libmodbus-5.dll created with MinGW(GCC 4.8.1)
from the newest 2013-11-18 libmodbus master branch.

after modbus_new_rtu(), modbus_connect() works normal, but
after modbus_new_tcp(), modbus_connect() throws always an error
and modbus devices cannot be read.

TCP IPv4 modbus_connect() works normal in libmodbus-5.dll's
compiled form libmodbus 3.0.5 and earlier, so i could track the
defect down to the function _connect() in src/modbus-tcp.c :
within the _connect() function the call of connect() in the line

    rc = connect(sockfd, addr, addrlen);

throws the wsaError WSAEWOULDBLOCK , that according to the

microsoft documentation of the connect() function

is something to be expected after a connect() to a nonblocking socket,
because the function returns immediately (= nonblocking).
but a bit time is needed to establish the connection.

the documentation also describes the solution: that is to check
with the select() function if the socket is writeable.
exactly this check is already implemented some lines after the call of
connect() within the function _connect() in src/modbus-tcp.c by the call
of select() with enought time (= the "response _ timeout" in this case) .

so to solve the defect, it's needed to catch the wsaError WSAEWOULDBLOCK .

i suggest the tested solution:
in src/modbus-tcp.c within the _connect() function to replace the line

    if (rc == -1 && WSAGetLastError() == WSAEINPROGRESS) {

with

    int wsaError = 0;
    if ( rc == -1 ) { wsaError = WSAGetLastError(); }
    if ( wsaError == WSAEWOULDBLOCK || wsaError == WSAEINPROGRESS ) {

although it would look simpler just to call WSAGetLastError() twice,
i favour to call it once and use the saved result, since i don't know
if multiple calls of WSAGetLastError() would reliably give always the
same result.

@morgoth6

This comment has been minimized.

Show comment
Hide comment
@morgoth6

morgoth6 Dec 12, 2013

Problem with this part of code reported some time ago in #100 (comment) I use libmodbus on Win32 with this change for some time not without any major problems.

Problem with this part of code reported some time ago in #100 (comment) I use libmodbus on Win32 with this change for some time not without any major problems.

@ghost

This comment has been minimized.

Show comment
Hide comment
@ghost

ghost Dec 12, 2013

hello morgoth6,

do you use the patch from pull request 100# or my patch above ?

... not without any major problems.

did you experience a problem with the patch ?

ghost commented Dec 12, 2013

hello morgoth6,

do you use the patch from pull request 100# or my patch above ?

... not without any major problems.

did you experience a problem with the patch ?

@stephane

This comment has been minimized.

Show comment
Hide comment
@stephane

stephane Jan 28, 2014

Owner

@morgoth6 did you notice any problems with this way to connect?

@MarjanTomas I think it's better to store the result as you propose

Owner

stephane commented Jan 28, 2014

@morgoth6 did you notice any problems with this way to connect?

@MarjanTomas I think it's better to store the result as you propose

stephane added a commit that referenced this issue Jan 28, 2014

Fix TCP IPv4 modbus_connect() on win32 (closes #100 and #165)
Thank you Petr Gladkiy and MarjanTomas.
Sorry for the delay...

@stephane stephane closed this Jan 28, 2014

mk8 added a commit to mk8/libmodbus that referenced this issue Jan 29, 2014

Fix TCP IPv4 modbus_connect() on win32 (closes #100 and #165)
Thank you Petr Gladkiy and MarjanTomas.
Sorry for the delay...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment