
jSimConnect is a java implementation of a simconnect client library.
SimConnect is a client/server architecture that enables add-ons developpers to communicate with
a running instance of Microsoft Flight Simulator X.
The original SimConnect client library included with the deluxe edition of FSX supports only
C/C++ and CLI compliant languages. This library is an attempt to provide the same set
of functionnalities to Java language developpers.
jSimConnect is not a native interface to the original microsoft library, it is a complete reimplementation of the client, partially compliant the version 4 of the SimConnect protocol (corresponding to FSX, SP1 and SP2 features). It is thus totally portable on all architectures supported by JVM (version 5.0). You can use it on the same machine as FSX or another machine through TCP/IP networking.
final int dataDefID = 1;
final int requestID = 1;
final int fourSeceventID = 1;
// connect to simconnect
SimConnect sc = new SimConnect("AIList", "10.1.0.6", 48447);
// build data definition
sc.addToDataDefinition(dataDefID, "STRUCT LATLONALT", null, SimConnectDataType.LATLONALT);
sc.addToDataDefinition(dataDefID, "ATC TYPE", null, SimConnectDataType.STRING32);
sc.addToDataDefinition(dataDefID, "ATC ID", null, SimConnectDataType.STRING32);
// get warned every 4 seconds when in sim mode
sc.subscribeToSystemEvent(fourSeceventID, "4sec");
DispatcherTask dt = new DispatcherTask(sc);
// just for esthetic purposes
dt.addOpenHandler(new OpenHandler() {
public void handleOpen(SimConnect sender, RecvOpen e) {
System.out.println("Connected to " + e.getApplicationName());
}});
// add an event handler to receive events every 4 seconds
dt.addEventHandler(new EventHandler() {
public void handleEvent(SimConnect sender, RecvEvent e) {
if (e.getEventID() == fourSeceventID) {
// request data for all aircrafts in the sim
try {
sender.requestDataOnSimObjectType(requestID, dataDefID, 100*1000, SimObjectType.AIRCRAFT);
} catch (IOException ioe) {}
}
}});
// handler called when received data requested by the call to
// requestDataOnSimObjectType
dt.addSimObjectDataTypeHandler(new SimObjectDataTypeHandler() {
public void handleSimObjectType(SimConnect sender, RecvSimObjectDataByType e) {
// retrieve data structures from packet
LatLonAlt position = e.getLatLonAlt();
String atcType = e.getDataString32();
String atcID = e.getDataString32();
// print to users
System.out.println("Plane id#" + e.getObjectID() + " no " + e.getEntryNumber() + "/" + e.getOutOf());
System.out.println("\tPosition: " + position.toString());
System.out.println("\tType/ID: " + atcType + " " + atcID);
// line separator
if (e.getEntryNumber() == e.getOutOf()) System.out.println();
}});
// spawn receiver thread
new Thread(dt).start();