Pages: [1]   Go Down
Author Topic: Ethernet, connect(), timeout  (Read 4261 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Jr. Member
**
Karma: 2
Posts: 66
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

based on WebClient example I am trying to connect to my web server. It works as expected, but standard behaviour does not meet my specific need.

Story: Arduino tries to connect to web server to get some data. If it cannot establish connection, it will use data stored in EEPROM or SD card instead.
In case of network/web server problem, client.connect() takes more than 30 seconds to return false. I need true or false within 2 seconds.

Question: Is it possible to set timeout period for connect() method?


Thank you


qwertysimo
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 653
Posts: 50924
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Have you looked at the source code for Client?
Logged

0
Offline Offline
Jr. Member
**
Karma: 2
Posts: 66
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I tracked it down to socket.cpp, function connect:

Quote
This function established  the connection for the channel in Active (client) mode. This function waits for the untill the connection is established.

Then down to :

Code:
void W5100Class::execCmdSn(SOCKET s, SockCMD _cmd) {
  // Send command to socket
  writeSnCR(s, _cmd);
  // Wait for command to complete
  while (readSnCR(s))
    ;
}

but cannot find writeSnCR neither readSnCR.
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 227
Posts: 14048
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

you could patch this loop like:
Code:
unsigned long limit = to be determined;
unsigned long t = 0;
while (readSnCR(s) && (t < limit) ) t++;
not nice but it might do the trick ?
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

0
Offline Offline
Newbie
*
Karma: 0
Posts: 18
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The troublesome code is not the execCmdSn; that returns instantly. The problem is back in Client::connect()

Code:
while (status() != SnSR::ESTABLISHED) {
    delay(1);
    if (status() == SnSR::CLOSED) {
      _sock = MAX_SOCK_NUM;
      return 0;
    }
  }

That is where you would put the patch suggested by robtillaart...but, it's so much easier than that!

The simple fix

arduino-0021\libraries\Ethernet\utility\w5100.cpp, add 2 lines to make the init function look like this

Code:
void W5100Class::init(void)
{
  delay(300);

  SPI.begin();
  initSS();
  
  writeMR(1<<RST);
  writeTMSR(0x55);
  writeRMSR(0x55);

  setRetransmissionTime(0x07D0);
  setRetransmissionCount(3);

  for (int i=0; i<MAX_SOCK_NUM; i++) {
    SBASE[i] = TXBUF_BASE + SSIZE * i;
    RBASE[i] = RXBUF_BASE + RSIZE * i;
  }
}

setRetransmissionTime sets the Wiznet's timeout period, where each unit is 100us, so 0x07D0 (decimal 2000) means 200ms.

setRetransmissionCount sets the Wiznet's retry count.

A retry count of 2 or 3 makes the connect function fail really quickly.

Hope that helps.

Ben

Edit:

The simpler fix

You don't even need to modify the library code. In your sketch, add this include:

Code:
#include <utility/w5100.h>

then, in your setup, after you call Ethernet.begin(), add the following two lines:

Code:
W5100.setRetransmissionTime(0x07D0);
W5100.setRetransmissionCount(3);

That gives me a 3 second timeout on a bad server connection.
« Last Edit: December 01, 2010, 05:31:24 pm by oilytheotter » Logged

0
Offline Offline
Jr. Member
**
Karma: 2
Posts: 66
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks. I will try this solution soon.
Logged

Pages: [1]   Go Up
Jump to: