Board index FlightGear Support Interfacing

Bidirectional UDP communication

Connecting two computers, using generic protocol, connecting with Matlab?

Bidirectional UDP communication

Postby Niklasm » Thu Aug 18, 2016 2:26 am

Hi everyone! This is my first post.

Im trying to get bi-directional UDP to work so that I can get flightgear talking to a python script I've written. I've been able to get UDP communicating properly using output only, but as soon as I switch to bidirectional protocol, I stop receiving any data. Does UDP support bidirectional protocol? Do I need to send some sort of handshake? I've tried simply sending some data back to FG before receiving, however, that just ends up with the sent data being bounced back at me. Im using the following command line arguments:

--generic=socket,out,5,localhost,5500,udp,FightData (Works!)
--generic=socket,bi,5,localhost,5500,udp,FightData (Doesn't Work)

And here's the python script I'm working with (which very well may be the problem):
Code (): Select all
import socket

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(('localhost',5500))
print('connected to ',sock)

while True:
data = sock.recv(1024)
print(data)


any ideas or help is really appreciated!
Niklasm
 
Posts: 2
Joined: Thu Aug 18, 2016 2:11 am

Re: Bidirectional UDP communication

Postby Niklasm » Thu Aug 18, 2016 3:05 am

Hi everyone!

I managed to solve my own problem thanks to this excellent article:
http://diydrones.com/profiles/blogs/clo ... utopilot-1

Anyway, the basic solution was to use two different UDP ports, one for input, and the other for output.
The command line arguments ended up being:

--httpd=5500 <<dont forget this
--generic=socket,out,5,localhost,5600,udp,FightOut
--generic=socket,in,5,localhost,5500,udp,FightIn

and my python test code was:

Code (): Select all
import socket
import time

sockOut = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sockIn = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sockOut.bind(('',5500))
sockIn.bind(('localhost',5600))
print('connected to ',sockOut)
print('connected to ',sockIn)
timeInit = time.clock()

sockOut.connect(('localhost',5500))

while True:
data = sockIn.recv(1024)
if timeInit - time.clock() > 30:
sockOut.send('0.0,1.0\n'.encode())
else:
sockOut.send('1.0,0.0\n'.encode())
print(data.decode())


If anyone has a solution to the original problem of getting bidirectional communication to work on a single port, I'd still like to hear it, but this seems to work for now.
Niklasm
 
Posts: 2
Joined: Thu Aug 18, 2016 2:11 am

Re: Bidirectional UDP communication

Postby Fabyflight86 » Tue Aug 23, 2016 12:29 pm

Ciao a tutti? E' il mio primo post.
Volevo segnalare dei bug a riguardo la versione ultima 2.1, i salvataggi durante i volo come seleziono la cartella di destinazione per il salvataggio si blocca e non mi resta che premere esc altrimenti rimane bloccato. Anche per la mappa del gioco ad un certo punto si inchioda e non mi fa visualizzare vor,traffico i nomi delle piste
senza avere un orientamneto, cosi come quando apro la plancia della radio per comunicare con la torre di controllo ad un certo punto non appare quando premo con il tasto - .
Aiuto!!!
Fabyflight86
 
Posts: 1
Joined: Tue Aug 23, 2016 12:01 pm

Re: Bidirectional UDP communication

Postby ludomotico » Tue Aug 23, 2016 12:49 pm

Niklasm wrote in Thu Aug 18, 2016 3:05 am:getting bidirectional communication to work on a single port, I'd still like to hear it, but this seems to work for now.


You cannot have two applications in the same PC listening to the same port: only one application can listen to a port at the same time. If you run your Python script and it binds to port 5500, any other application running on the same computer (such as FlightGear) cannot listen to the port 5500 until you stop your script.

You must either run your Python script on another computer to use the same port or, as you did, use different ports for each way of the communication.
User avatar
ludomotico
 
Posts: 1269
Joined: Tue Apr 24, 2012 2:01 pm
Version: nightly
OS: Windows 10


Return to Interfacing

Who is online

Users browsing this forum: No registered users and 4 guests