[英]Detecting characters in C++ char stream
我正在使用一個使用內置wifi的BlackWidow版本的arduino代碼。使用WiServer.h庫,我使用帶有mods的SimpleClient.pde示例向一個只返回一個web服務器的webserver發送一個調用整數 - 0,1或2.最終目標是打開紅綠燈,綠燈或黃燈的引腳。 整數表示我們的Hudson CI的聚合狀態。
我是一個PHP懶惰的混蛋,指針嚇到我了。 我正在使用的代碼是
// Function that prints data from the server
void printData(char* data, int len) {
// Print the data returned by the server
// Note that the data is not null-terminated, may be broken up into smaller packets, and
// includes the HTTP header.
while (len-- > 0) {
Serial.print(*(data++));
}
}
printData()是對Web服務器的調用的回調,並且在運行時它將以下內容發送到串行監視器(這是3個循環,在新輸出之前沒有換行符):
HTTP/1.1 200 OK
Date: Thu, 10 Feb 2011 17:37:37 GMT
Server: Apache/2.2.13 (Unix) mod_ssl/2.2.13 OpenSSL/0.9.8k DAV/2 PHP/5.2.11
X-Powered-By: PHP/5.2.11
Content-Length: 1
Connection: close
Content-Type: text/html
0HTTP/1.1 200 OK
Date: Thu, 10 Feb 2011 17:37:45 GMT
Server: Apache/2.2.13 (Unix) mod_ssl/2.2.13 OpenSSL/0.9.8k DAV/2 PHP/5.2.11
X-Powered-By: PHP/5.2.11
Content-Length: 1
Connection: close
Content-Type: text/html
0HTTP/1.1 200 OK
Date: Thu, 10 Feb 2011 17:37:58 GMT
Server: Apache/2.2.13 (Unix) mod_ssl/2.2.13 OpenSSL/0.9.8k DAV/2 PHP/5.2.11
X-Powered-By: PHP/5.2.11
Content-Length: 1
Connection: close
Content-Type: text/html
0
我需要識別的部分是0,也可以是1或2。
通過簡單地將引腳設置為HIGH,此函數將成為turnOnAppropriateLight()或其他內容,而不是printData()。 然后,這將激活繼電器,為相應的LED陣列供電。
現在我寫了這個,看起來我只需要保留最后一個字符並根據值進行切換。 *(數據++)是令人困惑的部分,即使我知道它正在遞增指針索引......我只是不確定如何直接轉到該索引中的最后一個字符。 不需要這個循環來吐出結果。
這並不強大,但是
Serial.print(data[len-1])
看看是什么讓你
這應該是你所需要的:
data[len - 1]
你可能是神經質的並解析每一行,或尋找最后的標簽: Content-Type :.
我會將C風格的字符串轉換為C ++ std::string
然后使用find_first
方法查找關鍵字。
std::istringstream
可用於將文本“0”轉換為數字0。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.