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

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

问题描述

我已经尝试了几个小时在 arduino 上组合一个简单的 JSON 对象字符串以发送到正在运行的 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).

知道会发生什么吗?

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

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 }";

果然,它开始工作了:

{ "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天全站免登陆