Arduino 字符串连接 Serial.println() 不起作用
Arduino String concatenation Serial.println() doesn't work
我试图通过首先将整个文本发送到一个函数来读取一些文本的传递数据到服务器,该函数找到它的长度并使用 AT+CIPSEND=length comes here 到稍后打开一个 TCP 端口。在我向响应数据中添加一些附加信息之前,一切正常。 我怀疑字符限制但即使调试了几个小时也找不到原因。
Serial.println() 不显示任何输出,并且字符串连接看起来并不健康。问题是没有传递文本,因此 CIPSEND 不起作用。相应的代码部分及其输出如下所示。
void sendHTTPResponse(int connectionId, String content) {
Serial.println("SENDHTTPRESPONSE1: " + content);
// build HTTP response
String httpResponse;
String httpHeader;
// HTTP Header
httpHeader = "HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=UTF-8\r\n";
httpHeader += "Content-Length: ";
httpHeader += content.length();
httpHeader += "\r\n";
httpHeader += "Connection: close\r\n\r\n";
Serial.println("SENDHTTPRESPONSE2: " + httpHeader);
httpResponse = httpHeader + content + " "; // There is a bug in this code: the last character of "content" is not sent, I cheated by adding this extra space
Serial.println("HTTPRESPONSE3: " + httpResponse);
sendCIPData(connectionId, httpResponse);
}
和串行监视器输出。 HTTPRESPONSE3 似乎有点空?
SENDHTTPRESPONSE1: {"data":[{"id":"100"}]}{"data":[{"id":"100"}]}{"data":[{"id":"100"}]}{"data":[{"id":"100"}]}
SENDHTTPRESPONSE2: HTTP/1.1 200 OK
Content-Type: text/html; charset=UTF-8
Content-Length: 92
Connection: close
DATA LENGTH:
======================================================
Executing command: AT+CIPSEND=0,0
HTTP/1.1
CoAT+CIPSEND=0,0
提前致谢。
这个答案实际上并不能解决您的问题。
恕我直言,我没有 Arduino,所以我无法重现您的问题。 :)
但是,由于您喜欢 "string concatenation" 风格,我认为您可能会受益于使用此处发布的源代码 (前提是您的项目有空间).
这是一个小型 c++ 风格 wrapper 我写了两个 print 库几年前。它会让你写这样的东西:
io::cout << "This is a beautiful message!\n"
<< io::setprecision(3) << some_float << "\n"
<< io::setbase(BIN) << some_int << io::endl;
默认情况下,库提供 io::cout
,它通过通常的 Serial
打印。您可以将此库与任何实现 Print 接口 的对象连接起来,例如序列号 和软件序列号。
与您现在所做的不同之处在于,通过使用“<<”,您将获得与“+”相同的简洁代码风格,但您不需要创建一个临时字符串。一切都立即下岗到输出缓冲区进行打印。因此,您不应遇到与您现在遇到的相同问题。换句话说,这按照@Olaf 在评论中的建议,但以一种奇特的方式进行。 ;)
注意:您可能需要修复 test-iocout.ino 文件中的包含以适合您的项目。
cout.h
#ifndef __COUT_H__
#define __COUT_H__
#include <Arduino.h>
namespace io {
/**
* marker to end a message
* (prints newline)
*/
struct Endl {};
const Endl endl = Endl();
/**
* marker to modify way in which numbers are
* printed on output stream
*/
struct setbase {
uint8_t base;
setbase(uint8_t v = DEC): base(v) {}
};
/**
* marker to modify number of digits of doubles
* printed on output stream
*/
struct setprecision {
uint8_t precision;
setprecision(uint8_t v = 2): precision(v) {}
};
/**
* class out
*
* Provides a C++-like interface for printing stuff on
* an output Stream, e.g. Serial, SoftwareSerial objects
* Assumes a separated Stream initialization handling
*/
class out {
public:
out(Print& print = Serial, uint8_t fmt = DEC, uint8_t dgt = 2);
out(const out& other);
out& operator =(const out& other);
virtual ~out();
inline out& operator<<(const String& msg)
{
__print.print(msg);
return *this;
};
inline out& operator<<(const char msg[])
{
__print.print(msg);
return *this;
};
inline out& operator<<(char c)
{
__print.print(c);
return *this;
};
inline out& operator<<(unsigned char uc)
{
__print.print(uc, __base);
return *this;
};
inline out& operator<<(int n)
{
__print.print(n, __base);
return *this;
};
inline out& operator<<(unsigned int un)
{
__print.print(un, __base);
return *this;
};
inline out& operator<<(long l)
{
__print.print(l, __base);
return *this;
};
inline out& operator<<(unsigned long ul)
{
__print.print(ul, __base);
return *this;
};
inline out& operator<<(double d)
{
__print.print(d, __precision);
return *this;
};
inline out& operator<<(const __FlashStringHelper *fsh)
{
__print.print(fsh);
return *this;
};
inline out& operator<<(const Printable& pr)
{
__print.print(pr);
return *this;
};
inline out& operator<<(const Endl& el)
{
__print.println("");
__base = DEC;
__precision = 2;
return *this;
};
inline out& operator<<(const setbase& p)
{
__base = p.base;
return *this;
};
inline out& operator<<(const setprecision& p)
{
__precision = p.precision;
return *this;
};
inline int getWriteError()
{
return __print.getWriteError();
};
inline void clearWriteError()
{
__print.clearWriteError();
};
private:
Print& __print;
///< output stream, must be separately initalized
uint8_t __base;
///< base with which print numerical data
uint8_t __precision;
///< number of fractional digits of float/double values
};
/**
* Global io::cout object
*/
extern out cout;
} /* namespace io */
#endif
cout.cpp
#include "cout.h"
namespace io {
out cout;
out::out(Print& print, uint8_t fmt, uint8_t dgt):
__print(print), __base(fmt),
__precision(dgt)
{
// nothing to do
};
out::out(const out& other):
__print(other.__print),
__base(other.__base),
__precision(other.__precision)
{
// nothing to do
};
out& out::operator =(const out& other)
{
if (this != &other) {
__print = other.__print;
__base = other.__base;
__precision = other.__precision;
}
return *this;
};
out::~out()
{
// nothing to do
};
} /* namespace io */
测试-iocout.ino
#include <Arduino.h>
#include "src/cout.h"
/******************************************************************************/
/*** PINS & GLOBALS ***/
/******************************************************************************/
const uint32_t SERIAL_BAUDRATE = 4800;
/******************************************************************************/
/*** RESOURCES ***/
/******************************************************************************/
/******************************************************************************/
/*** MAIN ***/
/******************************************************************************/
/**
* setup:
* sets up the resources used within the arduino
*/
void setup()
{
/* Initialize serial */
Serial.begin(SERIAL_BAUDRATE);
while (!Serial)
{
/* Needed by Arduino Leonardo */
}
/* new c++-like access to serial! */
io::cout << "##### Arduino Station #####\n\n"
<< "- io::cout test 1.0" << io::endl;
}
/**
* loop:
*
*/
void loop()
{
/* compute deltaTime */
uint32_t curr_time = millis();
static uint32_t start_time = curr_time; // note: initialized once!
uint32_t deltaTime = curr_time - start_time;
start_time = curr_time;
io::cout << "\n> Last loop duration was: ";
io::cout << io::setprecision(3)
<< deltaTime/1000.0f
<< " s." << io::endl;
io::cout << "> 1025 in binary is: "
<< io::setbase(BIN)
<< 1025 << io::endl;
delay(10000);
}
我试图通过首先将整个文本发送到一个函数来读取一些文本的传递数据到服务器,该函数找到它的长度并使用 AT+CIPSEND=length comes here 到稍后打开一个 TCP 端口。在我向响应数据中添加一些附加信息之前,一切正常。 我怀疑字符限制但即使调试了几个小时也找不到原因。
Serial.println() 不显示任何输出,并且字符串连接看起来并不健康。问题是没有传递文本,因此 CIPSEND 不起作用。相应的代码部分及其输出如下所示。
void sendHTTPResponse(int connectionId, String content) {
Serial.println("SENDHTTPRESPONSE1: " + content);
// build HTTP response
String httpResponse;
String httpHeader;
// HTTP Header
httpHeader = "HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=UTF-8\r\n";
httpHeader += "Content-Length: ";
httpHeader += content.length();
httpHeader += "\r\n";
httpHeader += "Connection: close\r\n\r\n";
Serial.println("SENDHTTPRESPONSE2: " + httpHeader);
httpResponse = httpHeader + content + " "; // There is a bug in this code: the last character of "content" is not sent, I cheated by adding this extra space
Serial.println("HTTPRESPONSE3: " + httpResponse);
sendCIPData(connectionId, httpResponse);
}
和串行监视器输出。 HTTPRESPONSE3 似乎有点空?
SENDHTTPRESPONSE1: {"data":[{"id":"100"}]}{"data":[{"id":"100"}]}{"data":[{"id":"100"}]}{"data":[{"id":"100"}]}
SENDHTTPRESPONSE2: HTTP/1.1 200 OK
Content-Type: text/html; charset=UTF-8
Content-Length: 92
Connection: close
DATA LENGTH:
======================================================
Executing command: AT+CIPSEND=0,0
HTTP/1.1
CoAT+CIPSEND=0,0
提前致谢。
这个答案实际上并不能解决您的问题。 恕我直言,我没有 Arduino,所以我无法重现您的问题。 :)
但是,由于您喜欢 "string concatenation" 风格,我认为您可能会受益于使用此处发布的源代码 (前提是您的项目有空间).
这是一个小型 c++ 风格 wrapper 我写了两个 print 库几年前。它会让你写这样的东西:
io::cout << "This is a beautiful message!\n"
<< io::setprecision(3) << some_float << "\n"
<< io::setbase(BIN) << some_int << io::endl;
默认情况下,库提供 io::cout
,它通过通常的 Serial
打印。您可以将此库与任何实现 Print 接口 的对象连接起来,例如序列号 和软件序列号。
与您现在所做的不同之处在于,通过使用“<<”,您将获得与“+”相同的简洁代码风格,但您不需要创建一个临时字符串。一切都立即下岗到输出缓冲区进行打印。因此,您不应遇到与您现在遇到的相同问题。换句话说,这按照@Olaf 在评论中的建议,但以一种奇特的方式进行。 ;)
注意:您可能需要修复 test-iocout.ino 文件中的包含以适合您的项目。
cout.h
#ifndef __COUT_H__ #define __COUT_H__ #include <Arduino.h> namespace io { /** * marker to end a message * (prints newline) */ struct Endl {}; const Endl endl = Endl(); /** * marker to modify way in which numbers are * printed on output stream */ struct setbase { uint8_t base; setbase(uint8_t v = DEC): base(v) {} }; /** * marker to modify number of digits of doubles * printed on output stream */ struct setprecision { uint8_t precision; setprecision(uint8_t v = 2): precision(v) {} }; /** * class out * * Provides a C++-like interface for printing stuff on * an output Stream, e.g. Serial, SoftwareSerial objects * Assumes a separated Stream initialization handling */ class out { public: out(Print& print = Serial, uint8_t fmt = DEC, uint8_t dgt = 2); out(const out& other); out& operator =(const out& other); virtual ~out(); inline out& operator<<(const String& msg) { __print.print(msg); return *this; }; inline out& operator<<(const char msg[]) { __print.print(msg); return *this; }; inline out& operator<<(char c) { __print.print(c); return *this; }; inline out& operator<<(unsigned char uc) { __print.print(uc, __base); return *this; }; inline out& operator<<(int n) { __print.print(n, __base); return *this; }; inline out& operator<<(unsigned int un) { __print.print(un, __base); return *this; }; inline out& operator<<(long l) { __print.print(l, __base); return *this; }; inline out& operator<<(unsigned long ul) { __print.print(ul, __base); return *this; }; inline out& operator<<(double d) { __print.print(d, __precision); return *this; }; inline out& operator<<(const __FlashStringHelper *fsh) { __print.print(fsh); return *this; }; inline out& operator<<(const Printable& pr) { __print.print(pr); return *this; }; inline out& operator<<(const Endl& el) { __print.println(""); __base = DEC; __precision = 2; return *this; }; inline out& operator<<(const setbase& p) { __base = p.base; return *this; }; inline out& operator<<(const setprecision& p) { __precision = p.precision; return *this; }; inline int getWriteError() { return __print.getWriteError(); }; inline void clearWriteError() { __print.clearWriteError(); }; private: Print& __print; ///< output stream, must be separately initalized uint8_t __base; ///< base with which print numerical data uint8_t __precision; ///< number of fractional digits of float/double values }; /** * Global io::cout object */ extern out cout; } /* namespace io */ #endif
cout.cpp
#include "cout.h" namespace io { out cout; out::out(Print& print, uint8_t fmt, uint8_t dgt): __print(print), __base(fmt), __precision(dgt) { // nothing to do }; out::out(const out& other): __print(other.__print), __base(other.__base), __precision(other.__precision) { // nothing to do }; out& out::operator =(const out& other) { if (this != &other) { __print = other.__print; __base = other.__base; __precision = other.__precision; } return *this; }; out::~out() { // nothing to do }; } /* namespace io */
测试-iocout.ino
#include <Arduino.h> #include "src/cout.h" /******************************************************************************/ /*** PINS & GLOBALS ***/ /******************************************************************************/ const uint32_t SERIAL_BAUDRATE = 4800; /******************************************************************************/ /*** RESOURCES ***/ /******************************************************************************/ /******************************************************************************/ /*** MAIN ***/ /******************************************************************************/ /** * setup: * sets up the resources used within the arduino */ void setup() { /* Initialize serial */ Serial.begin(SERIAL_BAUDRATE); while (!Serial) { /* Needed by Arduino Leonardo */ } /* new c++-like access to serial! */ io::cout << "##### Arduino Station #####\n\n" << "- io::cout test 1.0" << io::endl; } /** * loop: * */ void loop() { /* compute deltaTime */ uint32_t curr_time = millis(); static uint32_t start_time = curr_time; // note: initialized once! uint32_t deltaTime = curr_time - start_time; start_time = curr_time; io::cout << "\n> Last loop duration was: "; io::cout << io::setprecision(3) << deltaTime/1000.0f << " s." << io::endl; io::cout << "> 1025 in binary is: " << io::setbase(BIN) << 1025 << io::endl; delay(10000); }