Arduino中的字符串长度是否有限制? [英] Are there limits on string length in Arduino?

查看:968
本文介绍了Arduino中的字符串长度是否有限制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图将一个简单的JSON对象字符串放在arduino上,以发送到Raspberry Pi运行节点.

I've been trying for hours to put together a simple JSON object string on an arduino to send to a Raspberry Pi running node.

我似乎无法成功构建字符串.我已经尝试过一次构建字符串:

I cannot seem to successfully build the string. I have tried building the string all in one go:

"{" + string1 + "," + string2 + "," + string3 + "}" etc...

我也尝试过使用String.replace函数.每次结束时,我只需要一点点字符串,或者根本不输入. 下面的代码显示了正在发生的事情:

I've also tried using a the String.replace function. Each time I end up with a bit of my string, or none at all. The below code shows what's happening:

String msg = "{ \"message\" : \"statusUpdate\", ";
String active = " \"active\" : TOKEN, ";
String intakeTemp = " \"intakeTemp\" : TOKEN, ";
String intakeHumid = " \"intakeHumid\" : TOKEN, ";
String exhaustTemp = " \"exhaustTemp\" : TOKEN, ";
String exhaustHumid = " \"exhaustHumid\" : TOKEN, ";
String targetHumid = " \"targetHumid\" : TOKEN, ";
String completed = " \"taskCompleted\" : TOKEN }";


if(isActive)
  active.replace("TOKEN","true");
else
  active.replace("TOKEN","false");

intakeTemp.replace("TOKEN",floatToString(intakeTemperature,0));
intakeHumid.replace("TOKEN",floatToString(intakeHumidity,0));
exhaustTemp.replace("TOKEN",floatToString(exhaustTemperature,0));
exhaustHumid.replace("TOKEN",floatToString(exhaustHumidity,0));
targetHumid.replace("TOKEN",floatToString(targetHumidity,0));

if(taskFinished)
  taskCompleted.replace("TOKEN","true");
else
  taskCompleted.replace("TOKEN","false");



  String body = msg;
  Serial.println(body);
  body += active;
  Serial.println(body);
  body += intakeTemp;
  Serial.println(body);
  body += intakeHumid;
  Serial.println(body);
  body += exhaustTemp;
  Serial.println(body);
  body += exhaustHumid;
  Serial.println(body);
  body += targetHumid;
  Serial.println(body);
  body += taskCompleted;
  Serial.println(body);

您可以从上面的最后一行代码中看到,在构建字符串时,我将其吐到了串行监视器上.但是,这是我的串行输出:

You can see from the last bit of code above that as the string is being built, i'm spitting it out to the serial monitor. However, here is my serial output:

{ "message" : "statusUpdate", 
{ "message" : "statusUpdate",  "active" : false, 
{ "message" : "statusUpdate",  "active" : false,  "intakeTemp" : 0.0, 
{ "message" : "statusUpdate",  "active" : false,  "intakeTemp" : 0.0,  "intakeHumid" : 0.0, 
{ "message" : "statusUpdate",  "active" : false,  "intakeTemp" : 0.0,  "intakeHumid" : 0.0,  "exhaustTemp" : 0.0, 
{ "message" : "statusUpdate",  "active" : false,  "intakeTemp" : 0.0,  "intakeHumid" : 0.0,  "exhaustTemp" : 0.0, 
{ "message" : "statusUpdate",  "active" : false,  "intakeTemp" : 0.0,  "intakeHumid" : 0.0,  "exhaustTemp" : 0.0, 
{ "message" : "statusUpdate",  "active" : false,  "intakeTemp" : 0.0,  "intakeHumid" : 0.0,  "exhaustTemp" : 0.0, 

字符串长度是否有限制?我在文档中没有发现任何有关此类限制的信息. 除了标准的Ethernet库和通过HTTP请求(来自示例项目)发送代码的代码外,草图没有太多其他内容.

Is there a limit to the string length? I haven't found any mention of such limits in the docs. There's not much else to the sketch except the standard Ethernet library and the code to send it via an HTTP request (from the sample project).

任何想法都可能会发生什么?

Any idea what might be happening?

好的,我这样缩短了字符串:

Ok, I've shortened my string like so:

String msg = "{ \"m\" : \"status\", ";
String active = " \"a\" : TOKEN, ";
String intakeTemp = " \"iT\" : TOKEN, ";
String intakeHumid = " \"iH\" : TOKEN, ";
String exhaustTemp = " \"eT\" : TOKEN, ";
String exhaustHumid = " \"eH\" : TOKEN, ";
String targetHumid = " \"tH\" : TOKEN, ";
String dryerJustFinished = " \"f\" : TOKEN }";

可以肯定的是,它已经开始起作用:

and sure enough, it's started to work:

{ "m" : "status", 
{ "m" : "status",  "a" : false, 
{ "m" : "status",  "a" : false,  "iT" : 0.0, 
{ "m" : "status",  "a" : false,  "iT" : 0.0,  "iH" : 0.0, 
{ "m" : "status",  "a" : false,  "iT" : 0.0,  "iH" : 0.0,  "eT" : 0.0, 
{ "m" : "status",  "a" : false,  "iT" : 0.0,  "iH" : 0.0,  "eT" : 0.0,  "eH" : 0.0, 
{ "m" : "status",  "a" : false,  "iT" : 0.0,  "iH" : 0.0,  "eT" : 0.0,  "eH" : 0.0,  "tH" : 0.0, 
{ "m" : "status",  "a" : false,  "iT" : 0.0,  "iH" : 0.0,  "eT" : 0.0,  "eH" : 0.0,  "tH" : 0.0,  "f" : false }

这意味着有限制.这是内存限制吗?

Which means there is a limitation. Is this a memory limitation?

顺便说一句,硬件是Arduino Uno R3

By the way, the hardware is an Arduino Uno R3

推荐答案

Atmel处理器的内存管理相当有限,因此很容易以零碎的内存结束.请记住,运行时堆栈和堆也受到限制.

Atmel processor has fairly limited memory management so its easy to end up with fragmented memory. Remember the runtime stack and heap also limited.

静态字符串也可以放入PROGMEM

Static string can be put into PROGMEM also

在arduino.cc上还有一个freememory函数,它将向您显示您有多少可用内存.

There is also a freememory function on the arduino.cc that will show you how much free memory you have.

这篇关于Arduino中的字符串长度是否有限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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