检测 C++ 中的字符字符流
我正在编写一段 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
我需要的部分recognize 是 0,也可以是 1 或 2。
通过简单地将引脚设置为高电平,该函数将变为 TurnOnAppropriateLight() 或其他函数,而不是 printData()。然后,这将激活继电器,为相应的 LED 阵列供电。
现在我已经写好了,看起来我只需要保留最后一个字符并根据该值进行切换。 *(data++) 是令人困惑的部分,即使我知道它正在增加指针索引......我只是不确定如何直接转到该索引中的最后一个字符。不需要这个循环来输出结果。
I am working on a piece of arduino code that is using the BlackWidow version with wifi built in. Using the WiServer.h library, I'm using the SimpleClient.pde example with mods to send a call to a webserver that will simply return an integer - 0, 1, or 2. The end goal is to turn on a pin for the proper red, green, or yellow of a stoplight. The integers represent the aggregate state of our Hudson CI.
I'm a PHP lazy bastard, and pointers scare me. The code I am working with is
// 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() is the callback of the call to the webserver, and when run it sends the following to the serial monitor (this is 3 loops, no newline before new output):
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
The part that I need to identify is the 0, which could also be 1 or 2.
Instead of printData(), this function will become turnOnAppropriateLight() or something, by simply setting a pin to HIGH. This will then activate a relay, to power the corresponding LED array.
Now that I've written this up it looks like I just need to keep the last character around and do a switch based on the value. The *(data++) is the confusing part even though I know it's incrementing a pointer index...I'm just not sure how to go directly to the last char in that index. No need for this looping to spit out the result.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这根本不强大,但
看看这会给你带来什么
This is not robust AT ALL, but
See what that gets you
这应该就是您所需要的:
this should be all you need:
您可能会神经质并解析每一行,或者查找最后一个标签:Content-Type:。
我会将 C 样式字符串转换为 C++
std::string
,然后使用find_first
方法查找关键字。std::istringstream
可用于将文本“0”转换为数字 0。You could be neurotic and parse each line, or look for the last tags: Content-Type:.
I would convert the C-style string into a C++
std::string
then use thefind_first
method to look for the keywords.The
std::istringstream
can be used to convert from text "0" to numeric 0.