sockets - Arduino Ethernet Shield 连接到套接字服务器

标签 sockets tcp arduino ethernet

我正在使用 Arduino 的以太网屏蔽将其连接到套接字服务器(不同的计算机),以便我可以从它接收消息以激活一些例程。这是我的代码:

#include <Ethernet.h>
#include <SPI.h>

byte mac[] = { 0x90, 0xA2, 0xDA, 0x0F, 0x5A, 0x21 };
byte ip[] = { 192,168,1,11 }; //ip shield
byte server[] = { 192,168,1,7 }; // ip server

EthernetClient client;
String readString;

int ledPins[] = {19, 17, 2,3, 5, 6, 7, 8, 9};  // leds pins
int pinCount = 8;// number of leds
int const PINEYES = 9; //pin for different led
int const TIMERLEDS = 1000; 
int const TIMERTOOFF= 3000; 

//--------------------------------------------------------------------------

void setup() {
  turnOffLeds();
  Ethernet.begin(mac, ip);
  Serial.begin(9600);
  delay(1000);
  Serial.println("connecting...");
  if (client.connect(server, 1400)) {
    Serial.println("connected");
    client.println();
  } else {
    Serial.println("connection failed");
  }
  pinMode(PINEYES, OUTPUT);      
  int thisPin;
  for (int thisPin = 0; thisPin < pinCount; thisPin++)  {
    pinMode(ledPins[thisPin], OUTPUT);      
  }
}

//--------------------------------------------------------------------------    
void loop() {
  if (client.available()) {
    char c = client.read();

    if (readString.length() < 30) {
      if(c!='|')
        readString.concat(c);
      else {
        client.flush();
        //if (readString == "START_SENSATIONS") {
        if (readString == "on") {
          Serial.println("recebi");
          client.stop();
          turnOnMaya();
        }
        resetString();
      }
    }
    Serial.println(readString);
  }
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    for(;;)
      ;
  }
}

//--------------------------------------------------------------------------
void turnOnMaya(){
  turnOnLeds();
  for (int thisPin = 0; thisPin < pinCount; thisPin++) { 
     delay(TIMERLEDS);                  
     digitalWrite(ledPins[thisPin], LOW);    
  }
  turnOnEyes();
  delay(TIMERTOOFF);    
  turnOffLeds();
  digitalWrite(PINEYES, LOW);   
  client.connect(server, 1400);
}

//--------------------------------------------------------------------------
void turnOnLeds(){
  for (int thisPin = 0; thisPin < pinCount; thisPin++) { 
    digitalWrite(ledPins[thisPin], HIGH);   
  }
}

//--------------------------------------------------------------------------
void turnOffLeds(){
  for (int thisPin = 0; thisPin < pinCount; thisPin++) { 
    digitalWrite(ledPins[thisPin], LOW);   
  }
}

//--------------------------------------------------------------------------
void turnOnEyes(){
  digitalWrite(PINEYES, 255);
}

//--------------------------------------------------------------------------
void resetString() {
  readString = "";
}

问题是,当我的服务器停止或暂时不可用时,我需要我的 Arduino 继续尝试连接它,直到它再次可用。但我无法完成这项工作。 我试过这个:

  while(!client.available()){
    Serial.println("connection failed, trying again...");
    client.connect(server, 1400);
    delay(1000);
  }

但它不起作用。它只是永远打印 “连接失败,重试...”。我怎样才能做到这一点? 谢谢!

最佳答案

我假设您 PC 中的服务器是正常的 javac (或任何其他标准的 TCP 服务器)

但是你的arduino客户端没有指定是TCP。因此,要么更改您的服务器或客户端(如 here - 这使用 wifi 连接)。 如果你的服务器是在 java 中,它可能是这样的:

int port=9999;
    try{
        System.out.println("Starting server...");
        ServerSocket ss=new ServerSocket(port);
        Socket clientSocket=ss.accept();
        System.out.println("Connection has been established...");
        PrintWriter out=new PrintWriter(clientSocket.getOutputStream(),true);
        BufferedReader br=new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        String inputLine;
        System.out.println("Listening.....");
        while((inputLine=br.readLine())!=null)
            System.out.println(inputLine);
    }catch(Exception e){System.out.println(e.getMessage());}

关于sockets - Arduino Ethernet Shield 连接到套接字服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25450106/

相关文章:

xcode - 我正在使用 Xcode 快速编写一个基本的命令行(UDP)服务器/监听器。 #GCDAsyncUdpSocket 不会调用我的任何代表

c++ - 我如何知道请求是否完全使用 TCP 套接字接收?

c++ - Arduino - 将拆分字符串与另一个字符串进行比较

c - 未定义对 crcsum 的引用(unsigned char const*,unsigned long,unsigned short)

用于发送数据的 PHP Ratchet 套接字客户端

c# - TcpClient 创建似乎很慢。我可以缓存那些吗?

JavaEE面向对象网络

c# - 网络连接仅在设备通过电缆连接时有效

c - recv() 返回零但看不到任何 TCP 断开连接

c++ - arduino count++ 有限制吗?如何解决?