How can I store in an struct member and after that print them using Serial.println() if the data to store is a stringstream?, framework arduino ESP32

huangapple go评论52阅读模式
英文:

How can I store in an struct member and after that print them using Serial.println() if the data to store is a stringstream?, framework arduino ESP32

问题

I can provide a translation of your code and explanation. Here's the translated code and explanation:

我正在尝试打印一些以字符串形式接收的数据,为此我正在使用以下函数(`FechaProg` 在函数之后进行定义)

uint8_t Extrae_Data(string trama, FechaProg* destino ,char sep) {
  uint8_t contador = 0;
  string lectura;
  stringstream cadena_leida(trama);
  Serial.print("The frame is: ");
  Serial.println(trama.c_str());
  Serial.print("The stringstream is: ");
  Serial.println(cadena_leida.str().c_str());
  while(getline(cadena_leida, lectura, sep))
  {
    if(sep == '-')
    {
      switch(contador)
      {
        case 1:
        {
          destino->mes = lectura.c_str();
        }
        break;
        case 2:
        {
          destino->dia = lectura.c_str();
        }
        break;
        case 3:
        {
          destino->ano = lectura.c_str();
        }
      }
    }
    else if(sep == ':')
    {
      switch(contador)
      {
        case 1:
        {
          destino->hora = lectura.c_str();
        }
        break;
        case 2:
        {
          destino->minuto = lectura.c_str();
        }
        break;
      }
    }
    contador++;
    Serial.println(contador);
    if(contador == 3)
    {
      Serial.print("The string is: ");
      Serial.println(destino->mes);
      Serial.println(destino->dia);
      Serial.println(destino->ano);
    }
    if(contador == 2)
    {
      Serial.print("The string is: ");
      Serial.println(destino->hora);
      Serial.println(destino->minuto);
    }
  }
  return contador;
}

我使用的指针类型 FechaProg 的结构如下:

struct FechaProg {
  const char* dia;
  const char* mes;
  const char* ano;
  const char* hora;
  const char* minuto;
  const char* segundo;
};

当然,我实例化 FechaProg 类型的对象如下:

FechaProg tiempo = {"0"};

并且函数的调用如下:

Extrae_Data(jsonObj["fecha"], &tiempo, '-');

其中 jsonObj["fecha"] 是接收到的数据。

如果在将信息存储在指针之前打印 tramacadena_leida.str().c_str(),则两者都能正常打印。但是,如果我尝试使用 Serial.println(destino->minuto) 打印指针中存储的值,那么将打印出不可读的信息。

截至目前,我无法解决这个问题。

2023年1月4日更新:Extrae_Data 函数的输入参数是一个字符串、一个结构体的指针和一个分隔符。

在函数内部,我有一个计数器,用于在找到每个标记后将其存储在结构体的特定成员中;例如,微控制器接收到日期 2023:01:01,分隔符是 ":",然后我希望标记存储如下:

destino->ano = 2023;
destino->mes = 01;
destino->dia = 01;

这必须在 getline 函数从字符串中提取标记后执行,这是为了在函数之外执行其他操作,但似乎是将从字符串中提取的标记分配给结构体成员的指针是我做错了的地方。

我再次重申,我没有将字符串视为结构体,但我想将从字符串中提取的标记存储在结构体的成员中。那么,如何打印这个?

提前感谢您的帮助。

英文:

I'm trying to print some data that I received as a string and for that I'm using the following function (FechaProg definition after the function)

uint8_t Extrae_Data(string trama, FechaProg* destino ,char sep) {
  uint8_t contador = 0;
  string lectura;
  stringstream cadena_leida(trama);
  Serial.print("La trama es: ");
  Serial.println(trama.c_str());
  Serial.print("El stringstream es: ");
  Serial.println(cadena_leida.str().c_str());
  while(getline(cadena_leida, lectura, sep))
  {
    if(sep == '-')
    {
      switch(contador)
      {
        case 1:
        {
          destino->mes = lectura.c_str();
        }
        break;
        case 2:
        {
          destino->dia = lectura.c_str();
        }
        break;
        case 3:
        {
          destino->ano = lectura.c_str();
        }
      }
    }
    else if(sep == ':')
    {
      switch(contador)
      {
        case 1:
        {
          destino->hora = lectura.c_str();
        }
        break;
        case 2:
        {
          destino->minuto = lectura.c_str();
        }
        break;
      }
    }
    contador++;
    Serial.println(contador);
    if(contador == 3)
    {
            //Serial.print("Lectura: ");
      Serial.print("La cadena es: ");
      Serial.println(destino->mes);
      Serial.println(destino->dia);
      Serial.println(destino->ano);
    }
    if(contador == 2)
    {
      //Serial.print("Lectura: ");
      Serial.print("La cadena es: ");
      Serial.println(destino->hora);
      Serial.println(destino->minuto);
    }
  }
  return contador;
}

