dronefleet / mavlink

A Java API for MAVLink communication
MIT License
110 stars 67 forks source link

Connection refused:connect #66

Open mandar-bhide opened 3 years ago

mandar-bhide commented 3 years ago

My code is:

package mavlink;

import java.io.EOFException;
import java.io.IOException;
import java.net.Socket;

import io.dronefleet.mavlink.Mavlink2Message;
import io.dronefleet.mavlink.MavlinkConnection;
import io.dronefleet.mavlink.MavlinkMessage;

public class MainClass {
    private static void print(Object o) {
        System.out.println(o);
    }
    public static void main(String[] args) {
        try(Socket socket = new Socket("127.0.0.1", 5760)){
            MavlinkConnection connection = MavlinkConnection.create(socket.getInputStream(),socket.getOutputStream());
            MavlinkMessage message;
            while((message = connection.next())!=null) {
                if(message instanceof Mavlink2Message) {
                    print(message.getPayload());
                }
            }
        }catch(EOFException eof) {
            eof.printStackTrace();
        }catch(IOException ioe) {
            ioe.printStackTrace();
        }
    }
}

Terminal output is:

java.net.ConnectException: Connection refused: connect
    at java.base/sun.nio.ch.Net.connect0(Native Method)
    at java.base/sun.nio.ch.Net.connect(Net.java:503)
    at java.base/sun.nio.ch.Net.connect(Net.java:492)
    at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:588)
    at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:333)
    at java.base/java.net.Socket.connect(Socket.java:648)
    at java.base/java.net.Socket.connect(Socket.java:597)
    at java.base/java.net.Socket.<init>(Socket.java:520)
    at java.base/java.net.Socket.<init>(Socket.java:294)
    at mavlink.MainClass.main(MainClass.java:16)

I have connected APM 2.8.0 via USB to windows 10 with intel i7-8550u. I am looking forward to deploy the code on a raspberry pi. Actually I recieved similar error with dronekit python so shifted to Java. But stuck at same point. Thank you.

seanrowens commented 1 year ago

Based on your description, your computer is connected to your flight controller over a serial connection. But your code is trying to use a TCP socket.