本帖最后由 布列松 于 2015-4-11 01:29 编辑
tsaiwn 发表于 2015-4-11 00:16 
要 hex 你就自己写 0x??
反正对计算机来说
以下三个完全一样:
这就是我现在的代码,第一段的:
unsigned char hexdata[8] = {0x01,0x05,0x00,0x10,0xFF,0x00,0x8D,0xFF};
实现起来有效的,
但第二段:
unsigned char yy[] = {0x01,0x05,0x00,0x10,0x00,0x00,0xCC,0xFF};
是无效的。
- #include <EtherCard.h>
- static byte mymac[] = {0x74,0x69,0x69,0x2D,0x30,0x31};
- static byte myip[] = {192,168,1,180};
- byte Ethernet::buffer[700];
- const int ledPin = 3;
- boolean ledStatus;
- char* on = "ON";
- char* off = "OFF";
- char* statusLabel;
- char* buttonLabel;
- unsigned char hexdata[8] = {0x01,0x05,0x00,0x10,0xFF,0x00,0x8D,0xFF};
- unsigned char yy[] = {0x01,0x05,0x00,0x10,0x00,0x00,0xCC,0xFF};
- void setup () {
-
- Serial.begin(9600);
- Serial.println("WebLed Demo");
-
- if (!ether.begin(sizeof Ethernet::buffer, mymac, 10))
- Serial.println( "Failed to access Ethernet controller");
- else
- Serial.println("Ethernet controller initialized");
-
- if (!ether.staticSetup(myip))
- Serial.println("Failed to set IP address");
- Serial.println();
-
- pinMode(ledPin, OUTPUT);
- digitalWrite(ledPin, LOW);
- ledStatus = false;
- }
-
- void loop() {
-
- word len = ether.packetReceive();
- word pos = ether.packetLoop(len);
-
- if(pos) {
-
- if(strstr((char *)Ethernet::buffer + pos, "GET /?status=ON") != 0) {
- Serial.write(hexdata, 8);
- ledStatus = true;
- }
- if(strstr((char *)Ethernet::buffer + pos, "GET /?status=OFF") != 0) {
- Serial.write(yy, sizeof( yy ));
- ledStatus = false;
- }
-
- if(ledStatus) {
- digitalWrite(ledPin, HIGH);
- statusLabel = on;
- buttonLabel = off;
- } else {
- digitalWrite(ledPin, LOW);
- statusLabel = off;
- buttonLabel = on;
- }
-
- BufferFiller bfill = ether.tcpOffset();
- bfill.emit_p(PSTR("HTTP/1.0 200 OK\r\n"
- "Content-Type: text/html\r\nPragma: no-cache\r\n\r\n"
- "<html><head><title>WebLed</title></head>"
- "<body>LED Status: $S "
- "<a href="/?status=$S"><input type="button" value="$S"></a>"
- "</body></html>"
- ), statusLabel, buttonLabel, buttonLabel);
- ether.httpServerReply(bfill.position());
- }
- }
复制代码 |