How to convert Python socket to Java? for Tello DJI drone
up vote
0
down vote
favorite
I am looking for a way to make the code that I wrote in Java work to soon develop an interface for the drone tello.
I have this java code:
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);
}
}
When I execute that just show a error 1 message with the bind, but I don't know if the rest of methods works.
The next code was written on python and works. It's officially from 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 ('rnrnTello Python3 Demo.rn')
print ('Tello: command takeoff land flip forward back left right rn up down cw ccw speed speed?rn')
print ('end -- quit demo.rn')
#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
I'm working in Ubuntu 18.10 if that it's important and the drone connects with the smartphone.
If someone can help me convert the python code to java I will be grateful.
java python sockets
add a comment |
up vote
0
down vote
favorite
I am looking for a way to make the code that I wrote in Java work to soon develop an interface for the drone tello.
I have this java code:
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);
}
}
When I execute that just show a error 1 message with the bind, but I don't know if the rest of methods works.
The next code was written on python and works. It's officially from 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 ('rnrnTello Python3 Demo.rn')
print ('Tello: command takeoff land flip forward back left right rn up down cw ccw speed speed?rn')
print ('end -- quit demo.rn')
#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
I'm working in Ubuntu 18.10 if that it's important and the drone connects with the smartphone.
If someone can help me convert the python code to java I will be grateful.
java python sockets
Whats the error message?
– Moritz Schmidt
Nov 8 at 6:02
Use one paramatere InetSocketAddress constructor. publicInetSocketAddress(int port)
creates a socket address where the IP address is the wildcard address and the port number a specified value.
– Zaboj Campula
Nov 8 at 6:40
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am looking for a way to make the code that I wrote in Java work to soon develop an interface for the drone tello.
I have this java code:
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);
}
}
When I execute that just show a error 1 message with the bind, but I don't know if the rest of methods works.
The next code was written on python and works. It's officially from 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 ('rnrnTello Python3 Demo.rn')
print ('Tello: command takeoff land flip forward back left right rn up down cw ccw speed speed?rn')
print ('end -- quit demo.rn')
#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
I'm working in Ubuntu 18.10 if that it's important and the drone connects with the smartphone.
If someone can help me convert the python code to java I will be grateful.
java python sockets
I am looking for a way to make the code that I wrote in Java work to soon develop an interface for the drone tello.
I have this java code:
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);
}
}
When I execute that just show a error 1 message with the bind, but I don't know if the rest of methods works.
The next code was written on python and works. It's officially from 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 ('rnrnTello Python3 Demo.rn')
print ('Tello: command takeoff land flip forward back left right rn up down cw ccw speed speed?rn')
print ('end -- quit demo.rn')
#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
I'm working in Ubuntu 18.10 if that it's important and the drone connects with the smartphone.
If someone can help me convert the python code to java I will be grateful.
java python sockets
java python sockets
edited Nov 15 at 6:25
tkausl
7,96912042
7,96912042
asked Nov 8 at 4:54
Luis Alberto Colin Macias
12
12
Whats the error message?
– Moritz Schmidt
Nov 8 at 6:02
Use one paramatere InetSocketAddress constructor. publicInetSocketAddress(int port)
creates a socket address where the IP address is the wildcard address and the port number a specified value.
– Zaboj Campula
Nov 8 at 6:40
add a comment |
Whats the error message?
– Moritz Schmidt
Nov 8 at 6:02
Use one paramatere InetSocketAddress constructor. publicInetSocketAddress(int port)
creates a socket address where the IP address is the wildcard address and the port number a specified value.
– Zaboj Campula
Nov 8 at 6:40
Whats the error message?
– Moritz Schmidt
Nov 8 at 6:02
Whats the error message?
– Moritz Schmidt
Nov 8 at 6:02
Use one paramatere InetSocketAddress constructor. public
InetSocketAddress(int port)
creates a socket address where the IP address is the wildcard address and the port number a specified value.– Zaboj Campula
Nov 8 at 6:40
Use one paramatere InetSocketAddress constructor. public
InetSocketAddress(int port)
creates a socket address where the IP address is the wildcard address and the port number a specified value.– Zaboj Campula
Nov 8 at 6:40
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53201758%2fhow-to-convert-python-socket-to-java-for-tello-dji-drone%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Whats the error message?
– Moritz Schmidt
Nov 8 at 6:02
Use one paramatere InetSocketAddress constructor. public
InetSocketAddress(int port)
creates a socket address where the IP address is the wildcard address and the port number a specified value.– Zaboj Campula
Nov 8 at 6:40