vranki / ExtPlane

Plugin for X-Plane flight simulator which allows reading and writing simulation properties
138 stars 44 forks source link

set dataref issue with python (and telnet) #56

Closed crosay closed 4 years ago

crosay commented 4 years ago

Dear extplane developers, first thanks for this amazing work! It is really great to have a CLI available to read dataref and send commands to xplane. I am working on a DIY cockpit using adruino cards connected to a raspberry pi. But I have the following issue with the set <dataref) using a python script. Everything works file to connect to extplane to send command and to read dataref but the set seems to do nothing. Here is the code:

import socket
import struct
import time

PORT = 51000

BEACON = '239.255.1.1'
BCN_PORT = 49707
server_address = ('', BCN_PORT)

# Create the socket
mcs = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# Tell the operating system to add the socket to the multicast group
# on all interfaces.
group = socket.inet_aton(BEACON)
mreq = struct.pack('4sL', group, socket.INADDR_ANY)
mcs.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
# Bind to the server address
mcs.bind(server_address)
# Receive/respond loop
received = False
print ('waiting to receive XPLANE BEACON signal')

while (not received):
    data, address = mcs.recvfrom(4)
    print ('received %s bytes from %s' % (len(data), address))
    print (str(data))
    if (data == b'BECN'):
        received = True

client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
extplaneAddress = (address[0], PORT)
client.connect(extplaneAddress)
print ('connected to ' + HOST + ':' + str(PORT))
print ('Reception...')
data = client.recv(1024).decode("utf8", "strict")
print ('>', data)

client.send(b"cmd once sim/electrical/battery_1_on\n")
client.send(b"cmd once sim/systems/avionics_on\n")
client.send(b"sub sim/cockpit2/engine/actuators/mixture_ratio\n")
data = client.recv(1024).decode("utf8", "strict")
print ('>', data)
client.send(b"set sim/cockpit2/engine/actuators/mixture_ratio [50.0,0]\n")
client.send(b"sub sim/cockpit2/engine/actuators/mixture_ratio\n")
data = client.recv(1024).decode("utf8", "strict")
print ('>', data)
client.send(b"sub sim/aircraft/view/acf_descrip:string\n")
data = client.recv(1024).decode("utf8", "strict")
print ('>', data)
print ('disconnect')
client.close()

the result is the following:

%Run extplane.py waiting to receive XPLANE BEACON signal received 4 bytes from ('127.0.0.1', 49707) b'BECN' connected to 127.0.0.1:51000 Reception... EXTPLANE 1 EXTPLANE-VERSION 1002

ufa sim/cockpit2/engine/actuators/mixture_ratio [0,0,0,0,0,0,0,0]

ufa sim/cockpit2/engine/actuators/mixture_ratio [50,0,0,0,0,0,0,0]

ub sim/aircraft/view/acf_descrip:string "Beechcraft Baron B58"

disconnect

as one can see the client.send(b"set sim/cockpit2/engine/actuators/mixture_ratio [50.0,0]\n") did not change the value of the dataref which is writeable. Note: the "cmd once sim/electrical/battery_1_on" and "cmd once sim/systems/avionics_on" changed visually the position of the battery switch abs avionics master in the sim. Any clue? thank you for your answer

crosay commented 4 years ago

I have changed the command values to [0.5, 0.1] and the result are now correct: ufa sim/cockpit2/engine/actuators/mixture_ratio [0.5,0.1,0,0,0,0,0,0]