私は単純なEthernetServerの例をarduinoにインストールしました。私はセットアップIPアドレスとMACアドレスを持っています。私はPCからArduinoにpingできますが、単純なJavaプログラムからデータを送信することはできません。ここでArduinoイーサネットシールド+ Javaソケット接続
はアルドゥイーノからのソースです:
#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());
}
}
を、私は、コマンドプロンプトからのArduinoにpingを実行することができます
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上のTelnetとポート8080に接続しようとすると同じ話です。
私は間違って何をしていますか?
Shazinありがとう、私はあなたのコードをコピーしましたが、まだ問題は残ります。またarduinoにLANケーブルで私のPCだけを接続しようとしている。私はそれをpingすることができますが、Arduinoとの間でデータを送受信することはできません。 – Ferguson