检测 C++ 中的字符字符流

发布于 2024-10-17 05:49:28 字数 1718 浏览 1 评论 0原文

我正在编写一段 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++) 是令人困惑的部分,即使我知道它正在增加指针索引......我只是不确定如何直接转到该索引中的最后一个字符。不需要这个循环来输出结果。

arduino 和 4-relay board 在此处输入图像描述

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.

arduino and 4-relay board
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

神也荒唐 2024-10-24 05:49:28

这根本不强大,但

Serial.print(data[len-1])

看看这会给你带来什么

This is not robust AT ALL, but

Serial.print(data[len-1])

See what that gets you

愁以何悠 2024-10-24 05:49:28

这应该就是您所需要的:

data[len - 1]

this should be all you need:

data[len - 1]
此刻的回忆 2024-10-24 05:49:28

您可能会神经质并解析每一行,或者查找最后一个标签: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 the find_first method to look for the keywords.

The std::istringstream can be used to convert from text "0" to numeric 0.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文