【问题标题】:Using a char array on a ESP8266 chip from arduino program在 arduino 程序的 ESP8266 芯片上使用 char 数组
【发布时间】:2019-04-11 18:57:35
【问题描述】:

我在 android 上编写了可以运行的代码,但是在为“通用 8266 板”编译时它不起作用。 我有一个二维字符数组,因为 8266 编译器不接受字符串数组。 它被设置为一组缓冲区和指向它们的指针数组。

char buffer0[80] ; // storage containers
    char buffer1[80] ; 
    char buffer2[80] ;
    char buffer5[80] ;
    char buffer6[80] ; // buffer used to transport strings
    // Matrix of char buffers using pointers
    char *Scans[] = {"buffer0 , buffer1 , buffer2 , buffer3 , buffer4 , buffer5"};

这编译OK并且不会导致堆栈问题。我确实在 Stackoverflow 上查找了答案,但答案是负数或仅适用于单个 char 数组中的字符串,而不是多维的,也不能从数组中返回字符串以进行打印或使用太控制程序。

但是当它在基于 ESP8266 的板的 ESP-01 上运行时,我遇到了一些问题,包括编译器未捕获的堆栈问题错误代码 28/29。 我使用#include 来编译代码。 我试过了

char* strcpy_P(mess,Scans[0]);

mess.toCharArray(buffer0, 80);

mess.toCharArray(Scans[i], 80);

strcpy_P(Scans[i], mess);

Mess 是我想在变量“i”控制循环中放入数组中的字符串。

我也尝试了所有的 char * 到和从字符串 https://arduino-esp8266.readthedocs.io/en/latest/PROGMEM.html?highlight=str#functions-to-read-back-from-progmem 而且它们都不能在 8266 上工作。

我希望能够将多个字符串的汇编存储到一个字符串中。然后将其复制到循环内的一个字符数组(缓冲区)中,使用 i 指示将字符串放入哪个缓冲区。每个都来自不同的 wifi 站,因此不能将它们混为一谈。

然后将其从正确的数组编号中删除,以将其打印为电子邮件中的行。 它必须在 ESP8266WiFi.h 上编译,并且不会因堆栈错误而崩溃,因为它将在没有串行端口将错误转储到的情况下自动工作。

【问题讨论】:

    标签: arrays string arduino char esp8266


    【解决方案1】:

    我已经尝试在我的 Arduino IDE 中编译您的代码,没有问题持续存在,它只是工作。尝试在您的 Arduino IDE 中更新 esp8266 板。

    无论如何,当你创建一个包含多个值的数组时,你应该用逗号分隔每个值,并且每个值都应该用双引号引起来。

    所以,不要使用这个:

        char *Scans[] = {"buffer0 , buffer1 , buffer2 , buffer3 , buffer4 , buffer5"};
    

    你应该使用这个:

        char *Scans[] = {"buffer0" , "buffer1" , "buffer2" , "buffer3" , "buffer4" , "buffer5"};
    

    这是我的完整代码:

        char buffer0[80];
        char buffer1[80];
        char buffer2[80];
        char buffer3[80];
        char buffer4[80];
        char buffer5[80];
    
        char *Scans[] = {"buffer0" , "buffer1" , "buffer2" , "buffer3" , "buffer4" , "buffer5"};
        String mess;
    
        void setup() {}
    
        void loop() {
          for (int i = 0; i <= 5; i++) {
            mess.toCharArray(Scans[i], 80);
          }
        }
    

    希望对您有所帮助。

    【讨论】:

      【解决方案2】:

      Thanks for the suggestion, the final solution that causes no crashes on the ESP-01 (8266 chipset) is // 设置全局变量 char buffer0[80] ; // 存储容器 char buffer1[80] ; char buffer2[80] ; char buffer3[80] ; char buffer4[80] ; char buffer5[80] ;@ 987654328@char buffer6[80] ; // 用于传输字符串的缓冲区 // 使用指针的 char 缓冲区矩阵 char Scans[] = {buffer0 , buffer1 , buffer2 , buffer3 , buffer4 , buffer5 }; // 复制矩阵缓冲回字符串 // string = Scans[i]; // Copies string into Matrix char Array // mess.toCharArray(buffer0, 80); String mess = ""; 超过缓冲区的数量会导致堆栈崩溃。 代码 strcpy_P(Scans[i], mess) 和 char strstr_P(const char* haystack, PGM_P needle) 仅适用于只读常量。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-02-09
        • 1970-01-01
        • 1970-01-01
        • 2020-08-25
        • 1970-01-01
        • 2015-11-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多