Pages: 1 [2]   Go Down
Author Topic: Problem with structure  (Read 1553 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 20
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If i comment out the the following line, Arduino work correctly and Serial console output is right
Code:
//char* sensor_data = strtok(data,",");
      /*while(sensor_data)
      {
          Serial.print("Token: [");
          Serial.print(sensor_data);
          Serial.println("]");
          if(row == 0)strcpy(sensor[line].index,sensor_data);
          if(row == 1)strcpy(sensor[line].pintype,sensor_data);
          if(row == 2)strcpy(sensor[line].pin,sensor_data);
          if(row == 3)strcpy(sensor[line].model,sensor_data);
          if(row == 4)strcpy(sensor[line].address,sensor_data);
          if(row == 5) {
            strcpy(sensor[line].description,sensor_data);
            line++;
          }
          sensor_data = strtok(NULL,",");
          row++;
      }*/
I'm sure that Arduino crashes when i operate with strcpy() and struct array! Could be ide 1.0 problem? Same problem declaring bidimensional array and pass it to strcpy() function.
Logged

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

Code:
//char* sensor_data = strtok(data,",");
What does sensor_data contain initially, if not commented?

Quote
If i comment out the the following line, Arduino work correctly and Serial console output is right
What does this mean? You haven't given the Arduino anything to do, unless you have changed the code and not posted new code.

Quote
I'm sure that Arduino crashes when i operate with strcpy() and struct array!
Proof? What evidence do you have that the Arduino is not behaving correctly? You haven't posted anything to back up this claim.

Quote
Could be ide 1.0 problem? Same problem declaring bidimensional array and pass it to strcpy() function.
No.
« Last Edit: February 21, 2012, 02:46:29 pm by PaulS » Logged

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

So, this is the situation. The fallowing code run correctly :
Code:
#include <EEPROM.h>
#include <SD.h>
#include <SPI.h>
#include <Ethernet.h>
#include <Wire.h>
#include <OneWire.h>

#define MAX_SENSORS 5

typedef struct{
  char index[2];
  char pintype[3];
  char pin[2];
  char model[11];
  char address[11];
  char description[31];
}SENSORS_T;

typedef struct{
  byte ip[4];
  byte subnet[4];
  byte gateway[4];
  byte mac[6];
} NETWORK_T;

SENSORS_T sensor[MAX_SENSORS];
NETWORK_T network;

char file_net_cfg[] = {
  "config/net.cfg"};
char file_sensors_cfg[] = {
  "config/sensors.cfg"};

boolean from_eeprom = false;


//*************************************************************************//
//FUNCTION TO START NETWORK & IMPORT PARAMETERS IN STRUCTURE              //
//***********************************************************************//
void network_setup(char data[],boolean &eeprom )
{
  network.mac[0] = 0xA2;
  network.mac[1] = 0xDA;
  network.mac[2] = 0x00;
  network.mac[3] = 0x22;
  network.mac[4] = 0x27;
  network.mac[5] = 0x90;
  Serial.print(F("Loading ethernet from "));
  if (!eeprom)
  {
      uint8_t a = 0;
      char* net_data = strtok(data,".");
      Serial.print(F("SD: "));
      while(net_data)
      {
          if(a >= 0 && a < 4 )network.ip[a] = atoi(net_data);
          if(a >= 4 && a < 8)network.subnet[a-4] = atoi(net_data);
          if(a >= 8 && a < 12) network.gateway[a-8]= atoi(net_data);
          net_data = strtok(NULL,".");
          a++;
      }
      network.ip[4] = '\0';
      network.subnet[4] = '\0';
      network.gateway[4] = '\0';
      network.mac[6] =  '\0';
      Ethernet.begin(network.mac, network.ip, network.gateway, network.subnet);
      Serial.println(F("OK"));
      Serial.print(F("IP: "));
      Serial.println(Ethernet.localIP());
     
  } else {
    Serial.print(F("EEPROM: "));
    uint8_t arr_count = 0; //contatore per array variabili temporanee
    for (uint8_t a = 20; a < 32; a++)
    {
      if (arr_count == 4 && a < 32) arr_count = 0;
      if ((a-20) < 4) network.ip[arr_count] = EEPROM.read(a);
      if ((a-20) == 4 && (a-20) < 8) network.subnet[arr_count] = EEPROM.read(a);
      if ((a-20) == 8 && (a-20) < 12) network.gateway[arr_count] = EEPROM.read(a);
      arr_count++;
    }

    Ethernet.begin(network.mac, network.ip, network.gateway, network.subnet);
    Serial.println(F("OK"));
  }
}
//**********************************************************************//



//*************************************************************************//
//FUNCTION TO IMPORT SENSORS PARAMETERS IN STRUCTURE                     //
//***********************************************************************//

void sensors_setup(char data[])
{
      Serial.println(F("Loading Sensors: "));   
      Serial.print("Input data: ");
      Serial.print(data);
      Serial.println("]");
      int line = 0;
      uint8_t row = 0;
      char* sensor_data = strtok(data,",");
      while(sensor_data)
      {
          Serial.print("Token: [");
          Serial.print(sensor_data);
          Serial.println("]");
          sensor_data = strtok(NULL,",");
          row++;
      }
      Serial.println(F("OK"));
     
}
//**********************************************************************//


//*************************************************************************//
//FUNCTION TO READ DATA FROM FILE IN SD                                   //
//***********************************************************************//
void file_read(char data[], char path[]){
File file_to_read;
file_to_read = SD.open(path, FILE_READ);
if (file_to_read){
      uint8_t index = 0;
      int lastread;
      while (file_to_read.available())
      {
          lastread = file_to_read.read(); //read 1 byte in cicle
          data[index] = lastread;
          index++;
      }
      data[index] = '\0';
      file_to_read.close();
    } else {
      Serial.println(F("error reading file"));
    }
}
//**********************************************************************//


void setup() {
  Serial.begin(19200);
  Wire.begin();
  boolean sd_start = SD.begin(4);
  char sd_tmpdata[250];
  if(sd_start) {
      file_read(sd_tmpdata, file_sensors_cfg);
      sensors_setup(sd_tmpdata);
      file_read(sd_tmpdata,file_net_cfg);
      network_setup(sd_tmpdata,from_eeprom);
  }  else {
      from_eeprom = true;
     network_setup(netcfg,from_eeprom);
  }



void loop(){

}
This is the serial output:
Code:
Loading Sensors:
Input data: 0,D,12,DS18B20,0,Sensore_temperatura_esterno
1,I,8,TMP102,72,sensore_temperatura_interno
2,A,0,MQ5,0,Sensore_GAS_cucina
3,W,7,DHT11,0,Sensore_temp-unidita_camera
4,A,1,CTSENSOR,0,Sensore_consumi]
Token: [0]
Token: [D]
Token: [12]
Token: [DS18B20]
Token: [0]
Token: [Sensore_temperatura_esterno
1]
Token: [I]
Token: [8]
Token: [TMP102]
Token: [72]
Token: [sensore_temperatura_interno
2]
Token: [A]
Token: [0]
Token: [MQ5]
Token: [0]
Token: [Sensore_GAS_cucina
3]
Token: [W]
Token: [7]
Token: [DHT11]
Token: [0]
Token: [Sensore_temp-unidita_camera
4]
Token: [A]
Token: [1]
Token: [CTSENSOR]
Token: [0]
Token: [Sensore_consumi]
OK
Loading ethernet from SD: OK
IP: 192.168.1.20
But, if i add line with strcpy(), Arduino crash. This is the code:
Code:

...........

//*************************************************************************//
//FUNCTION TO IMPORT SENSORS PARAMETERS IN STRUCTURE                     //
//***********************************************************************//

void sensors_setup(char data[])
{
      Serial.println(F("Loading Sensors: "));   
      Serial.print("Input data: ");
      Serial.print(data);
      Serial.println("]");
      int line = 0;
      uint8_t row = 0;
      char* sensor_data = strtok(data,",");
      while(sensor_data)
      {
          Serial.print("Token: [");
          Serial.print(sensor_data);
          Serial.println("]");
          if(row == 0)strcpy(sensor[line].index,sensor_data); //with this line Arduino crash
          sensor_data = strtok(NULL,",");
          row++;
      }
      Serial.println(F("OK"));
     
}
//**********************************************************************//

.............

With this code..........no output from serial console. Where's the error?! Why the strcpy makes arduino crashes? I have no idea how to solve this problem!PLEASE HELP ME smiley-sad
Logged

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

Quote
With this code..........no output from serial console. Where's the error?! Why the strcpy makes arduino crashes? I have no idea how to solve this problem!PLEASE HELP ME
Your debug output is telling you what at least part of the problem is.
Quote
Token: [Sensore_temperatura_esterno
1]
Does that look right to you?

You are assuming that every token ends with a comma, and that is not the case. The strtok() function takes a string, not a char, as the 2nd argument, and looks for a token that ends with one of the characters in the string. So, your string of delimiters should be ",\n", not ",".

With the strcpy() call uncommented, are you saying that you get NO serial output?
Logged

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

I fixed the code by adding ",\n\r" to sensors_setup() and ".\n\r" to network_setup()!! Thanks PaulS smiley
Quote
With the strcpy() call uncommented, are you saying that you get NO serial output?
Even with the code fixed, with strcpy() uncommented there isn't output from serial and the Arduino led remain on!!! Here the fixed code:
Code:
#include <EEPROM.h>
#include <SD.h>
#include <SPI.h>
#include <Ethernet.h>
#include <Wire.h>
#include <OneWire.h>

#define MAX_SENSORS 5

typedef struct{
  char index[2];
  char pintype[3];
  char pin[2];
  char model[11];
  char address[11];
  char description[31];
}SENSORS_T;

typedef struct{
  byte ip[4];
  byte subnet[4];
  byte gateway[4];
  byte mac[6];
} NETWORK_T;

SENSORS_T sensor[MAX_SENSORS];
NETWORK_T network;

char file_net_cfg[] = {
  "config/net.cfg"};
char file_sensors_cfg[] = {
  "config/sensors.cfg"};

boolean from_eeprom = false;

//*************************************//
//FUNCTION FOR EEPROM DEBUG           //
//***********************************//
/*void read_eeprom()
{
  for (int a = 20; a < 38; a++)
  {
    byte eeread = EEPROM.read(a);
    Serial.print("EEPROM position: ");
    Serial.print(a);
    Serial.print(" contains ");
    Serial.println(eeread);
  }
}*/
//********************************//


//*************************************************************************//
//FUNCTION TO START NETWORK & IMPORT PARAMETERS IN STRUCTURE              //
//***********************************************************************//
void network_setup(char data[],boolean &eeprom )
{
  network.mac[0] = 0xA2;
  network.mac[1] = 0xDA;
  network.mac[2] = 0x00;
  network.mac[3] = 0x22;
  network.mac[4] = 0x27;
  network.mac[5] = 0x90;
  Serial.print(F("Loading ethernet from "));
  if (!eeprom)
  {
      uint8_t a = 0;
      char* net_data = strtok(data,".\n\r");
      Serial.print(F("SD: "));
     while(net_data)
      {
          if(a >= 0 && a < 4 )network.ip[a] = atoi(net_data);
          if(a >= 4 && a < 8)network.subnet[a-4] = atoi(net_data);
          if(a >= 8 && a < 12) network.gateway[a-8]= atoi(net_data);
          net_data = strtok(NULL,".\n\r");
          a++;
      }
      network.ip[4] = '\0';
      network.subnet[4] = '\0';
      network.gateway[4] = '\0';
      network.mac[6] =  '\0';
      Ethernet.begin(network.mac, network.ip, network.gateway, network.subnet);
      Serial.println(F("OK"));
      Serial.print(F("IP: "));
      Serial.println(Ethernet.localIP());
     
  } else {
    Serial.print(F("EEPROM: "));
    uint8_t arr_count = 0; //contatore per array variabili temporanee
    for (uint8_t a = 20; a < 32; a++)
    {
      if (arr_count == 4 && a < 32) arr_count = 0;
      if ((a-20) < 4) network.ip[arr_count] = EEPROM.read(a);
      if ((a-20) == 4 && (a-20) < 8) network.subnet[arr_count] = EEPROM.read(a);
      if ((a-20) == 8 && (a-20) < 12) network.gateway[arr_count] = EEPROM.read(a);
      arr_count++;
    }

    Ethernet.begin(network.mac, network.ip, network.gateway, network.subnet);
    Serial.println(F("OK"));
  }
}
//**********************************************************************//



//*************************************************************************//
//FUNCTION TO IMPORT SENSORS PARAMETERS IN STRUCTURE                     //
//***********************************************************************//

void sensors_setup(char data[])
{
      Serial.println(F("Loading Sensors: "));   
      Serial.print("Input data: ");
      Serial.print(data);
      Serial.println("]");
      int line = 1;
      int row = 0;
      char* sensor_data = strtok(data,",\n\r");
     while(sensor_data)
      {
          Serial.print("Token: [");
          Serial.print(sensor_data);
          Serial.println("]");
          if(row == 0)strcpy(sensor[line].index,sensor_data); //with this line Arduino crash and no output from console
          sensor_data = strtok(NULL,",\n\r");
          row++;
      }
      Serial.println(F("OK"));
     
}
//**********************************************************************//


//*************************************************************************//
//FUNCTION TO READ DATA FROM FILE IN SD                                   //
//***********************************************************************//
void file_read(char data[], char path[]){
File file_to_read;
file_to_read = SD.open(path, FILE_READ);
if (file_to_read){
      uint8_t index = 0;
      int lastread;
      while (file_to_read.available())
      {
          lastread = file_to_read.read(); //read 1 byte in cicle
          data[index] = lastread;
          index++;
      }
      data[index] = '\0';
      file_to_read.close();
      Serial.println("file reading");
    } else {
      Serial.println(F("error reading file"));
    }
}
//**********************************************************************//


void setup() {
  Serial.begin(19200);
  Wire.begin();
  boolean sd_start = SD.begin(4);
  char ip_tmpdata[50];
  char sens_tmpdata[250];
 
  if(sd_start) {
      file_read(sens_tmpdata, file_sensors_cfg);
      sensors_setup(sens_tmpdata);
      file_read(ip_tmpdata,file_net_cfg);
      network_setup(ip_tmpdata,from_eeprom);
  }  else {
      from_eeprom = true;
      network_setup(ip_tmpdata,from_eeprom);
  }



void loop(){

}
without line
Quote
if(row == 0)strcpy(sensor[line].index,sensor_data); //with this line Arduino crash and no output from console
The output is:
Code:
file reading
Loading Sensors:
Input data: 0,D,12,DS18B20,0,Sensore_temperatura_esterno,
1,I,8,TMP102,72,sensore_temperatura_interno,
2,A,0,MQ5,0,Sensore_GAS_cucina,
3,W,7,DHT11,0,Sensore_temp-unidita_camera,
4,A,1,CTSENSOR,0,Sensore_consumi,]
Token: [0]
Token: [D]
Token: [12]
Token: [DS18B20]
Token: [0]
Token: [Sensore_temperatura_esterno]
Token: [1]
Token: [I]
Token: [8]
Token: [TMP102]
Token: [72]
Token: [sensore_temperatura_interno]
Token: [2]
Token: [A]
Token: [0]
Token: [MQ5]
Token: [0]
Token: [Sensore_GAS_cucina]
Token: [3]
Token: [W]
Token: [7]
Token: [DHT11]
Token: [0]
Token: [Sensore_temp-unidita_camera]
Token: [4]
Token: [A]
Token: [1]
Token: [CTSENSOR]
Token: [0]
Token: [Sensore_consumi]
OK
file reading
Loading ethernet from SD: OK
IP: 192.168.1.20
Logged

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

Have you mentioned which Arduino you are using?

Code:
typedef struct{
  char index[2];
  char pintype[3];
  char pin[2];
  char model[11];
  char address[11];
  char description[31];
}SENSORS_T;
That's 60 bytes per instance.

Code:
#define MAX_SENSORS 5
SENSORS_T sensor[MAX_SENSORS];
That's 300 bytes total.

Code:
  char ip_tmpdata[50];
  char sens_tmpdata[250];
There's another 300 bytes. That's 600 of the 2000 on a Duemilanove/UNO/Mini, etc. That's a significant chunk, then you have these:
Code:
#include <EEPROM.h>
#include <SD.h>
#include <SPI.h>
#include <Ethernet.h>
#include <Wire.h>
#include <OneWire.h>
Presumably, those are not there to increase the line count of the program.

I really think that you need to decrease the size of sens_tmpdata[], and process one record at a time from the file.

Before making that change, though, search for the FreeMemory() (or MemoryFree(), I can never remember which - I don't have enough either) function, and see just how much memory you have available.
Logged

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

Quote
char ip_tmpdata[50];
  char sens_tmpdata[250];
There's another 300 bytes. That's 600 of the 2000 on a Duemilanove/UNO/Mini, etc. That's a significant chunk, then you have these:

I've deleted this var and add only one var "char sd_tmpdata[220];", but with the same result (Arduino crashes).
I've Arduino Uno with Ethernet shield. I've tried to use avr-size to view a memory usage and this is a result:
Code:
text    data     bss     dec     hex filename
17388     156    1295   18839    4997 C:\******\Temp\build8581348915086550974.tmp\domotic.cpp.elf
156+1295=1451......It is not out of memory or I'm wrong?
Logged

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

IT'S A MEMORY OVERFLOW PROBLEMMMMMMMMMMMMMMMM! I've commented the network function and the Ethernet define and............................the sketch with strcpy run correctly!!!!!! Thanks PaulS thanks all
Logged

Pages: 1 [2]   Go Up
Jump to: