2016-06-20 75 views
0

我想首先發送整個文本到認定其長度和使用AT + CIPSEND = 長度的函數讀取一些文本服務器的通數據來這裏稍後打開TCP端口。在我的響應數據中添加一些額外的信息之前,一切正常。 我懷疑的字符限制,但無法找到一個理由,即使調試幾個小時。Arduino的字符串連接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++ - 風格包裝圍繞打印庫,我兩年前寫的。它可以讓你寫這樣的東西:

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

默認情況下,庫提供io::cout它通過平常Serial打印。您可以將該庫與任何實現打印界面的對象掛鉤,例如串行軟件序列

與您現在正在做的不同之處在於,通過使用「< <」,您將獲得與「+」相同的乾淨的代碼樣式,但不需要創建臨時String。所有東西都會立即下達到輸出緩衝區以打印出來。因此,您不應該遇到與您現在正在經歷的相同問題。換句話說,這就是@Olaf在評論中提出的建議,但以一種奇特的方式。 ;)

注意:你可能需要修復包括在測試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