Skip to content

getDREFs

Jason Watkins edited this page Mar 4, 2016 · 6 revisions

Gets the value of one or more X-Plane datarefs.

Syntax

Language Signature
C int getDREFs(XPCSocket sock, const char* drefs[], float* values[], unsigned char count, int sizes[])
MATLAB result = getDREFs( drefs, socket )
Java float[][] getDREFs(String[] drefs)
Python getDREFs(drefs)
Parameters

sock (C): The socket used to send the command.

msg: The string to display on the screen

drefs: The names of the datarefs to get.

values (C): A 2 dimensional array in which to store the result.

sizes (C): The allocated size of each array in values. When getDREFs returns, each item in sizes will be set to the number of items actually written to the respective array in values, which will be less than or equal to the initial value of that item in sizes.

socket (MATLAB): An optional reference to a socket opened with openUDP that should be used to send the command.

Return value

C: A negative value if an error occurs, otherwise 0.

All others: An array representing the dataref value.

Remarks

Dataref names and their associated data types can be found on the XPSDK wiki. The size of value should match the size given on that page. XPC currently sends all values as floats regardless of the type described on the wiki. This doesn't cause any data loss for most datarefs.

Exceptions

C Error Code Java Exception Python Error Description
-1 IOException OSError The client is unable to send the command
-2 IOException OSError The client is unable to read the response

Examples

C

Thanks to @TurboTiger for expanding this example.

#include "xplaneConnect.h"

XPCSocket sock = aopenUDP("127.0.0.1", 49007);

//We are using getDREFs() to request a float value, a float array of size 10, and another float.
const char* drefs[3] = {
	"sim/cockpit2/gauges/indicators/airspeed_kts_pilot", //indicated airspeed
	"sim/cockpit2/fuel/fuel_quantity",	//fuel quantity in each tank, a float array of size 10
	"sim/cockpit2/gauges/indicators/pitch_vacuum_deg_pilot" //pitch reading displayed on attitude indicator
};

//make sure to specify the same size as the number of elements you want to store,
// see: http://stackoverflow.com/questions/10051782/array-overflow-why-does-this-work
float* values[3];

//number of datarefs being requested.  NOTE: since unsigned char, must be in range [0,255],
unsigned char count = 3;
values[0] = (float*)malloc(1 * sizeof(float)); //see: http://www.cplusplus.com/reference/cstdlib/malloc/
values[1] = (float*)malloc(10 * sizeof(float)); //allocate a block of memory 10X larger than for a float since 10-element array
values[2] = (float*)malloc(1 * sizeof(float));

int sizes[3] = { 1, 10, 1 }; //allocated size of each item in "values"

if (getDREFs(xpcSocket, drefs, values, count, sizes) < 0)
{
	printf("An error occured."); //negative return value indicates an error
}
else
{
	printf("Airspeed = %f\n", values[0][0]); //Note the use of a 2D-array notation.
	printf("Fuel Qty (L) = %f\n", values[1][0]);
	printf("Fuel Qty (R) = %f\n", values[1][1]);
	printf("Indicated pitch = %f\n", values[2][0]);
}

closeUDP(sock);

MATLAB

import XPlaneConnect.*

DREFS = {'sim/cockpit/switches/gear_handle_status',...
         'sim/aircraft/parts/acf_gear_deploy'};

result = getDREFs(DREFS);
Java
import gov.nasa.xpc.XPlaneConnect;

try(XPlaneConnect xpc = new XPlaneConnect())
{
    String[] drefs ={"sim/cockpit/switches/gear_handle_status", "sim/aircraft/parts/acf_gear_deploy"};
    float[][] values = xpc.getDREFs(drefs);
    System.out.print("The gear handle status is ");
    System.out.println(values[0][0]);
}

Python

import xpc

with xpc.XPlaneConnect() as client:
    drefs = ["sim/cockpit/switches/gear_handle_status", "sim/aircraft/parts/acf_gear_deploy"]
    values = client.getDREFs(drefs)
    print "The gear handle status is " + str(values[0][0])