本帖最后由 Cimate 于 2016-5-20 14:07 编辑
我现在用MsTimer2实现每隔1秒通过TCP socket向服务器发送数据,arduino再开个TCP服务器端,当接收到客户端发送online?服务器就返回1234,我用电脑的TCP/UDP软件连接arduino时,每隔1秒发送online?这时会收到1234,但没过多久就收不到了。但,如果我把MsTimer2那段代码去掉,又很正常了
代码如下:
#include <SPI.h>
#include <Ethernet.h>
#include <MsTimer2.h>
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,177);
IPAddress Pserver(192,168,1,200);
EthernetClient client;
EthernetServer server(15000);
void heard()
{
client.println("OK1234");
}
void setup() {
Serial.begin(9600);
Ethernet.begin(mac, ip);
Serial.println(Ethernet.localIP());
server.begin();
if (client.connect(Pserver,16000)){
Serial.println("connected");
}
MsTimer2::set(10000, heard);
MsTimer2::start();
}
void loop() {
String msg="";
EthernetClient client = server.available();
if (client) {
while (client.connected()&&client.available()>0) {
char c = client.read();
msg+=c;
}
if(msg.equals("online?")){
server.print("1234");
}
}
}
|