java - Arduino 以太网客户端发送 TCP 时出现问题

标签 java tcp arduino ethernet

我在尝试在 arduino 和 java 之间发送消息时遇到一些问题。 arduino有一个超声波传感器,我想将传感器检测到的距离发送到java。

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

#define TRIGGER_PIN  12  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     11  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress gateway(192,168,1, 1);
IPAddress subnet(255, 255, 0, 0);
IPAddress serverIP(192,168,1,3);
int serverPort=8887;
EthernetClient client;

int msg;

void setup() 
{  
 Serial.begin(115200); 

if (Ethernet.begin(mac) == 0) 
{
  Serial.println("Error con conexion DHCP");
  Serial.println("Pasamos a direccion estatica");
  IPAddress IP(192,168,1,177);
  Ethernet.begin(mac,IP,gateway,subnet);
}
 delay(1000);
 Serial.println("connecting to Server ...");

// if you get a connection to the Server
if (client.connect(serverIP, serverPort)) {
  Serial.println("Conectado");//report it to the Serial
  String msg="Hola, servidor";//Message to be sent
  Serial.println("sending Message:"+msg);//log to serial
  client.println(msg);//send the message
}
else {
 // if you didn't get a connection to the server:
 Serial.println("connection failed");
}
}
void loop()
{
delay(50);                    
unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
int msg=uS / US_ROUNDTRIP_CM;
Serial.print("Ping: ");
Serial.print(msg); 
Serial.println("cm");
client.print("Ping: ");
client.print(msg); 
client.println("cm");


if (client.available()) {
 char c = client.read();
 Serial.print(c);
}

// if the server's disconnected, stop the client:
if (!client.connected()) {
 Serial.println();//report it to the serial
 Serial.println("disconnected");
 client.stop();
}
}

这是java代码

public static void main(String[] args) {

    ServerSocket s; //Socket servidor
    Socket sc; //Socket cliente

    BufferedReader b; //Canal de Lectura

    String mensaje;

    try {
        //Creo el socket server
        s = new ServerSocket(8887);

        //Invoco el metodo accept del socket servidor, me devuelve una referencia al socket cliente
        sc = s.accept();

        //Obtengo una referencia a los canales de escritura y lectura del socket cliente
        b = new BufferedReader( new InputStreamReader ( sc.getInputStream() ) );


        while ( true ) {
            //Leo lo que escribio el socket cliente en el canal de lectura
            mensaje = b.readLine();
            System.out.println(mensaje);

            //Escribo en canal de escritura el mismo mensaje recibido

            if ( mensaje.equals("by")) { // this dont have sense now
                break;
            }
        }
        b.close();

        sc.close();
        s.close();
    } catch (IOException e) {
        System.out.println(e);
    }       

}

为什么我的java代码总是收到0CM的距离?

最佳答案

问题已解决。

在这里我找到了它不起作用的原因:Arduino Ethernet UDPSendReceive example disables all pin outputs?

我只更改了连接超声波的引脚:

#define TRIGGER_PIN  8  
#define ECHO_PIN     7

关于java - Arduino 以太网客户端发送 TCP 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20858814/

相关文章:

java - 如何获取 URI 的最后一个路径段

java - 数据提取 Tar 和 7z

c++ - DisableAddressSharing注册表不起作用

c++ - RGB 的奇怪输出

python - Arduino Yun Python 脚本

java - JUnit 测试中的假设与断言

java - 土耳其语语音识别

c - socket编程中如何正确拒绝客户端连接?

Java-如何根据时间对tcp服务器中接收到的数据进行排序?

c - 从 NTP 服务器获取毫秒