The Ethernet Shield on Arduino Due hang up after parsing a string using sscanf

Hi everyone,
I can't find the root of an issue I'm getting on my project. I really need help, theoretically I'm doing things well.

My platform is Arduino Due Board + Ethernet Shield Rev.3 + Ide 1.5.2. My goal is to read the file "eth.ini" (see attached) to configure the Ethernet Shield.

eth.ini content:
mac=90.A2.DA.0D.A2.76;
ip=172.27.101.62;
net=255.255.0.0;

If I parse only the first two lines (i.e. if I use sscanf twice in the same function) I can configure the Ethernet Shiel, everything works fine and I can ping the shield. If I try to parse the third line I get no error, but the shield doesn't work anymore (it is not possibile to ping).
Note that I can even execute the code in my loop, so it doesn't seems a crash.
Furthermore, even the third parsing works, but then cause something unexpected.

Here what the setup() routine prints:
Initializing SD card...card initialized.
mac=90.A2.DA.0D.A2.76;
90.A2.DA.D.A2.76
ip=172.27.101.62;
172.27.101.62
net=255.255.0.0;
255.255.0.0

Where you see "mac", "ip", "net", there are the char array to be parsed. Each line after is the result of parsing.

/*
 * Definition of my error number list
 */
#define E_OK     0 // Success
#define E_NOENT -1 // No such file or directory
#define E_NXIO  -2 // No such device or address
#define E_INVAL -3 // Invalid argument
#define E_FAIL  -4 // Generic failure */

#include "SPI.h"
#include "Ethernet.h"

#define DEBUG_FILE 1
#define DEBUG_ETH_CONFIG 1
#define DEBUG_ETH_CLIENT 0
#define DEBUG_PI 0

#include <SD.h>

boolean gSwReset = false;

