Board index FlightGear Support Interfacing

Sending data to Flightgear via UDP and Generic Protocol

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

Sending data to Flightgear via UDP and Generic Protocol

Postby Autowings » Thu Mar 05, 2020 12:40 pm

Hi Everyone,

I try to establish a data exchange between Flightgear and a Python application through UDP sockets. Data transfer from Flightgear to the Python program is already working perfectly.

For data transfer from Python to Flightgear, I start FG using AP_I.xml as input protocol file with
Code: Select all
fgfs --generic = socket, in, 10, localhost, 52867, udp, AP_I


In Python, I use the following class member code to establish a connection:

Code: Select all
         try:
            self.Iface = socket.socket(socket.AF_INET, socket.SOCK_DGRAM);
            print("Socket created.");
         except socket.error as msg :
            print('Failed to create socket. Error Code : ' + str(msg);
            sys.exit();
         
         try:
            self.Iface.bind((self.Host, self.Port));
         except socket.error as msg:
            print('Bind failed. Error Code : ' + str(msg));
            sys.exit();
         print("Socket binding complete.");


Whenever I try to establish the connection, I get an error message on the call to Iface.bind():

"Bind failed. Error Code: [Error 98]: Address already in use."

I get this message whenever I try to open a socket on the FG "in" port, no matter, which port number I use.
I examined the port numbers with netstat, but this does not raise any conflicts. The port numbers are definitely not in use.

What am I doing wrong? And why does none of the many descriptions of the generic protocol, you can find in the internet, give an example for the "input" direction? It would be helpful to get a functional example of how to send data to Flightgear from external applications.


Thank you in advance.

Autowings
Autowings
 
Posts: 11
Joined: Thu Oct 31, 2019 8:09 am

Re: Sending data to Flightgear via UDP and Generic Protocol

Postby SurferTim » Thu Mar 05, 2020 1:06 pm

I presume you are using this on the same computer as FG, since you are using localhost.
I can't see in your code what the self.Port value is.

You can't bind two applications to the same ip/port. The second attempt will fail.

Edit: Ensure that the port you use in your "--generic" statement in FG is different than self.Port.
As a suggestion, bind to (self.Port + 1) and send packets to self.Port.
Fly low, fly slow, land on a dime. Twin Otter DHC6-300+. https://github.com/SurferTim/dhc6p
PirateAir videos at https://www.youtube.com/user/SurferTim850
User avatar
SurferTim
 
Posts: 949
Joined: Sun Dec 09, 2018 5:49 pm
Location: Miramar Beach, FL
Callsign: Pirate
Version: 2019.2.0
OS: Ubuntu 18.04

Re: Sending data to Flightgear via UDP and Generic Protocol

Postby Autowings » Tue Mar 10, 2020 7:20 pm

Hi Surfer Tim,

Thank you for your reply. Physically, I have the following configuration:

In Flightgear:
Code: Select all
  fgfs --generic = socket, out, 10, localhost, 52866, udp, AP_O
            --generic = socket, in, 10, localhost, 52867, udp, AP_I


In my Python program, I configured like this:
Code: Select all
  For Port 52866 (FG out, Python in), I configured a client socket, binding to Port 52866 on localhost.
    For Port 52867 (FG in, Python out), I configured a server socket, listening on Port 52867.

The communication works perfectly, if I don't try to call socket.bind() on the server socket.

Now, I try to establish a second pair of connections between two Python applications on Ports 52868 and 52869.
Application A opens 52868 as a client socket and 52869 as a server socket, exactly the same way as at the FG connection.
At application B, I exchanged the port numbers for client (now 52869) and server (now 52868), in order to make the server at application A listen to the client at application A and vice versa..

But anyway, now I get the same error again, no matter which port number is assigned to which socket..

I think the problem is not a big one, but there seems to be something about the behaviour of the socket lib I don't understand.

Any help will be appreciated for some light in the darkness. :-)

Autowings
Autowings
 
Posts: 11
Joined: Thu Oct 31, 2019 8:09 am

Re: Sending data to Flightgear via UDP and Generic Protocol

Postby SurferTim » Wed Mar 11, 2020 8:29 am

Code: Select all
--generic = socket, in, 10, localhost, 52867, udp, AP_I

Then...
For Port 52867 (FG in, Python out), I configured a server socket, listening on Port 52867.