The Fechaprog type that I'm using as a pointer is a struct that looks like follows

struct FechaProg {
  const char* dia;
  const char* mes;
  const char* ano;
  const char* hora;
  const char* minuto;
  const char* segundo;
};

Of course I'm instantiating the object of the type FechaProg like follows

FechaProg tiempo = {"0"};

and the function is called like follows

Extrae_Data(jsonObj["fecha"], &tiempo, '-')

where jsonObj["fecha"] is the data that is received.

If trama and cadena_leida.str().c_str() are printed before storing the information in the pointer the both are well printed but if I try to print the values stored in the pointers with Serial.println(destino->minuto) then not readeable information is printed.

Currently I couldn't solve this problem.

UPDATE 01/04/2023: My input arguments for the Extrae_Data function are a string, a pointer to a struct and a separator.

Inside the function I have a counter which serves to me to store each token in an specific member of the struct once each token is founded; for example, the micro receives the date 2023:01:01 where the separator is ":", then I want the tokens stored like follows:

destino->ano = 2023;
destino->mes = 01;
destino->dia = 01;

This must be done after the getline function extracts the token from the string, this is to make other operation outside the function but is apparently the assigning of the obtained token to the pointer of the member of the struct what I'm doing wrong.

I repeat, I'm not treating the string as a struct but I want to store the token extracted from the string in the members of the struct.

So, how can I print this?

Thanks in advance for the help.

答案1

得分: 0

这不是对你的问题的完整回答,因为你的问题只能部分回答。Extrae_Data() 函数似乎不太合理,需要更多的解释或提供在你的代码中如何使用它的附加上下文。

这只回答了如何根据你的更新于 2023 年 01 月 04 日的评论将字符串解析到一个结构体中。

如果你有一个字符串数据 2023:01:01,并希望将其解析并分配给结构体的不同元素,你可以使用经典的 strtok() 函数来实现:

struct FechaProg {
  const char* dia;
  const char* mes;
  const char* ano;
  const char* hora;
  const char* minuto;
  const char* segundo;
};

// 请注意这不是你原始函数的签名
void Extrae_Data(char* trama, FechaProg* destino ,const char* sep) {
    destino->ano  = strtok(trama, sep);
    destino->mes = strtok(NULL, sep);
    destino->dia = strtok(NULL, sep);
}

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

    char str[]="2023:01:01";
    const char* separator = ":";
    
    FechaProg myStruct{};
    
    Extrae_Data(str, &myStruct, separator);
    // 由于你正在使用 esp32,printf 是可用的
    Serial.printf("%s %s %s\n", myStruct.ano, myStruct.mes, myStruct.dia); 
}
英文:

This is not a complete answer to your problem, as your question is only partially answerable. The Extrae_Data() function doesn't make a lot of sense and needs more explanation or provide addition context on how it is used in your code.

This only answer how to parse a string into a struct based on your updated comment 01/04/2023.

If you have a piece of data as a string 2023:01:01, and you want to parse it and assign to different elements of a struct, you can use the good old strtok() function to achieve that:

struct FechaProg {
  const char* dia;
  const char* mes;
  const char* ano;
  const char* hora;
  const char* minuto;
  const char* segundo;
};

// please noted this is not your original function signature
void Extrae_Data(char* trama, FechaProg* destino ,const char* sep) {
    destino->ano  = strtok(trama, sep);
    destino->mes = strtok(NULL, sep);
    destino->dia = strtok(NULL, sep);
}

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

    char str[]="2023:01:01";
    const char* separator = ":";
    
    FechaProg myStruct{};
    
    Extrae_Data(str, &myStruct, separator);
    //since you are using esp32, printf is available
    Serial.printf("%s %s %s\n", myStruct.ano, myStruct.mes, myStruct.dia); 
}


</details>



huangapple
  • 本文由 发表于 2023年3月31日 04:20:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75892663.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定