[31] | 1 | # Basic test program for the Python client library
|
---|
| 2 |
|
---|
| 3 | #------------------------------------------------------------------------------
|
---|
| 4 |
|
---|
| 5 | if __name__ == "__main__":
|
---|
| 6 | from xplra import XPlane, ProtocolException
|
---|
| 7 |
|
---|
| 8 | xplane = XPlane()
|
---|
| 9 |
|
---|
| 10 | try:
|
---|
| 11 | print "Connecting to X-Plane..."
|
---|
| 12 | xplane.connect()
|
---|
| 13 | print "Connected to X-Plane."
|
---|
| 14 | print
|
---|
| 15 |
|
---|
[36] | 16 | print "Querying the versions..."
|
---|
| 17 | (xplaneVersion, xplmVersion, xplraVersion) = xplane.getVersions()
|
---|
| 18 | print "X-Plane version: %d, XPLM: %d, XPLRA: %03d" % \
|
---|
| 19 | (xplaneVersion, xplmVersion, xplraVersion)
|
---|
| 20 | print
|
---|
| 21 |
|
---|
[31] | 22 | print "Querying the number of the engines..."
|
---|
| 23 | numEngines = xplane.getInt("sim/aircraft/engine/acf_num_engines")
|
---|
| 24 | print "The number of engines:", numEngines
|
---|
| 25 | print
|
---|
| 26 |
|
---|
| 27 | try:
|
---|
| 28 | print "Querying an invalid dataref..."
|
---|
| 29 | xplane.getInt("sim/aircraft/engine/num_engines")
|
---|
| 30 | print
|
---|
| 31 | print ">>>>>>>>>>>> Succeeded!!!!!!!!!!!!!!!!!!!!!!"
|
---|
| 32 | except ProtocolException as e:
|
---|
| 33 | print "Exception caugth:", str(e)
|
---|
| 34 | print
|
---|
| 35 |
|
---|
| 36 | print "Querying the spool time of a jet engine..."
|
---|
| 37 | spoolTime = xplane.getFloat("sim/aircraft/engine/acf_spooltime_jet")
|
---|
| 38 | print "The spool time:", spoolTime
|
---|
| 39 | print
|
---|
| 40 |
|
---|
| 41 | print "Querying the spool time of a propeller..."
|
---|
| 42 | spoolTime = xplane.getFloat("sim/aircraft/engine/acf_spooltime_prop")
|
---|
| 43 | print "The spool time:", spoolTime
|
---|
| 44 | print
|
---|
| 45 |
|
---|
| 46 | print "Querying the coordinates..."
|
---|
| 47 | latitude = xplane.getDouble("sim/flightmodel/position/latitude")
|
---|
| 48 | longitude = xplane.getDouble("sim/flightmodel/position/longitude")
|
---|
| 49 | print "The coordinates: " + `latitude` + ", " + `longitude`
|
---|
| 50 | print
|
---|
| 51 |
|
---|
| 52 | print "Querying the aircraft's description..."
|
---|
| 53 | result = xplane.getByteArray("sim/aircraft/view/acf_descrip")
|
---|
[35] | 54 | print "The description:", result
|
---|
[31] | 55 | print
|
---|
| 56 |
|
---|
| 57 | print "Querying the aircraft's description as a string, with an offset of 3..."
|
---|
| 58 | result2 = xplane.getString("sim/aircraft/view/acf_descrip", offset = 3);
|
---|
[32] | 59 | print "Got: '" + result2 + "' (" + `len(result2)` + ")"
|
---|
| 60 | print result2=="h 8 Q400"
|
---|
[31] | 61 | print
|
---|
| 62 |
|
---|
| 63 | print "Querying the aircraft's engine types..."
|
---|
| 64 | result3 = xplane.getIntArray("sim/aircraft/prop/acf_en_type")
|
---|
[35] | 65 | print "Got:", result3
|
---|
[31] | 66 | print
|
---|
| 67 |
|
---|
| 68 | print "Querying the aircraft's propeller directions..."
|
---|
| 69 | result5 = xplane.getFloatArray("sim/aircraft/prop/acf_prop_dir")
|
---|
[35] | 70 | print "Got:", result5
|
---|
[31] | 71 | print
|
---|
| 72 |
|
---|
[32] | 73 | print "Setting the number of the engines to", numEngines + 1
|
---|
| 74 | xplane.setInt("sim/aircraft/engine/acf_num_engines", numEngines + 1)
|
---|
| 75 | numEngines = xplane.getInt("sim/aircraft/engine/acf_num_engines")
|
---|
| 76 | print "The new number of engines:", numEngines
|
---|
| 77 | print
|
---|
| 78 |
|
---|
| 79 | acfElevUp = xplane.getFloat("sim/aircraft/controls/acf_elev_up")
|
---|
| 80 | print "Setting the aircraft elevator up control from %f to %f..." % \
|
---|
| 81 | (acfElevUp, acfElevUp + 15.0)
|
---|
| 82 | xplane.setFloat("sim/aircraft/controls/acf_elev_up", acfElevUp + 15.0)
|
---|
| 83 | acfElevUp = xplane.getFloat("sim/aircraft/controls/acf_elev_up")
|
---|
| 84 | print "The aircraft elevator up control set to", acfElevUp
|
---|
| 85 | print
|
---|
| 86 |
|
---|
| 87 | localX = xplane.getDouble("sim/flightmodel/position/local_x")
|
---|
| 88 | print "Setting the aircraft's local X-coordinate from %f to %f..." % \
|
---|
| 89 | (localX, localX + 15.0)
|
---|
| 90 | xplane.setDouble("sim/flightmodel/position/local_x", localX + 15.0)
|
---|
| 91 | localX = xplane.getDouble("sim/flightmodel/position/local_x")
|
---|
| 92 | print "The aircraft's local X-coordinate set to", localX
|
---|
| 93 | print
|
---|
| 94 |
|
---|
[35] | 95 | numBlades = xplane.getFloatArray("sim/aircraft/prop/acf_num_blades")
|
---|
[32] | 96 | print "Setting the number of blades"
|
---|
| 97 | print " from:", numBlades
|
---|
| 98 | numBlades = [n+2.5 for n in numBlades]
|
---|
| 99 | print " to:", numBlades
|
---|
| 100 | xplane.setFloatArray("sim/aircraft/prop/acf_num_blades", numBlades)
|
---|
| 101 |
|
---|
[35] | 102 | numBlades = xplane.getFloatArray("sim/aircraft/prop/acf_num_blades")
|
---|
[32] | 103 | print "The result:", numBlades
|
---|
| 104 | print
|
---|
| 105 |
|
---|
[35] | 106 | batteryArrayOn = xplane.getIntArray("sim/cockpit/electrical/battery_array_on")
|
---|
[32] | 107 | print "Setting the batteries"
|
---|
| 108 | print " from:", batteryArrayOn
|
---|
| 109 | batteryArrayOn = [1 if b==0 else 0 for b in batteryArrayOn]
|
---|
| 110 | print " to:", batteryArrayOn
|
---|
| 111 | xplane.setIntArray("sim/cockpit/electrical/battery_array_on", batteryArrayOn)
|
---|
[35] | 112 | batteryArrayOn = xplane.getIntArray("sim/cockpit/electrical/battery_array_on")
|
---|
[32] | 113 | print "The result:", batteryArrayOn
|
---|
| 114 | print
|
---|
| 115 |
|
---|
| 116 | tailNum = [0] * 40
|
---|
| 117 | tailNum[0] = ord('H')
|
---|
| 118 | tailNum[1] = ord('A')
|
---|
| 119 | tailNum[2] = ord('-')
|
---|
| 120 | tailNum[3] = ord('V')
|
---|
| 121 | tailNum[4] = ord('A')
|
---|
| 122 | tailNum[5] = ord('I')
|
---|
| 123 |
|
---|
| 124 | print "Setting the tail number to %s as a byte array..." % (tailNum,)
|
---|
| 125 | xplane.setByteArray("sim/aircraft/view/acf_tailnum", tailNum)
|
---|
| 126 | print "The tail number is:", xplane.getString("sim/aircraft/view/acf_tailnum")
|
---|
| 127 | print
|
---|
| 128 |
|
---|
| 129 | tailNum1 = "VAIS"
|
---|
| 130 | print "Setting the tail number to " + tailNum1 + " as a string..."
|
---|
| 131 | xplane.setString("sim/aircraft/view/acf_tailnum", tailNum1, 40)
|
---|
| 132 | print "The tail number is:", xplane.getString("sim/aircraft/view/acf_tailnum")
|
---|
| 133 | print
|
---|
| 134 |
|
---|
| 135 |
|
---|
[31] | 136 | except Exception as e:
|
---|
| 137 | print ">>>>>>>>>>>>>>>>>>>>>> Exception caught:", str(e)
|
---|
| 138 | finally:
|
---|
| 139 | xplane.disconnect()
|
---|