Arduino以太网盾+ Java套接字连接

时间:2017-06-01 08:03:37

标签: java sockets arduino

我的arduino上安装了一个简单的EthernetServer示例。我已经设置了IP地址和MAC地址。我可以从我的PC上ping Arduino但我不能从一个简单的Java程序发送任何数据。

以下是Arduino的来源:

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };  
byte ip[] = { 172, 16, 201, 218 };    


EthernetServer server = EthernetServer(8080);

void setup()
{
  Serial.begin(9600);
  Ethernet.begin(mac, ip);

  server.begin();
  Serial.print("My IP address: ");
  Serial.println(Ethernet.localIP());
}

void loop()
{
  // if an incoming client connects, there will be bytes available to read:
  EthernetClient client = server.available();
  if (client == true) {
    // read bytes from the incoming client and write them back
    // to any clients connected to the server:
    server.write(client.read());
  }
}

当我运行代码时,我得到: enter image description here

我可以从命令提示符ping Arduino:

C:\WINDOWS\system32>ping 172.16.201.218

Pinging 172.16.201.218 with 32 bytes of data:
Reply from 172.16.201.218: bytes=32 time<1ms TTL=128
Reply from 172.16.201.218: bytes=32 time<1ms TTL=128
Reply from 172.16.201.218: bytes=32 time<1ms TTL=128
Reply from 172.16.201.218: bytes=32 time<1ms TTL=128

Ping statistics for 172.16.201.218:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum = 0ms, Average = 0ms

C:\WINDOWS\system32>

Java中的客户端是:

package ardsocket;

import java.io.*;
import java.net.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ArdSocket  {

   public static void main(String args[]) throws IOException {
        final String host = "172.16.201.218";
        final int portNumber = 8080;
        System.out.println("Creating socket to '" + host + "' on port " + portNumber);

        while (true) {
            Socket socket = new Socket(host, portNumber);
            BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

            System.out.println("server says:" + br.readLine());

            BufferedReader userInputBR = new BufferedReader(new InputStreamReader(System.in));
            String userInput = userInputBR.readLine();

            out.println(userInput);

            System.out.println("server says:" + br.readLine());

            if ("exit".equalsIgnoreCase(userInput)) {
                socket.close();
                break;
            }
        }
    }

}

当我运行Java源代码时,没有任何事情发生.. 此外,如果我尝试通过IP和端口8080连接Telnet是相同的故事。

我做错了什么?

2 个答案:

答案 0 :(得分:1)

希望我的代码Java有用!

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.InetAddress;
    import java.net.Socket;

public class GateControl {
    public static void main(String []args) {
        try {
            // "192.168.1.177" is a IP of Server
            Socket s = new Socket("192.168.1.177", 80); 
            InetAddress add = s.getInetAddress();
            System.out.println("Connected to " + add);

            PrintWriter pw = new PrintWriter(s.getOutputStream());
            // "?butonon" is a content which you send to server
            pw.println("GET /?buttonon HTTP/1.1");
            pw.println("");
            pw.println("");
            pw.flush();
            System.out.println("Request sent");

            BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
            System.out.println(br.readLine());
        }
        catch (IOException e) {
            e.printStackTrace();
        }

    }
}

答案 1 :(得分:0)

将日志添加到Arduino Sketch for Debug Purposes

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };  
byte ip[] = { 172, 16, 201, 218 };    


EthernetServer server = EthernetServer(8080);

void setup()
{
  Serial.begin(9600);
  Ethernet.begin(mac, ip);

  server.begin();
  Serial.print("My IP address: ");
  Serial.println(Ethernet.localIP());
}

void loop()
{
  // if an incoming client connects, there will be bytes available to read:
  EthernetClient client = server.available();
  if (client == true) {
    // Client Connected
    Serial.println("Client Connected.");
    server.write(client.read());
  }
}

在java客户端尝试将out.flush();放在out.println(userInput);之后,我认为您不需要在循环内创建SocketBufferedReaderPrintWriter 。你可以将它们移到顶部。

package ardsocket;

import java.io.*;
import java.net.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ArdSocket  {

   public static void main(String args[]) throws IOException {
        final String host = "172.16.201.218";
        final int portNumber = 8080;
        System.out.println("Creating socket to '" + host + "' on port " + portNumber);

        Socket socket = new Socket(host, portNumber);
        BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

        BufferedReader userInputBR = new BufferedReader(new InputStreamReader(System.in));

        while (true) {
            System.out.println("server says:" + br.readLine());

            String userInput = userInputBR.readLine();

            out.println(userInput);
            out.flush();

            System.out.println("server says:" + br.readLine());

            if ("exit".equalsIgnoreCase(userInput)) {
                socket.close();
                break;
            }
        }
    }

}