gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 02:41:05 27 4
gpt4 key购买 nike

我在尝试在 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com