帮我用我的笔记本电脑连接我的arduino以太网盾 [英] Help me connect my arduino ethernet shield with my laptop using java
本文介绍了帮我用我的笔记本电脑连接我的arduino以太网盾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试连接我的arduino以太网盾和java应用程序进行通信。我无法实现任何人都能提供的帮助。
这是我的java代码
/ *
*要更改此许可证标题,请在项目属性中选择许可标题。
*要更改此模板文件,请选择工具|模板
*并在编辑器中打开模板。
* /
包客户;
import java.io. *;
import java.net。*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
/ **
*
* @author amanuel Negash
* /
公共类客户扩展申请{
// IO流
DataOutputStream toServer = null;
DataInputStream fromServer = null;
@Override //覆盖Application类中的start方法
public void start(Stage primaryStage){
//面板p保存标签和文本字段
BorderPane paneForTextField = new BorderPane();
paneForTextField.setPadding(new Insets(5,5,5,5));
paneForTextField.setStyle( - fx-border-color:green);
paneForTextField.setLeft(new Label(输入半径:));
TextField tf = new TextField();
tf.setAlignment(Pos.BOTTOM_RIGHT);
paneForTextField.setCenter(tf);
BorderPane mainPane = new BorderPane();
//显示内容的文本区域
TextArea ta = new TextArea();
mainPane.setCenter(new ScrollPane(ta));
mainPane.setTop(paneForTextField);
//创建一个场景并将其放置在舞台上
场景场景=新场景(mainPane,450,200);
primaryStage.setTitle(Client); //设置舞台标题
primaryStage.setScene(scene); //将场景放在舞台
primaryStage.show(); //显示阶段
tf.setOnAction(e - > {
try {
//从文本字段获取半径
double radius = Double。 parseDouble(tf.getText()。trim());
//将半径发送到服务器
toServer.writeDouble(radius);
toServer.flush();
//从服务器获取区域
double area = fromServer.readDouble();
//显示到文本区域
ta.appendText (Radius is+ radius +\ n);
ta.appendText(从服务器收到的区域是
+区域+'\ n');
}
catch(IOException ex){
System.err.println(ex);
}
});
try {
SocketAddress sockaddr = new InetSocketAddress(172.17.31.47,59637);
//创建套接字
套接字套接字= new Socket();
//连接10秒超时
socket.connect(sockaddr,999999999);;
//创建一个输入流以从服务器接收数据
fromServer = new DataInputStream(socket.getInputStream());
//创建输出流以将数据发送到服务器
toServer = new DataOutputStream(socket.getOutputStream());
socket.close();
}
catch(IOException ex){
ta.appendText(ex.toString()+'\ n');
}
}
}
********************************************* ************************************************** ************************************************** ***********************
以下是我的etherent arduino代码
#include< ethernet.h>
#include< spi.h>
byte mac [] = {0x90,0xf2,0xda,0x0e,0x98,0x34};
EthernetClient客户端;
void setup(){
Serial.begin(9600);
Ethernet.begin(mac);
server.begin();
延迟(1000);
Serial.print(server is at);
Serial.println(Ethernet.localIP());
server.begin();
}
void loop(){
//Serial.println (\"loop);
client = server.available();
char来电;
if(client)
{
Serial.println(Client connected);
while(client.connected()){
if(client.available()){
incoming = client.read();
Serial.print(传入);
client.print(传入);
}
}
Serial.println(客户端断开连接);
}
client.stop();
}
我尝试过:
我已禁用防火墙,我可以ping服务器。
解决方案
没有真正发生的事情,java客户端结束时说错误:java.net.SocketTimeoutException:接收超时
I am trying to connect my arduino ethernet shield and a java application to communicate. I could not achieve that any one can help. this is my java code /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package client; import java.io.*; import java.net.*; import java.util.logging.Level; import java.util.logging.Logger; import javafx.application.Application; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.control.ScrollPane; import javafx.scene.control.TextArea; import javafx.scene.control.TextField; import javafx.scene.layout.BorderPane; import javafx.stage.Stage; /** * * @author amanuel Negash */ public class Client extends Application { // IO streams DataOutputStream toServer = null; DataInputStream fromServer = null; @Override // Override the start method in the Application class public void start(Stage primaryStage) { // Panel p to hold the label and text field BorderPane paneForTextField = new BorderPane(); paneForTextField.setPadding(new Insets(5, 5, 5, 5)); paneForTextField.setStyle("-fx-border-color: green"); paneForTextField.setLeft(new Label("Enter a radius: ")); TextField tf = new TextField(); tf.setAlignment(Pos.BOTTOM_RIGHT); paneForTextField.setCenter(tf); BorderPane mainPane = new BorderPane(); // Text area to display contents TextArea ta = new TextArea(); mainPane.setCenter(new ScrollPane(ta)); mainPane.setTop(paneForTextField); // Create a scene and place it in the stage Scene scene = new Scene(mainPane, 450, 200); primaryStage.setTitle("Client"); // Set the stage title primaryStage.setScene(scene); // Place the scene in the stage primaryStage.show(); // Display the stage tf.setOnAction(e -> { try { // Get the radius from the text field double radius = Double.parseDouble(tf.getText().trim()); // Send the radius to the server toServer.writeDouble(radius); toServer.flush(); // Get area from the server double area = fromServer.readDouble(); // Display to the text area ta.appendText("Radius is " + radius + "\n"); ta.appendText("Area received from the server is " + area + '\n'); } catch (IOException ex) { System.err.println(ex); } }); try { SocketAddress sockaddr = new InetSocketAddress("172.17.31.47", 59637); // Create your socket Socket socket = new Socket(); // Connect with 10 s timeout socket.connect(sockaddr, 999999999);; // Create an input stream to receive data from the server fromServer = new DataInputStream(socket.getInputStream()); // Create an output stream to send data to the server toServer = new DataOutputStream(socket.getOutputStream()); socket.close(); } catch (IOException ex) { ta.appendText(ex.toString() + '\n'); } } } **************************************************************************************************************************************************************************** the following is my etherent arduino code #include <ethernet.h> #include <spi.h> byte mac[] = {0x90, 0xf2, 0xda, 0x0e, 0x98, 0x34 }; EthernetClient client; void setup() { Serial.begin(9600); Ethernet.begin(mac); server.begin(); delay(1000); Serial.print("server is at "); Serial.println(Ethernet.localIP()); server.begin(); } void loop() { //Serial.println("loop"); client = server.available(); char incoming; if (client) { Serial.println("Client connected"); while(client.connected()) { if (client.available()) { incoming = client.read(); Serial.print(incoming); client.print(incoming); } } Serial.println("Client disconnected"); } client.stop(); } What I have tried: I have disabled the firewall and I can ping the server.
解决方案
nothing really happens the java client ends saying theError:java.net.SocketTimeoutException: Receive timed out
这篇关于帮我用我的笔记本电脑连接我的arduino以太网盾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文