Skip to main content

原生 ctypes SimConnect 库 — 零外部依赖,直接加载 SimConnect.dll 与 MSFS 通讯

Project description

simconnect-H

原生 ctypes SimConnect 库 — 零外部依赖,直接加载 SimConnect.dll 与 Microsoft Flight Simulator 通讯。

特性

  • 🚫 零依赖 — 仅 ctypes 标准库,纯 Python、可 PyInstaller 打包
  • 🔓 MIT 许可 — 无 AGPL 污染,可闭源商用
  • 🪶 单文件 — 即插即用,源码仅 ~800 行
  • 🔒 线程安全 — dispatch 回调加锁保护
  • 📦 上下文管理器 — 支持 with sc: 自动断开
  • 🔍 智能搜索 — 自动定位 SimConnect.dll

安装

# 从源码安装
pip install .

# 或直接复制 simconnect_native/ 到项目目录

需要 SimConnect.dll(微软模拟飞行 SDK 可再发行组件),程序会自动在以下位置查找:

  1. MSFS SDK 安装目录(Program Files (x86)/Microsoft SDKs/FlightSimulator/
  2. 本文件同目录
  3. 当前工作目录
  4. site-packages/SimConnect/(PySimConnect 安装路径)
  5. 系统 PATH

也可以手动指定路径:

sc = SimConnect()
sc.load_dll(r"C:\Path\To\SimConnect.dll")

快速入门

from simconnect_native import (
    SimConnect,
    SIMCONNECT_SIMOBJECT_TYPE_USER,
    SIMCONNECT_RECV_ID_SIMOBJECT_DATA_BYTYPE,
    SIMCONNECT_RECV_ID_OPEN,
    SIMCONNECT_RECV_ID_EXCEPTION,
    FULL_SIMOBJECT_DATA, EXCEPTION_MSG, EXCEPTION_NAMES,
)

# 支持 with 语句,自动 close
with SimConnect() as sc:
    sc.load_dll()
    sc.open(b"MyApp")

    # 注册数据定义
    sc.add_to_data_definition(1, b"PLANE ALTITUDE", b"Feet")

    # 设置 dispatch 回调
    def on_dispatch(pData, cbData, pContext):
        try:
            dwID = pData.contents.dwID
        except Exception:
            return

        if dwID == SIMCONNECT_RECV_ID_OPEN:
            print("✓ 已连接到 MSFS")

        elif dwID == SIMCONNECT_RECV_ID_EXCEPTION:
            exc = ctypes.cast(pData, ctypes.POINTER(EXCEPTION_MSG)).contents
            name = EXCEPTION_NAMES.get(exc.dwException, f"UNKNOWN({exc.dwException})")
            print(f"⚠ 异常: {name}")

        elif dwID == SIMCONNECT_RECV_ID_SIMOBJECT_DATA_BYTYPE:
            req_id, val = sc.read_double(pData)
            print(f"📊 req={req_id} value={val}")

    sc.set_dispatch_cb(on_dispatch)

    # 请求数据
    sc.request_data_on_simobject_type(1, 1, 0, SIMCONNECT_SIMOBJECT_TYPE_USER)

    # 轮询
    import time
    for _ in range(100):
        sc.dispatch()
        time.sleep(0.01)

    # 写入数据
    from ctypes import c_double, cast, c_void_p
    arr = (c_double * 1)(1000.0)
    ptr = cast(arr, c_void_p)
    sc.set_data_on_simobject(1, data_ptr=ptr)

    # 发送事件
    sc.map_client_event_to_sim_event(100, b"KEY_TOGGLE")
    sc.transmit_client_event(0, 100, 0)

# with 块结束自动断开

v0.2.0 迁移说明FULL_SIMOBJECT_DATA 已精简为头部元数据结构体(SIMOBJECT_DATA_HEADER),不再包含预分配的 dwData 数组。 所有数据读取请改用 SimConnect.read_data(pData, datatype)sc.read_double(pData),内部使用指针偏移零拷贝读取。 向后兼容:FULL_SIMOBJECT_DATA 名称仍可作为 SIMOBJECT_DATA_HEADER 的别名导入。

API 一览

SimConnect

方法 说明
load_dll(path=None) 加载 SimConnect.dll
open(app_name, ...) 连接 MSFS
close() 断开连接
add_to_data_definition(id, name, unit, ...) 注册 SimVar
clear_data_definition(id) 清除定义
request_data_on_simobject_type(req_id, def_id, ...) 请求数据
request_data_on_simobject(req_id, def_id, ...) 请求持续数据更新
add_and_request(req_id, def_id, name, unit, ...) 注册+请求一步完成
set_data_on_simobject(def_id, *, object_id, flags, ...) 写入数据
write_double(def_id, value) 快捷写入 double 值
map_client_event_to_sim_event(ev_id, name) 映射事件
transmit_client_event(obj_id, ev_id, data, ...) 发送事件
subscribe_to_system_event(id, name) 订阅系统事件
dispatch() 处理一次消息队列
set_dispatch_cb(callback) 设置 dispatch 回调
call_dispatch(callback) 设置并触发 dispatch
read_double(pData) 从回调中解析 float64 值
read_data(pData, datatype=0) 从回调指针按类型读取数据(静态方法,零拷贝)
start_background_dispatch(callback=None) 启动后台 dispatch 线程
stop_background_dispatch() 停止后台 dispatch 线程
get_last_sent_packet_id() 获取最后发送的数据包 ID
event_data_float(value) float → DWORD 位转换(静态方法)

属性

属性 说明
handle SimConnect 句柄(HANDLE)
dll 已加载的 WinDLL 对象
is_open 是否已连接

模块级工具

函数 说明
find_simconnect_dll() 自动搜索 SimConnect.dll 路径
read_data_value(pData, datatype=0) 从 dispatch 回调中读取指定类型数据
__version__ 当前库版本 "0.2.0"

许可证

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

simconnect_h-0.2.0.tar.gz (11.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

simconnect_h-0.2.0-py3-none-any.whl (13.0 kB view details)

Uploaded Python 3

File details

Details for the file simconnect_h-0.2.0.tar.gz.

File metadata

  • Download URL: simconnect_h-0.2.0.tar.gz
  • Upload date:
  • Size: 11.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for simconnect_h-0.2.0.tar.gz
Algorithm Hash digest
SHA256 6ab4caadff595086e4b1551d0e8c0ccac54409757fc10329521b23df65db17a1
MD5 0f3a2d632fd6b6c4841ec8e2087137d9
BLAKE2b-256 336322319a818a254c65fe496e238692edae9e20ac8967ff32005674657f3620

See more details on using hashes here.

File details

Details for the file simconnect_h-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: simconnect_h-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 13.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for simconnect_h-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 62a323de007c6b6a3b49e689d3269f5aeb6106f0d8dd6cfef848d37a6d6381d0
MD5 ebdb18d87acee715886ada89136ed1a3
BLAKE2b-256 f37400f3fa9fa74ec3442c73e94cf5d1ac3602af115e65deaab446a165560274

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page