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)