#航天 #SimConnect #优化 #msfs-2020

simconnect-sdk

SimConnect SDK。一个具有观点的SimConnect客户端,完全封装了C API,并优化了开发者体验。

7 个版本

0.2.2 2023年2月22日
0.2.1 2022年10月29日
0.1.3 2022年10月24日

#103模拟

MIT 许可证

160KB
1K SLoC

包含 (Windows DLL, 63KB) ffi/lib/SimConnect.dll, (静态库, 24KB) ffi/lib/SimConnect.lib

SimConnect SDK

Crates Documentation CI license Crates.io
一个具有观点的SimConnect客户端,完全封装了C API,并优化了开发者体验。

用法

[dependencies]
simconnect-sdk = { version = "0.2", features = ["derive"] }
use simconnect_sdk::{Notification, SimConnect, SimConnectObject};

/// A data structure that will be used to receive data from SimConnect.
/// See the documentation of `SimConnectObject` for more information on the arguments of the `simconnect` attribute.
#[derive(Debug, Clone, SimConnectObject)]
#[simconnect(period = "second")]
#[allow(dead_code)]
struct AirplaneData {
    #[simconnect(name = "TITLE")]
    title: String,
    #[simconnect(name = "CATEGORY")]
    category: String,
    #[simconnect(name = "PLANE LATITUDE", unit = "degrees")]
    lat: f64,
    #[simconnect(name = "PLANE LONGITUDE", unit = "degrees")]
    lon: f64,
    #[simconnect(name = "PLANE ALTITUDE", unit = "feet")]
    alt: f64,
    #[simconnect(name = "SIM ON GROUND")]
    sim_on_ground: bool,
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = SimConnect::new("Receiving data example");

    match client {
        Ok(mut client) => {
            let mut notifications_received = 0;

            loop {
                let notification = client.get_next_dispatch()?;

                match notification {
                    Some(Notification::Open) => {
                        println!("Connection opened.");

                        // After the connection is successfully open, we register the struct
                        client.register_object::<AirplaneData>()?;
                    }
                    Some(Notification::Object(data)) => {
                        if let Ok(airplane_data) = AirplaneData::try_from(&data) {
                            println!("{airplane_data:?}");

                            notifications_received += 1;

                            // After we have received 10 notifications, we unregister the struct
                            if notifications_received > 10 {
                                client.unregister_object::<AirplaneData>()?;
                                println!("Subscription stopped.");
                                break;
                            }
                        }
                    }
                    _ => (),
                }

                // sleep for about a frame to reduce CPU usage
                std::thread::sleep(std::time::Duration::from_millis(16));
            }
        }
        Err(e) => {
            println!("Error: {e:?}")
        }
    }

    Ok(())
}

请参阅 更多示例

贡献

欢迎并鼓励贡献!请参阅 CONTRIBUTING.md

支持的功能

请参阅 FEATURES.md

致谢

rylev/msfs2020Sequal32/simconnect-rust 的启发。

依赖项

~1.5–3.5MB
~74K SLoC