Problem while using Structure

Hello,

I have a function took String as CSV text ( "text,text,text,...,#") then return array of Strings contain the sub-strings witch is inside this CSV string. after that i use this array to set data into class variables, with Set functions that was already built.

I refer the this link https://www.geeksforgeeks.org/return-local-array-c-function/ to build the Structure and the function and they are working properly.

but while I tried to call the function from the void loop() it returned arrays took the exact values mentioned , but when i tried to call it from another function it returns a empty value.

I defined my Structure:

**struct arrStructure { **

** String arr[100]; **
};

And this is my function:

struct arrStructure distrCsvMsgIntoStrgAry(String csvData)
{
** int commasCount = 0;**
** for (unsigned int i = 0; i < csvData.length(); i++)**
** if (csvData.charAt(i) == ',')**
** commasCount++;**

** int partsCount = commasCount + 1; **
** struct arrStructure stringTo;**

** int partsIndex = 0;**
** int indexOfComma = csvData.indexOf(','); **
** while (partsIndex < partsCount)**
** {**
** stringTo.arr[partsIndex] = csvData.substring(0, indexOfComma);**
** csvData = csvData.substring(indexOfComma + 1, csvData.length()); **
** indexOfComma = csvData.indexOf(','); **
** partsIndex++;**
** }**
** return stringTo; **
}

when i need covert CSV String data to Array of Strings :

String dataRecived="confS,A,0,0,150,-5,6,70000,4-20mA,Flow Meter,m3//hr,#";

struct arrStructure stringTo = distrCsvMsgIntoStrgAry(dataRecived);

Serial.println(stringTo.arr[0]); Serial.println(stringTo.arr[1]); Serial.println(stringTo.arr[2]);

when i type this orders inside the void loop() the serial print :
confS
A
0

but if i use same 3 line within another function inside my Program, the serial returns empty values.

kindly help me to solve my problem

Thank you

Hello,

I have a function took String as CSV text ( "text,text,text,...,#") then return array of Strings contain the sub-strings witch is inside this CSV string. after that i use this array to set data into class variables, with Set functions that was already built.

I refer the this link https://www.geeksforgeeks.org/return-local-array-c-function/ to build the Structure and the function and they are working properly.

but while I tried to call the function from the void loop() it returned arrays took the exact values mentioned , but when i tried to call it from another function it returns a empty value.

I defined my Structure:

struct  arrStructure { 

   String arr[100]; 
};

And this is my function:

struct arrStructure distrCsvMsgIntoStrgAry(String csvData)
{
  int commasCount = 0;
  for (unsigned int i = 0; i < csvData.length(); i++)
    if (csvData.charAt(i) == ',')
      commasCount++;


   int  partsCount = commasCount + 1;        
   struct arrStructure stringTo;
                       
   int partsIndex = 0;
   int indexOfComma = csvData.indexOf(',');       
   while (partsIndex < partsCount)
   {
     stringTo.arr[partsIndex] = csvData.substring(0, indexOfComma);
     csvData = csvData.substring(indexOfComma + 1, csvData.length());      
     indexOfComma = csvData.indexOf(',');                                          
     partsIndex++;
   }
 return stringTo; 
}

when i need covert CSV String data to Array of Strings :

String dataRecived="confS,A,0,0,150,-5,6,70000,4-20mA,Flow Meter,m3//hr,#";

struct arrStructure stringTo = distrCsvMsgIntoStrgAry(dataRecived);

Serial.println(stringTo.arr[0]); Serial.println(stringTo.arr[1]); Serial.println(stringTo.arr[2]);

when i type this orders inside the void loop() the serial print :
confS
A
0

but if i use same 3 line within another function inside my Program, the serial returns empty values.

kindly help me to solve my problem

Thank you

How may comma separated values?
Using String this code is going to run out of, and/or fragment, memory very fast.
An array of 100 strings is huge for a start.

Best to use fixed sized buffers and strtok() function.

Duplicate topics merged to preserve replies

Thanks for owning up to the duplicate

hello, thanks pcbbc i will redefine the array with 15 Strings,

Sorry about post duplication, i deleted the first post on the sport but it didn't then i've made a report to solve this duplication.

this the code that i'm following it to make my Idea.

#include
using namespace std;

struct arrWrap {
int arr[100];
};

struct arrWrap fun()
{
struct arrWrap x;

x.arr[0] = 10;
x.arr[1] = 20;

return x;
}

int main()
{
struct arrWrap x = fun();
cout << x.arr[0] << " " << x.arr[1];
return 0;
}

Like this two function "struct arrWrap x = fun(); " "x.arr[1]; " works in the void loop Properly and returned the specific string that was called, but it returns empty values if i used them inside another function in the program.

Why not post the code that causes you problems so that we can help?

i just do this two function, to test the first step, my the problem solved, i was trying to define a empty list in class to fill it later in the program , and i was not give a size for this list.

AnalogPin analogPins[10]; // analogue pin from 0 to 15
int analogIndex=0;
DigitalPin digitalPins[20]={}; // digital pin from 22 to 51
int digitalIndex=0;

after i get the size of two listes "10" and "20" it works.

but i have another problem with Serial.readStringUntil('#')

i tried to do this sketch before potting in the main program , the i try to send this phrase (confS,A0,150,-5,6,70000,#) from the arduino serial. but the program stack on the Serial.readStringUntil('#') .
this is my code

struct  arrStructure { 
 String arr[8];        // this array contain the number of substring in data sent from raspberry pi to arduino
};

int k=0;
void setup() {
  Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}

void loop() {
  // send data only when you receive data:

   readFromRaspberryPi();  
  
}

///////////////////////////////////////////////////////////////////// readFromRaspberryPi ////////////////////
String readFromRaspberryPi()
{  delay(1000);
 unsigned long recivingTimer=millis();
  if(Serial.available()>0)
  { delay(1000);
    String dataRecived = Serial.readStringUntil("#");
//    delay(10);Serial.println("test");
    while ((millis()-recivingTimer)<5000)
    { 
    
//    String dataRecived="confS,A0,150,-5,6,70000,#";       //test    
    delay(100);  
    Serial.println(dataRecived);               
    delay(100);
     if(Serial.readStringUntil("#")=="ok" )
      {
        if (dataRecived.startsWith("confS"))
         {
         }
         }
    }
  }
}

If you're using Python on your RPi, it would make things a lot simpler and more reliable if you used the compatible libraries: pySerialTransfer and SerialTransfer.h.

pySerialTransfer is pip-installable and cross-platform compatible. SerialTransfer.h runs on the Arduino platform and can be installed through the Arduino IDE's Libraries Manager.

yes thank you, i will try this library but i'm did start yet in raspberry pi software I'm trying to test this sketch using Arduino serial reading, so what is the problem with Serial.readStringUntil('#') in this sketch ??

i try to make this small program for testing and the Serial.readStringUntil('#') worked properly.

struct  arrStructure { 
    String arr[20]; 
};


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

}


////////////////////////////////////////// loop ////////////////////////
  void loop() 
{
  if(Serial.available()>0)
  {
    String dataRecived = Serial.readStringUntil("#");
    Serial.println(dataRecived);
  }
}

problem solved thank you,

the problem was because i declared a String function (readFromRaspberryPi()) and i don't return any thing yet from this function, when i turned it to void function the Serial.readStringUntil("#") function works normally.