Arduino子字符串不起作用 [英] Arduino substring doesn't work

查看:222
本文介绍了Arduino子字符串不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个静态方法,可以将TAG之间的值搜索(并返回)到String msg

I have a static method that searches (and returns) into String msg the value between a TAG

这是代码功能:

static String genericCutterMessage(String TAG, String msg){
    Serial.print("a-----");
    Serial.println(msg);
    Serial.print("b-----");
    Serial.println(TAG);
    if(msg.indexOf(TAG) >= 0){
        Serial.print("msg ");
        Serial.println(msg);
        int startTx = msg.indexOf(TAG)+3;
        int endTx = msg.indexOf(TAG,startTx)-2;
        Serial.print("startTx ");
        Serial.println(startTx);
        Serial.print("endTx ");
        Serial.println(endTx);
        String newMsg = msg.substring(startTx,endTx);
        Serial.print("d-----");
        Serial.println(newMsg);
        Serial.println("END");
        Serial.println(newMsg.length());
        return newMsg;
    } else {
        Serial.println("d-----TAG NOT FOUND");
        return "";
    }
}

这是输出

a-----[HS][TS]5132[/TS][TO]5000[/TO][/HS]
b-----HS
msg [HS][TS]5132[/TS][TO]5000[/TO][/HS]
startTx 4
endTx 30
d-----
END
0
fake -_-'....go on!  <-- print out of genericCutterMessage

在这种情况下,我想返回HS标签之间的字符串,所以我的预期输出是

in that case I want return the string between HS tag, so my expected output is

[TS]5132[/TS][TO]5000[/TO]

但是我不知道为什么我收到一个空字符串.

but I don't know why I receive a void string.

要了解子字符串的工作原理,我只是按照Arduino官方网站上的教程进行操作

to understand how substring works I just followed tutorial on official Arduino site

http://www.arduino.cc/en/Tutorial/StringSubstring

我不是C ++和Arduino方面的专家,但这看起来像是刷新或缓冲问题,不是吗?

I'm not an expert in C++ and Arduino but this looks like a flushing or buffering problem, isn't it?

有什么主意吗?

推荐答案

您的代码正确,这不会发生.这迫使您考虑可能会失败的意外方式.我真的可以想到只有一个候选事故,您的Arduino的RAM即将用完.它只有很少的空间,例如Uno只有2 KB.不需要花费太多的时间就可以将其填满.

Your code is correct, this should not happen. Which forces you to consider the unexpected ways that this could possibly fail. There is really only one candidate mishap I can think of, your Arduino is running out of RAM. It has very little, the Uno only has 2 kilobytes for example. It doesn't take a lot of string munching to fill that up.

这不是很顺利地报告.我所能做的就是将您指向相关的公司页面.报价:

This is not reported in a smooth way. All I can do is point you to the relevant company page. Quoting:

如果用完SRAM,您的程序可能会以意外的方式失败;它似乎可以成功上传,但无法运行,或者运行异常.要检查这种情况是否发生,可以尝试注释掉或缩短草图中的字符串或其他数据结构(无需更改代码).如果它随后成功运行,则可能是SRAM用完了.您可以采取一些措施来解决此问题:

If you run out of SRAM, your program may fail in unexpected ways; it will appear to upload successfully, but not run, or run strangely. To check if this is happening, you can try commenting out or shortening the strings or other data structures in your sketch (without changing the code). If it then runs successfully, you're probably running out of SRAM. There are a few things you can do to address this problem:

  • 如果您的草图与(台式机/笔记本电脑)计算机上运行的程序对话,则可以尝试将数据或计算结果转移到计算机上,从而减轻Arduino的负担.
  • 如果您有查找表或其他大型数组,请使用必要的最小数据类型来存储所需的值;例如,一个int占用两个字节,而一个字节仅使用一个字节(但可以存储较小范围的值).
  • 如果在运行草图时不需要修改字符串或数据,则可以将其存储在闪存(程序)存储器中,而不是SRAM;为此,请使用PROGMEM关键字.
  • If your sketch talks to a program running on a (desktop/laptop) computer, you can try shifting data or calculations to the computer, reducing the load on the Arduino.
  • If you have lookup tables or other large arrays, use the smallest data type necessary to store the values you need; for example, an int takes up two bytes, while a byte uses only one (but can store a smaller range of values).
  • If you don't need to modify the strings or data while your sketch is running, you can store them in flash (program) memory instead of SRAM; to do this, use the PROGMEM keyword.

这对您的具体情况不是很有帮助,您必须查看计划的其余部分以寻找候选人.或升级您的硬件,StackExchange为Arduino爱好者提供了一个专用站点,肯定是获得建议的最佳位置.

That's not very helpful in your specific case, you'll have to look at the rest of the program for candidates. Or upgrade your hardware, StackExchange has a dedicated site for Arduino enthusiasts, surely the best place to get advice.

这篇关于Arduino子字符串不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