Reading the sim is useful. Changing it is where a home cockpit starts to feel like one.
X-Plane 12 accepts two kinds of message on port 49000 that matter here. A command does what pressing a key or a button does. A dataref write puts a number straight into a value.
Both in one script
import dgram from "node:dgram";
const host = "127.0.0.1";
const port = 49000;
const socket = dgram.createSocket("udp4");
const send = (packet) => new Promise((resolve) => socket.send(packet, port, host, resolve));
// "CMND", a zero byte, then the command name.
const command = (path) => send(Buffer.from(`CMND\0${path}\0`, "ascii"));
// "DREF", a zero byte, the value as a 4 byte float, then the dataref name padded to 500 bytes.
function dataref(path, value) {
const packet = Buffer.alloc(509);
packet.write("DREF\0", 0, "ascii");
packet.writeFloatLE(value, 5);
packet.write(path, 9, "ascii");
return send(packet);
}
await command("sim/lights/beacon_lights_toggle");
await dataref("sim/cockpit/autopilot/heading_mag", 270);
socket.close();
Save it as control.mjs, load a flight and run node control.mjs. The beacon should toggle and the heading bug should move to 270.
If the sim is on another PC, change host to its address.
