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

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

问题描述

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

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 内存不足.它很少,例如,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 关键字.

这在您的特定情况下不是很有帮助,您必须为候选人查看程序的其余部分.或者升级您的硬件,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天全站免登陆