2016-11-08 53 views
0

我已经做了一个简单的软件来测量温度。我想要将XML发送到外部浏览器或其他软件。当我用浏览器连接到Arduino时,我得到了这张照片(如下)。什么我'做错了?:通过Arduino以太网套接字发送XML

enter image description here

的代码发送XML是:

EthernetClient client = server.available(); 
if (client) { 
Serial.println("new client"); 
// an http request ends with a blank line 
boolean currentLineIsBlank = true; 
while (client.connected()) { 
    if (client.available()) { 
    char c = client.read(); 
    Serial.write(c); 

    if (c == '\n' && currentLineIsBlank) { 
     client.println("HTTP/1.1 200 OK"); 
     client.println("Content-Type: text/xml;charset=UTF-8"); 
     client.println("Connection: close"); // the connection will be closed after completion of the response 
     client.println(); 
     client.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); 
     client.println("<TEMP>"); 

     client.print(calcTemp(cnt1), 3); 
     client.println("<TEMP/>"); 


     break; 
    } 
    if (c == '\n') { 
     // you're starting a new line 
     currentLineIsBlank = true; 
    } else if (c != '\r') { 
     // you've gotten a character on the current line 
     currentLineIsBlank = false; 
    } 
    } 
} 
// give the web browser time to receive the data 
delay(1); 
client.stop(); 

回答

1

已检测到错误的行4如下:

client.println("<TEMP/>"); 

关闭Xml标签的正确语法是:

client.println("</TEMP>"); 

注意:语法<TEMP/>用于声明一个空属性。它相当于<TEMP></TEMP>

+0

Arrgg ..谢谢:)我的错误...现在正在工作.. – Ferguson