Skip to content

sendDREFs

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

Sets the specified datarefs to the specified values.

Syntax

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

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

drefs: The names of the datarefs to set.

values: A multidimensional array of values representing the data to set.

sizes (C): The number of items in each row of values.

count (C): The number of datarefs being sent.

Return value

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

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 Error ValueError The length of dref is greater than 255 characters
-2 Error ValueError There are more than 255 items in value
-3 IOException OSError The client is unable to send the command

Examples

C
#include "xplaneConnect.h"

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

// Set the status of the gear handle
const char* drefs[] = {"sim/cockpit/switches/gear_handle_status", "sim/aircraft/parts/acf_gear_deploy"};
float* values[2];
for(int i = 0; i < 2; ++i)
{
	values[i] = (float*)malloc(4);
}
values[0][0] = 1.0F;
values[1][0] = 1.0F;
int sizes[] = {1, 1};
sendDREFs(sock, drefs, values, sizes, 2);

closeUDP(sock);

Thanks to @TurboTiger for the following expanded C++ example:

#include "xplaneConnect.h"

const char* XPlane_IP = "127.0.0.1"; //IP address of PC running X-Plane
XPCSocket sock = openUDP(XPlane_IP);

int err;    //holds return code
const char* drefs[] = { "sim/cockpit2/switches/landing_lights_on", "sim/cockpit2/switches/beacon_on" };
float* values[2]; //A multidimensional array of values representing the data to set.

for (int i = 0; i < 2; ++i)
{
    values[i] = (float*)malloc(4);
}
//The following may be used in place of the FOR loop above if desired
//values[0] = (float*)malloc(1 * sizeof(float));
//values[1] = (float*)malloc(1 * sizeof(float));

//Note: with XPC, everything is sent as floats, even if X-Plane wants an int.
values[0][0] = 0.0F; //turn off landing lights (1.0F = ON)
values[1][0] = 0.0F; //turn off beacon (1.0F = ON)
int sizes[] = { 1, 1 }; // The number of items in each row of "values"
int count = 2; //The number of datarefs being sent

err = sendDREFs(sock, drefs, values, sizes, count);

switch (err)  //interpret return value (error code)
{
case -1:
    std::cout << "\nERROR: Length of dref is greater than 255 characters." << "\n";
    break;
case -2:
    std::cout << "\nERROR: There are more than 255 items in \"value\"." << "\n";
    break;
case -3:
    std::cout << "\nERROR: XPC client unable to send command." << "\n";
    break;
case 0:
    std::cout << "\nSUCCESS: XPC sent the specified datarefs." << "\n\n";
}

MATLAB

import XPlaneConnect.*

drefs = {'sim/cockpit/switches/gear_handle_status', 'sim/aircraft/parts/acf_gear_deploy'};
values = [0.0; 0.0];

sendDREFs(drefs, values);
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 = {{0.0F}, {0.0F};
    xpc.sendDREFs(drefs, values);
}

Python

import xpc

with xpc.XPlaneConnect() as client:
    drefs = ["sim/cockpit/switches/gear_handle_status", "sim/aircraft/parts/acf_gear_deploy"]
    values = [[0.0F], [0.0F]]
    client.sendDREFs(drefs, values)