What port or ports are you binding to in your Python script? FG is binding to port 52867, and listening on that port for your Python packets. Any other attempt to bind to that port will fail.
You can send packets to port 52867, but you can't bind to it.
One Python port should bind to port 52866 so FG can send packets to Python.

My suggestion is to bind to port 52868 (self.Port + 1) , and send packets to 52867 (self.Port).
Or if you send and receive on the same port in Python (I don't), bind to port 52866.
Fly low, fly slow, land on a dime. Twin Otter DHC6-300+. https://github.com/SurferTim/dhc6p
PirateAir videos at https://www.youtube.com/user/SurferTim850
User avatar
SurferTim
 
Posts: 949
Joined: Sun Dec 09, 2018 5:49 pm
Location: Miramar Beach, FL
Callsign: Pirate
Version: 2019.2.0
OS: Ubuntu 18.04

Re: Sending data to Flightgear via UDP and Generic Protocol

Postby Autowings » Wed Mar 11, 2020 8:52 am

Hi again,

it seems this issue is a bit difficult to explain. Definitely I don't use any of the ports more than once.
Ports 52866 and 52867 are set for the communication with Flightgear - this works without problems. 52866 is receiving packets from FG, 52867 is sending to it.

Besides the Flightgear connection, I need to establish two more connections in my Python app - each of them including one listening port and one client port.
These additional interfaces are needed for command transfer between the Python application and a monitoring application which may but does not need to run on a different machine.
At the moment, all three applications (Flightgear, Python app and the monitoring software (also a Python app) are running on my localhost for developing reasons.

Communication between the two Python apps is done via ports 52868 and 52869. Python app A tries to bind to (localhost, 52868) for packet receive and listens on 52869 for packet send.
Python app B (the monitoring program) tries to bind to (localhost, 52869) for packet receive and listens on 52868 for packet send.

Although each of the ports is used for binding only once, I get this error 98 (address already in use) as soon as the second one of the Python apps tries to call socket.bind(). This is a thing, I don't understand, because each address/port combination is definitely used only once.

Regards
Autowings
Autowings
 
Posts: 11
Joined: Thu Oct 31, 2019 8:09 am

Re: Sending data to Flightgear via UDP and Generic Protocol

Postby SurferTim » Wed Mar 11, 2020 8:58 am

That error message is sent only if you try to bind (not send, but bind) to a port that is already in use or unavailable.
Try different ports. I use ports from 5000 to 5010. They seem to work on both Windows and Linux.
You are stuck where you are until you can get those ports to bind successfully. Get them to bind first.

Edit: try running your Python script without starting FG. Does it bind ok?
If so, you are attempting to use a port that FG is using.
If not, then that port is either unavailable or in use by another application.
Fly low, fly slow, land on a dime. Twin Otter DHC6-300+. https://github.com/SurferTim/dhc6p
PirateAir videos at https://www.youtube.com/user/SurferTim850
User avatar
SurferTim
 
Posts: 949
Joined: Sun Dec 09, 2018 5:49 pm
Location: Miramar Beach, FL
Callsign: Pirate
Version: 2019.2.0
OS: Ubuntu 18.04

Re: Sending data to Flightgear via UDP and Generic Protocol

Postby Autowings » Wed Mar 11, 2020 9:59 am

At last, it's working now.

Exchange of the two ports in the monitor application had failed because the bind command which resides in a sub module class was called previously. Thus, both apps tried to bind the same host/port combination. :oops:
Just like a former colleague of mine used to say: "As soon you do things right, they will work."

Thank you for your support. I can continue now. :)
Autowings
 
Posts: 11
Joined: Thu Oct 31, 2019 8:09 am

Re: Sending data to Flightgear via UDP and Generic Protocol

Postby SurferTim » Wed Mar 11, 2020 10:01 am

Good deal! Your fun has just started! Need more help? You know where to find it. :D
Fly low, fly slow, land on a dime. Twin Otter DHC6-300+. https://github.com/SurferTim/dhc6p
PirateAir videos at https://www.youtube.com/user/SurferTim850
User avatar
SurferTim
 
Posts: 949
Joined: Sun Dec 09, 2018 5:49 pm
Location: Miramar Beach, FL
Callsign: Pirate
Version: 2019.2.0
OS: Ubuntu 18.04


Return to Interfacing

Who is online

Users browsing this forum: No registered users and 1 guest