【发布时间】:2011-06-25 01:11:09
【问题描述】:
我正在编写一段 arduino 代码,该代码使用内置 wifi 的 BlackWidow 版本。使用 WiServer.h 库,我正在使用带有 mods 的 SimpleClient.pde 示例向网络服务器发送调用,该服务器将只需返回一个整数 - 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() 是调用网络服务器的回调,运行时它会向串口监视器发送以下内容(这是 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。
这个函数不是 printData(),而是变成 turnOnAppropriateLight() 之类的,只需将 pin 设置为 HIGH。这将激活一个继电器,为相应的 LED 阵列供电。
现在我已经写了这个,看起来我只需要保留最后一个字符并根据值进行切换。 *(data++) 是令人困惑的部分,即使我知道它正在增加指针索引......我只是不确定如何直接转到该索引中的最后一个字符。这个循环不需要吐出结果。
【问题讨论】:
-
您需要知道流中的字符何时可用还是需要解析流中的字符?