void setup()
{   
    int result = 0xFF;
    
    /*
     * The default chip select pins must be set as output.
     * Pin #4 is the SPI CS for the SD card reader
     * Pin #10 is the SPI CS for the Wiznet Ethernet chip
     *
     * This is the pinout of Arduino Due Board
     */
    pinMode(4, OUTPUT);
    pinMode(10, OUTPUT);

    Serial.begin(9600); //Initialize the serial port and wait for port to open

    Serial.print("Initializing SD card...");

    if ( initSdCard() == E_OK )
        Serial.println("card initialized.");
    else
        Serial.println("Card failed, or not present");

    byte lIp[] = { 0xFF, 0xFF, 0xFF, 0xFF };
    byte lMac[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
    byte lSubnet[] = { 0xFF, 0xFF, 0xFF, 0xFF };

    result = getEthConfig ( lMac, lIp, lSubnet );
    
    //Without the following calls the Ethenet device doesn't work
    digitalWrite(4, HIGH);
    digitalWrite(10, LOW);
    
    // Ethernet.begin(mac, ip, dns, gateway, subnet) in order to force the subnet
    // The forced subnet is the one for the DLA network {255, 255, 0, 0}
    if ( result == E_OK )
        Ethernet.begin( lMac, lIp, lIp, lIp, {255, 255, 0, 0}); // Initialize the Ethernet adapter
    else 
        Serial.println("FATAL ERROR!");
        
    delay(2000);
}

void loop()
{
    //Serial.println("ALIVE!");
}

byte initSdCard()
{
    /*
     * On the Ethernet Shield, CS is pin 4. Note that even if it's not used as the CS pin,
     * the hardware CS pin (10 on most Arduino boards) must be left as an output or the
     * SD library functions will not work.
     */
    const int chipSelect = 4;

    // see if the card is present and can be initialized:
    if (!SD.begin(chipSelect))
        return E_NXIO;

    return E_OK;
}

int getEthConfig ( byte* mac, byte* ip, byte* subnet  )
{
    File dataFile = SD.open("eth.ini");
    
    if (dataFile) 
    {
        int i = 0;
        char buffer[65];
        char *pch;
        
        while (dataFile.available()) 
        {
            buffer[i] = dataFile.read();
            i++;
        }
        dataFile.close();
        
        pch = strtok (buffer, "\r\n");
        
        i = 0;
        
        while (pch != NULL)
        {
            switch( i )
            {
                case 0:
                    #if DEBUG_FILE
                        Serial.println ( pch );
                    #endif
                    
                    sscanf ( pch + 4, "%x.%x.%x.%x.%x.%x;", &mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5] );
                    
                    #if DEBUG_ETH_CONFIG
                        Serial.print ( mac[0], HEX );
                        Serial.print (".");
                        Serial.print ( mac[1], HEX );
                        Serial.print (".");
                        Serial.print ( mac[2], HEX );
                        Serial.print (".");
                        Serial.print ( mac[3], HEX );
                        Serial.print (".");
                        Serial.print ( mac[4], HEX );
                        Serial.print (".");
                        Serial.println ( mac[5], HEX );
                    #endif
                    
                    break;
                
                case 1:
                    #if DEBUG_FILE
                        Serial.println ( pch );
                    #endif
                    
                    sscanf ( pch + 3, "%u.%u.%u.%u;", &ip[0], &ip[1], &ip[2], &ip[3] );
                    
                    #if DEBUG_ETH_CONFIG
                        Serial.print ( ip[0] );
                        Serial.print (".");
                        Serial.print ( ip[1] );
                        Serial.print (".");
                        Serial.print ( ip[2] );
                        Serial.print (".");
                        Serial.println ( ip[3] );
                    #endif
                    
                    break;
                
                case 2:                               
                    #if DEBUG_FILE
                        Serial.println ( pch );
                    #endif
//#if 0                   
/************** The Ethernet Shield doesn't work after the next sscanf, if I remove the comment from the '#if 0' I can ping the board **************/
                    sscanf ( pch + 4, "%u.%u.%u.%u;", &subnet[0], &subnet[1], &subnet[2], &subnet[3] );  ////////////////////////////////////////////////// <-----------
/************** The Ethernet Shield doesn't work anymore **************/

                    #if DEBUG_ETH_CONFIG
                        Serial.print ( subnet[0] );
                        Serial.print (".");
                        Serial.print ( subnet[1] );
                        Serial.print (".");
                        Serial.print ( subnet[2] );
                        Serial.print (".");
                        Serial.println ( subnet[3] );
                    #endif
//#endif             
                    return E_OK;
             
                default:
                    break;
            }
            
            pch = strtok (NULL, "\r\n");
            i++;
        }
    }  
    else 
    {
        Serial.println("Error opening eth.ini");
        return E_NOENT;
    }
    return E_OK;
}

That's all as far, thank you!

eth.ini (61 Bytes)

Does the same problem persist is you define the subnet mask correctly in the eth.ini file (255.255.255.0)?

I use a file to set up my ethernet shield and it works ok. I use a slightly different format.

This works on a Mega/ethernet shield.

Thank you very much for your suggestions.

Does the same problem persist is you define the subnet mask correctly in the eth.ini file (255.255.255.0)?

PaulS, why does the (255.255.255.0) is more correct than (255.255.0.0)?

SurferTim, ok. Using the "atoi" and building the string pieces as you made it's another good alternative. I could try.
BTW, I have an update that sounds strange to me. I found a workaround.

