首页 > 解决方案 > Arduino 将字符串复制到 char * 并使用外部函数

问题描述

我正在使用 ESP32 WIFI 微控制器开发程序,我需要扫描附近可用的 WiFi 网络并将它们存储在列表中,以便稍后在函数之外使用它们。我最初设法使用 String 类型变量轻松完成所有工作,但我听到很多关于在 Arduino 中使用 String 的坏话,所以我现在尝试使用 char* 做所有事情,我发现这比 String 更具挑战性。

我找到了一篇描述如何将字符串转换为字符数组的文章: https ://circuits4you.com/2018/03/08/arduino-convert-string-to-character-array/

// Define 
String str = "This is my string"; 

// Length (with one extra character for the null terminator)
int str_len = str.length() + 1; 

// Prepare the character array (the buffer) 
char char_array[str_len];

// Copy it over 
str.toCharArray(char_array, str_len);

在我的代码中:我初始化了一个全局变量 char 数组,我想在其中存储我的信息。

char* list_strings[10];

然后我调用函数来扫描网络:

int scan_wifi_networks(){

  Serial.println("scan start");
    // WiFi.scanNetworks will return the number of networks found
    int n = WiFi.scanNetworks();
    Serial.println("scan done");
    if (n == 0) {
        Serial.println("no networks found");
    } else {
        Serial.print(n);
        Serial.println(" networks found");
        for (int i = 0; i < n; ++i) {
            
            int str_len = WiFi.SSID(i).length() + 1;  // this is to know the the length of the string +1
            char temp_buffer[str_len]; // create a temporary storage to copy the string to char array
            WiFi.SSID(i).toCharArray(temp_buffer, str_len); // converts string to char array 
            Serial.print("temp buffer is set to=");
            Serial.println(temp_buffer);
            list_strings[i] = temp_buffer;
        }
    }
    Serial.print("list of strings inside scanning function");
    for(int i=0 ; i<=n ; i++){
      Serial.println(list_strings[i]); // prints a lot of garbage??
    }
    return n;
}

上面的函数将扫描网络并假设将字符串变量 (WiFi.SSID) 转换为 char 数组,然后将其存储到我的列表中:

list_strings[i] = temp_buffer;

我的串行监视器: 串行监视器图像

它能够在 for 循环中正确打印出 wifi 名称,但是一旦它退出 for 循环,它就会打印一些垃圾。

感谢任何帮助

更新1

我正在尝试打印 temp_buffer 的地址:

char temp_buffer[str_len]; // create a temporary storage to copy the string to char array
int ptr =  &temp_buffer;
Serial.print("Pointer :" );
Serial.println(ptr);

但没有运气。我得到一个从 char* 到 int 错误的无效转换

更新2

我已将 temp_buffer 转换为 int 并且可以编译,但是,我不知道这是否正确。

            char temp_buffer[str_len]; // create a temporary storage to copy the string to char array
            int ptr =  (int)&temp_buffer;
            Serial.print("Pointer :" );
            Serial.println(ptr);

串行监视器:

Pointer :1073422080
temp buffer is set to=Telia-33F8A3-Greitas
Pointer :1073422080
temp buffer is set to=HUAWEI-STOLYARCHUK
Pointer :1073422080
temp buffer is set to=Telia-E619A5-Greitas
Pointer :1073422080
temp buffer is set to=Telia-ED7E29-Greitas
Pointer :1073422096
temp buffer is set to=MEZON_1156F3
Pointer :1073422080
temp buffer is set to=Carolyne’s iPhone
Pointer :1073422080
temp buffer is set to=Teo-E6A379-Greitasis
Pointer :1073422096
temp buffer is set to=HH40V_BE2F
Pointer :1073422096
temp buffer is set to=Fishman
Pointer :1073422096
temp buffer is set to=MEZON_9EE8
Pointer :1073422096
temp buffer is set to=4G-Gateway-4981
Pointer :1073422080
temp buffer is set to=HUAWEI-B525-C466
Pointer :1073422096
temp buffer is set to=DGD
Pointer :1073422096
temp buffer is set to=4G-Gateway-D29C
Pointer :1073422096
temp buffer is set to=WIFI 2021
Pointer :1073422080
temp buffer is set to=Telia-E79C95-Greitas
Pointer :1073422080
temp buffer is set to=Telia-E5E1B9-Greitas
Pointer :1073422096
temp buffer is set to=#Telia-1E6E7A
list of strings inside scanning function

由于某些奇怪的原因,指针不时从 1073422080 变为 1073422096

标签: c++stringarduinochar

解决方案


问题是char temp_buffer[str_len];。这会在堆栈上分配内存(几乎可以肯定在 for 循环的每次迭代中都有相同的内存位。我很惊讶这个编译,因为我希望得到一个错误,抱怨 str_len 不是一个常数。无论如何,你正在保存堆栈区域的地址,可在每次迭代以及退出循环后重复使用。在每次迭代时打印 temp_buffer 的地址可能会提供信息。

您可以改为使用 new() 在堆上分配内存,并记住在完成后使用 delete() 释放它。

更好的是使用 STL 中的字符串和向量。


推荐阅读