如何将Python套接字转换为Java? Tello DJI无人机

时间:2018-11-08 04:54:40

标签: java python sockets

我正在寻找一种方法来使我用Java编写的代码能够正常工作,以便很快为无人机Tello开发一个接口。 我有这个Java代码:

package conexion;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Tello3 extends JFrame implements ActionListener, KeyListener, Runnable {
    private static final long serialVersionUID = 1L;

    private JLabel l1;
    private JButton b1;
    private JTextField tf1;

    public Tello3() {
        this.setLayout(null);
        this.setTitle("Prueba 3");

        //Panel - Panel
        JPanel p = new JPanel();
        p.setBounds(0, 0, 300, 150);
        p.setLayout(null);
        add(p);

        //Textfield - Campo de texto
        tf1 = new JTextField();
        tf1.setBounds(10, 10, 280, 20);
        tf1.addKeyListener(this);
        tf1.setFocusable(true);
        p.add(tf1);

        //Labels - Etiquetas
        l1 = new JLabel();
        l1.setBounds(10, 50, 280, 20);
        p.add(l1);

        //Buttons - Botones
        b1 = new JButton("Send");
        b1.setBounds(80, 80, 140, 20);
        b1.addActionListener(this);
        b1.setFocusable(false);
        p.add(b1);

        Thread t = new Thread(this);
        t.start();
    }

    public void Action() {
        String command = tf1.getText();
        try {
            Socket s = new Socket("192.168.10.1", 8889);
            DataOutputStream dos = new DataOutputStream(s.getOutputStream());
            dos.writeUTF(command);
            dos.close();
            if(command == "end") {
                s.close();
            }
        } catch (UnknownHostException e) {
            JOptionPane.showMessageDialog(null, e.getMessage(), "Error3", 1);
        } catch (IOException e) {
            JOptionPane.showMessageDialog(null, e.getMessage(), "Error4", 1);
        }
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        try {
            Socket s = new Socket();
            s.bind(new InetSocketAddress("", 9000));
            //s.connect(new InetSocketAddress("", 9000));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            JOptionPane.showMessageDialog(null, e.getMessage(), "Error1", 1);
        }
        try {
            ServerSocket ss = new ServerSocket(1518);
            while(true) {
                Socket s = ss.accept();
                DataInputStream dis = new DataInputStream(s.getInputStream());
                String returned = dis.readUTF();
                l1.setText(returned);
                dis.close();
                if(returned == "end") {
                    ss.close();
                }
            }
        } catch (IOException e1) {
            JOptionPane.showMessageDialog(null, e1.getMessage(), "Error2", 1);
        }
    }

    @Override
    public void keyPressed(KeyEvent arg0) {
        // TODO Auto-generated method stub
        if(arg0.getKeyCode() == 10) {
            Action();
        }
    }

    @Override
    public void keyReleased(KeyEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void keyTyped(KeyEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub
        Action();
    }

    public static void main(String[] arg0) {
        JFrame form_tello = new Tello3();
        form_tello.setSize(300, 150);
        form_tello.setLocationRelativeTo(null);
        form_tello.setResizable(false);
        form_tello.setVisible(true);
        form_tello.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

}

执行该命令时,仅显示一条带有绑定的错误1消息,但我不知道其余方法是否有效。 下一个代码是在python上编写的并且可以工作。这是DJI的官方消息。

#
# Tello Python3 Control Demo 
#
# http://www.ryzerobotics.com/
#
# 1/1/2018

import threading 
import socket
import sys
import time


host = ''
port = 9000
locaddr = (host,port) 


# Create a UDP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

tello_address = ('192.168.10.1', 8889)

sock.bind(locaddr)

def recv():
    count = 0
    while True: 
        try:
            data, server = sock.recvfrom(1518)
            print(data.decode(encoding="utf-8"))
        except Exception:
            print ('\nExit . . .\n')
            break


print ('\r\n\r\nTello Python3 Demo.\r\n')

print ('Tello: command takeoff land flip forward back left right \r\n       up down cw ccw speed speed?\r\n')

print ('end -- quit demo.\r\n')


#recvThread create
recvThread = threading.Thread(target=recv)
recvThread.start()

while True: 

    try:
        msg = raw_input();

        if not msg:
            break  

        if 'end' in msg:
            print ('...')
            sock.close()
            break
    if 'exit' in msg:
        break

        # Send data
        msg = msg.encode(encoding="utf-8") 
        sent = sock.sendto(msg, tello_address)
    except KeyboardInterrupt:
        print ('\n . . .\n')
        sock.close()  
        break

如果这很重要,并且无人机与智能手机连接,我正在使用Ubuntu 18.10。

如果有人可以帮助我将python代码转换为java,我将不胜感激。

0 个答案:

没有答案