I declared my ip local variable static! And everything work!

    static uint8_t lIp[] = { 0xFF, 0xFF, 0xFF, 0xFF };
    uint8_t lMac[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
    uint8_t lSubnet[] = { 0xFF, 0xFF, 0xFF, 0xFF };

It looks like a memory problem, but the Arduino Due Board has 96 KB sram and it's quite impossible that I've already finished it.

I did also some other test to confirm my theory.

  1. I declared all the local variable of the network parameters as static. No good, doesn't work anymore.
    static uint8_t lIp[] = { 0xFF, 0xFF, 0xFF, 0xFF };
    static uint8_t lMac[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
    static uint8_t lSubnet[] = { 0xFF, 0xFF, 0xFF, 0xFF };
  1. I declared the network parameters as global variable. No good, doesn't work anymore.
    //Global variables (whole file scope)
    uint8_t lIp[] = { 0xFF, 0xFF, 0xFF, 0xFF };
    uint8_t lMac[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
    uint8_t lSubnet[] = { 0xFF, 0xFF, 0xFF, 0xFF };
  1. If I use the original code giving directly numbers instead of variables to the Ethernet.begin, everything works fine. But It's not my goal. It's just another proof.
Ethernet.begin( { 0xAA, 0xBB, 0xCC, 0x11, 0x11, 0x11 }, {172.24.0.1}, {172.24.0.1}, {172.24.0.1}, {255.255.0.0});

It doesn't seem a problem related to the code, but to the environment. That's only my opinion, I would appreciate other point of view. Thank you again.

why does the (255.255.255.0) is more correct than (255.255.0.0)?

Because one is correct, and the other isn't. You must figure out which. That depends on your current network setup in the localnet gateway router.

This is incorrect.

Ethernet.begin( { 0xAA, 0xBB, 0xCC, 0x11, 0x11, 0x11 }, {172.24.0.1}, {172.24.0.1}, {172.24.0.1}, {255.255.0.0});

This is the format.
Ethernet begin(mac, ip, dns, gateway, subnet);
Do not use your shield ip as the dns and gateway. Bad idea. Use valid network settings here.

SurferTim, ok. Using the "atoi" and building the string pieces as you made it's another good alternative. I could try.

atoi? Where did you see that in my code? I use sscanf also.

Did you try my code?

Ok SurferTim, I considered the wrong code. My correct subnet mask is 255.255.0.0.
Regarding the network parameters, I don't think the wrong gateway is the root of the issue, but I will adjust it.

I tried your code and doesn't work, maybe I ran into the same issue. My guess is again that the root of the issue is the compiler or something related to the platform, because I think you've already tested your code.

#include <SD.h>
#include <SPI.h>
#include <Ethernet.h>

byte myMac[6];
byte myIP[4];
byte myNM[4];
byte myGW[4];
byte myDNS[4];

void setup() {
  Serial.begin(9600);  

  pinMode(10,OUTPUT);
  digitalWrite(10,HIGH);

  if(!SD.begin(4)) Serial.println("SD fail");
  else Serial.println("SD ok");

  //File fh = SD.open("network.txt",FILE_READ);
  File fh = SD.open("net2.txt",FILE_READ);
  char netBuffer[32];
  
  if(!fh)
  {
    Serial.println("SD open fail");
    return;    
  }

  int chPos = 0;
  int lineNo = 0;
  
  while(fh.available())
  {
    char ch = fh.read();
    if(ch == '\r') {
      chPos = 0;

      switch(lineNo) {
        case 0:
          Serial.print("mac ");        
  sscanf(netBuffer,"%2x:%2x:%2x:%2x:%2x:%2x",&myMac[0],&myMac[1],&myMac[2],&myMac[3],&myMac[4],&myMac[5]);  
        break;

        case 1:
          Serial.print("ip ");        
  sscanf(netBuffer,"%u.%u.%u.%u",&myIP[0],&myIP[1],&myIP[2],&myIP[3]);  
        break;

        case 2:
          Serial.print("netmask ");        
  sscanf(netBuffer,"%u.%u.%u.%u",&myNM[0],&myNM[1],&myNM[2],&myNM[3]);  
        break;

        case 3:
          Serial.print("gateway ");        
  sscanf(netBuffer,"%u.%u.%u.%u",&myGW[0],&myGW[1],&myGW[2],&myGW[3]);  
        break;

        case 4:
          Serial.print("dns ");        
  sscanf(netBuffer,"%u.%u.%u.%u",&myDNS[0],&myDNS[1],&myDNS[2],&myDNS[3]);  
        break;
      }

      Serial.println(netBuffer);
      lineNo++;
    }
    else if(ch == '\n') {
      // do nothing
    }
    else if(chPos < 31) {
      //Serial.println (chPos);
      netBuffer[chPos] = ch;
       chPos++;
      netBuffer[chPos] = 0;
    }
  }
  
  fh.close();

  int x;
  
  Serial.print("\r\nmac ");
  for(x=0;x<6;x++) {
    Serial.print(myMac[x],HEX);
    if(x<5) Serial.print(":");
  }

  Serial.print("\r\nip ");
  for(x=0;x<4;x++) {
    Serial.print(myIP[x],DEC);
    if(x<3) Serial.print(".");
  }

  Serial.print("\r\nnetmask ");
  for(x=0;x<4;x++) {
    Serial.print(myNM[x],DEC);
    if(x<3) Serial.print(".");
  }

  Serial.print("\r\ngateway ");
  for(x=0;x<4;x++) {
    Serial.print(myGW[x],DEC);
    if(x<3) Serial.print(".");
  }

  Serial.print("\r\ndns ");
  for(x=0;x<4;x++) {
    Serial.print(myDNS[x],DEC);
    if(x<3) Serial.print(".");
  }

  Serial.println("\r\nStarting ethernet");
  Ethernet.begin(myMac,myIP,myDNS,myGW,myNM);
  digitalWrite(10,HIGH);
  
  Serial.println(Ethernet.localIP());
}

void loop() {
}

I tried two configuration files:

  1. net1.txt
12:34:56:78:90:AB
172.27.101.60
255.255.0.0
172.27.2.254
172.27.0.32
  1. net2.txt
12:34:56:78:90:AB
192.168.2.2
255.255.255.0
192.168.2.1
192.168.2.1

Here the output with all your debug print:

  1. Using net1.txt
SD ok
mac 12:34:56:78:90:AB
ip 172.27.101.60
netmask 255.255.0.0
gateway 172.27.2.254

mac 12:34:56:78:90:AB
ip 0.0.0.60
netmask 255.255.0.0
gateway 172.27.2.254
dns 0.0.0.0
Starting ethernet
0.0.0.0
  1. Using net2.txt
SD ok
mac 12:34:56:78:90:AB
ip 192.168.2.2
netmask 255.255.255.0
gateway 192.168.2.1

mac 12:34:56:78:90:AB
ip 0.0.0.2
netmask 255.255.255.0
gateway 192.168.2.1
dns 0.0.0.0
Starting ethernet
0.0.0.0

You see, the ip address is always overwritten like an array index ran out of bounds. This could explain why my code work if a declare the IP address as static, because in that case the ip location in the memory will be different form the previous one.

The values that you are printing, for debugging purposes, are pretty much useless.

       Serial.print("mac ");        
       sscanf(netBuffer,"%2x:%2x:%2x:%2x:%2x:%2x",
                 &myMac[0],&myMac[1],&myMac[2],&myMac[3],&myMac[4],&myMac[5]);  
       Serial.println(netBuffer);

It isn't the data in netBuffer that is of interest. It is the data in myMac[] that is of interest. Printing the data in netBuffer BEFORE the calls to sscanf is fine, but print the scanned data afterwards.

Also, sscanf() returns a value that you are discarding. Printing that value just might be a clue by four.

Well PaulS, thank you. It was not my code, I was just trying to givo info to SurferTim without touching anything on his code.

BTW, I followed your directions and the scanning seems ok, then something happens. I think that's quite interesting.

SurferTim wrote:

This works on a Mega/ethernet shield.

New code:

#include <SD.h>
#include <SPI.h>
#include <Ethernet.h>

byte myMac[6];
byte myIP[4];
byte myNM[4];
byte myGW[4];
byte myDNS[4];

void setup() {
  int result = 0xFF;
  Serial.begin(9600);  

  pinMode(10,OUTPUT);
  digitalWrite(10,HIGH);

  if(!SD.begin(4)) Serial.println("SD fail");
  else Serial.println("SD ok");

  //File fh = SD.open("network.txt",FILE_READ);
  File fh = SD.open("net1.txt",FILE_READ);
  char netBuffer[32];
  
  if(!fh)
  {
    Serial.println("SD open fail");
    return;    
  }

  int chPos = 0;
  int lineNo = 0;
  
  while(fh.available())
  {
    char ch = fh.read();

    if(ch == '\r') {
      chPos = 0;

      switch(lineNo) {
        case 0:
          result = sscanf(netBuffer,"%2x:%2x:%2x:%2x:%2x:%2x",&myMac[0],&myMac[1],&myMac[2],&myMac[3],&myMac[4],&myMac[5]);
          Serial.print ("sscanf0 result: ");
          Serial.print ( result );
          Serial.print(",  mac string ");
          Serial.print (netBuffer);
          Serial.print(",  myMac bytes ");
          
          for( int x=0;x<6;x++) {
            Serial.print(myMac[x],HEX);
            if(x<5) Serial.print(":");
          }
          Serial.println ("");
        break;

        case 1:
          result = sscanf(netBuffer,"%u.%u.%u.%u",&myIP[0],&myIP[1],&myIP[2],&myIP[3]);  
          Serial.print ("sscanf1 result: ");
          Serial.print ( result );
          Serial.print(",  ip string ");
          Serial.print (netBuffer);
          Serial.print(",  myIP bytes ");
          
          for( int x=0;x<4;x++) {
            Serial.print(myIP[x],DEC);
            if(x<3) Serial.print(".");
          }
          Serial.println ("");      
        break;

        case 2:
          result = sscanf(netBuffer,"%u.%u.%u.%u",&myNM[0],&myNM[1],&myNM[2],&myNM[3]);  
          Serial.print ("sscanf2 result: ");
          Serial.print ( result );
          Serial.print(",  netmask string ");
          Serial.print (netBuffer);
          Serial.print(",  myNM bytes ");
          
          for( int x=0;x<4;x++) {
            Serial.print(myNM[x],DEC);
            if(x<3) Serial.print(".");
          }
          Serial.println ("");  
        break;

        case 3:
          result = sscanf(netBuffer,"%u.%u.%u.%u",&myGW[0],&myGW[1],&myGW[2],&myGW[3]);
          Serial.print ("sscanf3 result: ");
          Serial.print ( result );
          Serial.print(",  gateway string ");
          Serial.print (netBuffer);
          Serial.print(",  myGW bytes ");
          
          for( int x=0;x<4;x++) {
            Serial.print(myGW[x],DEC);
            if(x<3) Serial.print(".");
          }
          Serial.println ("");            
        break;

        case 4:
          Serial.print ("sscanf4 result: ");
          result = sscanf(netBuffer,"%u.%u.%u.%u",&myDNS[0],&myDNS[1],&myDNS[2],&myDNS[3]);  
          Serial.print ( result );
          Serial.print(",  dns string ");
          Serial.print (netBuffer);
          Serial.print(",  myDNS bytes ");
          
          for( int x=0;x<4;x++) {
            Serial.print(myDNS[x],DEC);
            if(x<3) Serial.print(".");
          }
          Serial.println ("");           
        break;
        
        default:
          Serial.println ("Default case reached");
      }

      //Serial.println(netBuffer);
      lineNo++;
    }
    else if(ch == '\n') {
      // do nothing
    }
    else if(chPos < 31) {
      //Serial.println (chPos);
      netBuffer[chPos] = ch;
       chPos++;
      netBuffer[chPos] = 0;
    }
  }
  
  fh.close();

  Serial.println ("Just before the Ethernet.begin");
  int x;

  Serial.print("mac ");
  for(x=0;x<6;x++) {
    Serial.print(myMac[x],HEX);
    if(x<5) Serial.print(":");
  }

  Serial.print("\r\nip ");
  for(x=0;x<4;x++) {
    Serial.print(myIP[x],DEC);
    if(x<3) Serial.print(".");
  }

  Serial.print("\r\nnetmask ");
  for(x=0;x<4;x++) {
    Serial.print(myNM[x],DEC);
    if(x<3) Serial.print(".");
  }

  Serial.print("\r\ngateway ");
  for(x=0;x<4;x++) {
    Serial.print(myGW[x],DEC);
    if(x<3) Serial.print(".");
  }

  Serial.print("\r\ndns ");
  for(x=0;x<4;x++) {
    Serial.print(myDNS[x],DEC);
    if(x<3) Serial.print(".");
  }


  Serial.println("\r\nStarting ethernet");
  Ethernet.begin(myMac,myIP,myDNS,myGW,myNM);
  digitalWrite(10,HIGH);
  
  Serial.println(Ethernet.localIP());
}

void loop() {
}

Debug prints:

SD ok
sscanf0 result: 6,  mac string 12:34:56:78:90:AB,  myMac bytes 12:34:56:78:90:AB
sscanf1 result: 4,  ip string 172.27.101.60,  myIP bytes 172.27.101.60
sscanf2 result: 4,  netmask string 255.255.0.0,  myNM bytes 255.255.0.0
sscanf3 result: 4,  gateway string 172.27.2.254,  myGW bytes 172.27.2.254
sscanf4 result: 4,  dns string 172.27.0.32,  myDNS bytes 172.27.0.32
Just before the Ethernet.begin
mac 12:34:56:78:90:AB
ip 0.0.0.60
netmask 255.255.0.0
gateway 172.27.2.254
dns 172.27.0.32
Starting ethernet
0.0.0.0

Everything looks good until here:

ip 0.0.0.60

and here:

Starting ethernet
0.0.0.0

?? :frowning:

I would make one more change. After each call to sscanf, I'd print all 5 arrays. One of them is stepping out of bounds. It's hard to see where when you only print the memory locations that are supposed to have been written to. The sscanf return values look correct, though.

I have added new debug prints as you suggested.

#include <SD.h>
#include <SPI.h>
#include <Ethernet.h>

byte myMac[6];
byte myIP[4];
byte myNM[4];
byte myGW[4];
byte myDNS[4];

void setup() {
  int result = 0xFF;
  Serial.begin(9600);  

  pinMode(10,OUTPUT);
  digitalWrite(10,HIGH);

  if(!SD.begin(4)) Serial.println("SD fail");
  else Serial.println("SD ok");

  //File fh = SD.open("network.txt",FILE_READ);
  File fh = SD.open("net1.txt",FILE_READ);
  char netBuffer[32];
  
  if(!fh)
  {
    Serial.println("SD open fail");
    return;    
  }

  int chPos = 0;
  int lineNo = 0;
  
  while(fh.available())
  {
    char ch = fh.read();

    if(ch == '\r') {
      chPos = 0;

      switch(lineNo) {
        case 0:
          result = sscanf(netBuffer,"%2x:%2x:%2x:%2x:%2x:%2x",&myMac[0],&myMac[1],&myMac[2],&myMac[3],&myMac[4],&myMac[5]);
          Serial.print ("sscanf0 result: ");
          Serial.print ( result );
          Serial.print(",  mac string ");
          Serial.print (netBuffer);
          Serial.print(",  myMac bytes ");
          
          for( int x=0;x<6;x++) {
            Serial.print(myMac[x],HEX);
            if(x<5) Serial.print(":");
          }
          Serial.println ("\r\nPrinting Eth Variables");  
          printEthVar();
        break;

        case 1:
          result = sscanf(netBuffer,"%u.%u.%u.%u",&myIP[0],&myIP[1],&myIP[2],&myIP[3]);  
          Serial.print ("\r\nsscanf1 result: ");
          Serial.print ( result );
          Serial.print(",  ip string ");
          Serial.print (netBuffer);
          Serial.print(",  myIP bytes ");
          
          for( int x=0;x<4;x++) {
            Serial.print(myIP[x],DEC);
            if(x<3) Serial.print(".");
          }
          Serial.println ("\r\nPrinting Eth Variables");  
          printEthVar();
              
        break;

        case 2:
          result = sscanf(netBuffer,"%u.%u.%u.%u",&myNM[0],&myNM[1],&myNM[2],&myNM[3]);  
          Serial.print ("\r\nsscanf2 result: ");
          Serial.print ( result );
          Serial.print(",  netmask string ");
          Serial.print (netBuffer);
          Serial.print(",  myNM bytes ");
          
          for( int x=0;x<4;x++) {
            Serial.print(myNM[x],DEC);
            if(x<3) Serial.print(".");
          }
          Serial.println ("\r\nPrinting Eth Variables");  
          printEthVar();  
        break;

        case 3:
          result = sscanf(netBuffer,"%u.%u.%u.%u",&myGW[0],&myGW[1],&myGW[2],&myGW[3]);
          Serial.print ("\r\nsscanf3 result: ");
          Serial.print ( result );
          Serial.print(",  gateway string ");
          Serial.print (netBuffer);
          Serial.print(",  myGW bytes ");
          
          for( int x=0;x<4;x++) {
            Serial.print(myGW[x],DEC);
            if(x<3) Serial.print(".");
          }
          Serial.println ("\r\nPrinting Eth Variables");  
          printEthVar();            
        break;

        case 4:
          Serial.print ("\r\nsscanf4 result: ");
          result = sscanf(netBuffer,"%u.%u.%u.%u",&myDNS[0],&myDNS[1],&myDNS[2],&myDNS[3]);  
          Serial.print ( result );
          Serial.print(",  dns string ");
          Serial.print (netBuffer);
          Serial.print(",  myDNS bytes ");
          
          for( int x=0;x<4;x++) {
            Serial.print(myDNS[x],DEC);
            if(x<3) Serial.print(".");
          }
          Serial.println ("\r\nPrinting Eth Variables");  
          printEthVar();          
        break;
        
        default:
          Serial.println ("Default case reached");
      }

      //Serial.println(netBuffer);
      lineNo++;
    }
    else if(ch == '\n') {
      // do nothing
    }
    else if(chPos < 31) {
      //Serial.println (chPos);
      netBuffer[chPos] = ch;
       chPos++;
      netBuffer[chPos] = 0;
    }
  }
  
  fh.close();

#if 0
  int x;

  Serial.print("mac ");
  for(x=0;x<6;x++) {
    Serial.print(myMac[x],HEX);
    if(x<5) Serial.print(":");
  }

  Serial.print("\r\nip ");
  for(x=0;x<4;x++) {
    Serial.print(myIP[x],DEC);
    if(x<3) Serial.print(".");
  }

  Serial.print("\r\nnetmask ");
  for(x=0;x<4;x++) {
    Serial.print(myNM[x],DEC);
    if(x<3) Serial.print(".");
  }

  Serial.print("\r\ngateway ");
  for(x=0;x<4;x++) {
    Serial.print(myGW[x],DEC);
    if(x<3) Serial.print(".");
  }

  Serial.print("\r\ndns ");
  for(x=0;x<4;x++) {
    Serial.print(myDNS[x],DEC);
    if(x<3) Serial.print(".");
  }
#endif

  Serial.println("\r\nStarting ethernet");
  Ethernet.begin(myMac,myIP,myDNS,myGW,myNM);
  digitalWrite(10,HIGH);
  
  Serial.println(Ethernet.localIP());
}

void loop() {
}

void printEthVar()
{
  int x;

  Serial.print("mac ");
  for(x=0;x<6;x++) {
    Serial.print(myMac[x],HEX);
    if(x<5) Serial.print(":");
  }

  Serial.print("\r\nip ");
  for(x=0;x<4;x++) {
    Serial.print(myIP[x],DEC);
    if(x<3) Serial.print(".");
  }

  Serial.print("\r\nnetmask ");
  for(x=0;x<4;x++) {
    Serial.print(myNM[x],DEC);
    if(x<3) Serial.print(".");
  }

  Serial.print("\r\ngateway ");
  for(x=0;x<4;x++) {
    Serial.print(myGW[x],DEC);
    if(x<3) Serial.print(".");
  }

  Serial.print("\r\ndns ");
  for(x=0;x<4;x++) {
    Serial.print(myDNS[x],DEC);
    if(x<3) Serial.print(".");
  }
}

Here they are:

SD ok
sscanf0 result: 6,  mac string 12:34:56:78:90:AB,  myMac bytes 12:34:56:78:90:AB
Printing Eth Variables
mac 12:34:56:78:90:AB
ip 0.0.0.0
netmask 0.0.0.0
gateway 0.0.0.0
dns 0.0.0.0
sscanf1 result: 4,  ip string 172.27.101.60,  myIP bytes 172.27.101.60
Printing Eth Variables
mac 12:34:56:78:90:AB
ip 172.27.101.60
netmask 0.0.0.0
gateway 0.0.0.0
dns 0.0.0.0
sscanf2 result: 4,  netmask string 255.255.0.0,  myNM bytes 255.255.0.0
Printing Eth Variables
mac 12:34:56:78:90:AB
ip 172.27.101.60
netmask 255.255.0.0
gateway 0.0.0.0
dns 0.0.0.0
sscanf3 result: 4,  gateway string 172.27.2.254,  myGW bytes 172.27.2.254
Printing Eth Variables
mac 12:34:56:78:90:AB
ip 0.0.0.60
netmask 255.255.0.0
gateway 172.27.2.254
dns 0.0.0.0
sscanf4 result: 4,  dns string 172.27.0.32,  myDNS bytes 172.27.0.32
Printing Eth Variables
mac 12:34:56:78:90:AB
ip 0.0.0.60
netmask 255.255.0.0
gateway 172.27.2.254
dns 172.27.0.32
Starting ethernet
0.0.0.0

I don't know what to think at this point. I put comments in the readout at the ip prints. It is ok after the netmask, but is incorrect after the gateway.

sscanf2 result: 4, netmask string 255.255.0.0, myNM bytes 255.255.0.0
Printing Eth Variables
mac 12:34:56:78:90:AB
// the ip is ok here
ip 172.27.101.60
netmask 255.255.0.0
gateway 0.0.0.0
dns 0.0.0.0
sscanf3 result: 4, gateway string 172.27.2.254, myGW bytes 172.27.2.254
Printing Eth Variables
mac 12:34:56:78:90:AB
// after the gateway, the ip isn't
ip 0.0.0.60
netmask 255.255.0.0
gateway 172.27.2.254
dns 0.0.0.0

The next thing I would try would be to change the array type from byte to int. I suspect that the %u is for unsigned int, not byte, so you ARE stepping all over memory. Worth a try, at least.

Thank you very much guys! The issue was generated by the placeholder %u that needs unsigned int.

    unsigned int tempIp[] = { 0xFF, 0xFF, 0xFF, 0xFF };
    unsigned int tempSubnet[] = { 0xFF, 0xFF, 0xFF, 0xFF };  

    sscanf ( pch + 3, "%u.%u.%u.%u;", &tempIp[0], &tempIp[1], &tempIp[2], &tempIp[3] );

    for ( int j = 0; j < 4; j++)
        ip [j] = byte( tempIp[j] );  //the cast has to be done becuase Ethernet.begin(...) accepts only byte array

//and so on...

That conversion/memory overwrite problem must be unique to the Due or IDE v1.5.2. I tested your code from reply #5 (not using your new mod) on my Mega with IDE v1.0.4, and all my network settings are fine.

SD ok
sscanf0 result: 6, mac string 12:34:56:78:90:AB, myMac bytes 12:34:56:78:90:AB
sscanf1 result: 4, ip string 192.168.2.2, myIP bytes 192.168.2.2
sscanf2 result: 4, netmask string 255.255.255.0, myNM bytes 255.255.255.0
sscanf3 result: 4, gateway string 192.168.2.1, myGW bytes 192.168.2.1
sscanf4 result: 4, dns string 192.168.2.1, myDNS bytes 192.168.2.1
Just before the Ethernet.begin
mac 12:34:56:78:90:AB
ip 192.168.2.2
netmask 255.255.255.0
gateway 192.168.2.1
dns 192.168.2.1
Starting ethernet
192.168.2.2