2016-06-20 4 views
0

長さを見つけてAT + CIPSEND = という長さの関数にテキスト全体を送信することで、一部のテキストのパスデータをサーバーに読み込もうとしています。でTCPポートを後で開きます。応答データに追加情報を追加する前に、すべてうまくいきました。 私は文字制限の疑いがありますが、数時間のデバッグを行っても理由が​​見つかりませんでした。Arduino String concatenation Serial.println()が機能しない

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 

ありがとうございます。

+0

ArduinoのはCではありません!そしてあなたのコードは、このような小型のデバイスには適していません。 Arduinoのようなベアメタルの組み込みデバイスにデスクトッププログラミング技術を適用することは悪い考えです。 – Olaf

+0

あなたは何をお勧めしますか?またはいくつかの参考文献をリンクしてください。 – eden

+0

これは個人指導サイトではありません。しかし、なぜあなたは断片的にテキストを送信しないのですか? – Olaf

答えて

1

この回答は実際にあなたの問題を解決するものではありません。 私の言いたいことは、Arduinoは私と一緒にいないので、問題を再現できません。 :)あなたが「文字列連結」スタイルにしているので、

は、しかし、私はあなたがここに掲載のソースコードを使用して恩恵を受けるかもしれないと思う(あなたのプロジェクトは、それのための部屋を持っている場合)。

イッツ小さなC++ - スタイル私は2年前に書いた印刷ライブラリのラッパー。デフォルトでは

io::cout << "This is a beautiful message!\n" 
     << io::setprecision(3) << some_float << "\n" 
     << io::setbase(BIN) << some_int << io::endl; 

ライブラリは通常Serialて印刷しio::coutを提供しています。それはあなたがこのようなものを書くようになります。このライブラリを、プリントインターフェイスを実装しているオブジェクトでフックすることができます。 シリアルおよびソフトウェアシリアル

"< <"を使用すると、 "+"と同じクリーンなコードスタイルが得られますが、一時的な文字列を作成する必要はありません。すべてが即座に出力バッファに送られ、出力されます。結果として、あなたは今あなたが経験しているのと同じ問題に陥るべきではありません。言い換えれば、これはオラフがコメントで示唆したことではなく、派手なやり方です。 ;)

:あなたはおそらく、あなたのプロジェクトに合わせてテスト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); 
    } 
    
+1

ありがとうございました。しかし私は私の問題のより簡単な解決策を見つけました。 Serial.printlnを使用するたびに、関数内の文字列がメモリの一部をいっぱいにします。私は私の問題を回避するために外部ライブラリF()を使用しました。 – eden

+1

@EnieJakiroコメントは最終的に削除される可能性があるので、あなたのコメントを回答として投稿してください。まだそれが可能な場合は、それも受け入れるべきです。 :) –

+0

あなたの解決策も私のために働いていて、それはちょっと説明的です。乾杯:) – eden