simconnect-H
面向 MSFS 的原生 Python SimConnect 库 — 仅用标准库 ctypes 加载 SimConnect.dll,零 pip 依赖,可 PyInstaller 打包、可闭源商用(MIT)。
为什么选它
| simconnect-H | 典型 FSX 时代封装 | |
|---|---|---|
| 依赖 | 仅 ctypes | 第三方包 / 旧枚举 |
MSFS DATATYPE |
与 SDK 一致(FLOAT64=4) |
常错成 0 |
| 连接 | connect() 一键搞定 |
需自己试 config_index |
| 高频数据 | subscribe_many 批量推送 |
多靠轮询 get() |
特性
- 零依赖 — 不拉 PyPI 运行时包,复制目录或
pip install -e .即可 - 一键连接 — 自动找 DLL、扫描
config_index0–7、等待 OPEN、启动后台 dispatch - 三层 API —
get/set/subscribe开箱即用;底层 ctypes 接口完整保留 - 批量订阅 —
subscribe_many一次定义几十个 SimVar,适合 20–50 Hz 遥测 - 健壮性 — HRESULT 严格检查、自动清理、
SimConnectError明确报错 - 线程安全 — dispatch 与订阅路由加锁;并发
get()串行隔离
安装
git clone https://github.com/hjznb887/simconnect-H.git
cd simconnect-H
pip install -e .
或直接复制 simconnect_native/ 到你的项目。
SimConnect.dll 从哪来
需要 64 位 MSFS SDK 可再发行版 SimConnect.dll。查找顺序:
- 环境变量
SIMCONNECT_DLL - 便携 exe 同目录 /
lib\SimConnect.dll - MSFS 游戏目录(Steam / Microsoft Store,与运行中模拟器同版本)
- 包内
simconnect_native/lib/SimConnect.dll - MSFS SDK 安装路径
# 可选:把 SDK DLL 复制进包内
scripts\copy_simconnect_dll.ps1
30 秒上手
MSFS 已启动、进入飞行、未暂停:
from simconnect_native import SimConnect
with SimConnect() as sc:
sc.connect("MyApp")
alt = sc.get("PLANE ALTITUDE", "feet", timeout=2.0)
print(f"高度: {alt:.1f} ft")
sc.subscribe("AIRSPEED INDICATED", "knots", lambda v: print(f"IAS: {v:.0f}"))
import time
time.sleep(5)
命令行自检
pip install -e .
python examples\diagnose_read.py # 应输出「读数链路正常」
python examples\read_write.py
推荐用法:高频 + 多变量
读:用 subscribe_many 推送,不要循环 get()。
写:用 set() / trigger(),发完即返。
from simconnect_native import SimConnect, SIMCONNECT_PERIOD_SIM_FRAME
FIELDS = {
"alt": ("PLANE ALTITUDE", "feet"),
"ias": ("AIRSPEED INDICATED", "knots"),
"hdg": ("PLANE HEADING DEGREES TRUE", "degrees"),
# … 继续加到几十个
}
state = {}
with SimConnect() as sc:
sc.connect("Telemetry")
sc.subscribe_many(
FIELDS,
lambda d: state.update(d),
period=SIMCONNECT_PERIOD_SIM_FRAME,
)
while running:
# 读 state,写控制
sc.set("GENERAL ENG THROTTLE LEVER POSITION:1", 0.75, "percent")
time.sleep(0.02) # 20ms 由你的业务决定,库按 sim 帧推送
| 场景 | 推荐 API |
|---|---|
| 偶尔读一次 | get() |
| 20–50 Hz、几十个变量 | subscribe_many() |
| 写 SimVar / 发事件 | set() / trigger() |
| 完全自控协议 | 底层 add_to_data_definition + dispatch |
架构一览
SimConnect.connect()
├── load_dll() 搜索 SimConnect.dll
├── open() SimConnect_Open + SimStart 订阅
└── background dispatch 后台线程 pump 消息
├── OPEN / QUIT / SimStart → 重连与恢复订阅
├── subscribe 回调 → SimVar 推送
└── get() 同步读 → 一次性 Request + Event
资源管理:推荐 with SimConnect() as sc: 或显式 close();析构时会兜底释放句柄。
API 速查
连接与生命周期
| 方法 | 说明 |
|---|---|
connect(app_name, ...) |
推荐 一键连接 |
load_dll(path=None) |
加载 DLL |
open(...) / close() |
底层连接 / 断开 |
start_background_dispatch() |
后台 pump(含自动重连) |
数据读写
| 方法 | 说明 |
|---|---|
get(var, unit, timeout=0.1) |
同步读(单次,勿高频轮询) |
set(var, value, unit) |
写 SimVar |
subscribe(var, unit, callback, period=SIM_FRAME) |
单变量订阅 |
subscribe_many(fields, callback, period=SIM_FRAME) |
多变量批量订阅 |
unsubscribe(sub_id) |
取消订阅 |
trigger(event_name, ...) |
触发 MSFS 事件 |
常量(与 MSFS SDK 一致)
from simconnect_native import (
SIMCONNECT_DATATYPE_FLOAT64, # 4
SIMCONNECT_PERIOD_SIM_FRAME, # 3 — 每 sim 帧
SIMCONNECT_PERIOD_VISUAL_FRAME,# 2 — 视觉帧
SIMConnectError,
)
完整列表见 simconnect_native/constants.py。
底层示例
需要完全控制 dispatch 时:
from simconnect_native import (
SimConnect,
SIMCONNECT_DATATYPE_FLOAT64,
SIMCONNECT_SIMOBJECT_TYPE_USER,
SIMCONNECT_RECV_ID_OPEN,
SIMCONNECT_RECV_ID_SIMOBJECT_DATA_BYTYPE,
)
with SimConnect() as sc:
sc.connect("MyApp", start_dispatch=False)
sc.add_to_data_definition(1, b"PLANE ALTITUDE", b"feet", SIMCONNECT_DATATYPE_FLOAT64)
def on_dispatch(p_data, _cb, _ctx):
if p_data.contents.dwID == SIMCONNECT_RECV_ID_OPEN:
print("OPEN")
elif p_data.contents.dwID == SIMCONNECT_RECV_ID_SIMOBJECT_DATA_BYTYPE:
_, val = sc.read_double(p_data)
print(f"ALT: {val}")
sc.set_dispatch_cb(on_dispatch)
sc.start_background_dispatch()
sc.request_data_on_simobject_type(1, 1, 0, SIMCONNECT_SIMOBJECT_TYPE_USER)
版本说明
v0.5.2
- 修复
connect(wait_open=True)未收到 OPEN 仍返回成功 - 修复
subscribe回调内get()无法完成、并发get()串包 - 重连时重置 SimStart 订阅;用户 dispatch 回调异常不再静默
- HRESULT 严格化、常量补全、34 项单元测试
v0.5.0
- 关键修复:
SIMCONNECT_DATATYPE_FLOAT64 = 4(MSFS SDK 正确值,非 FSX 的0) - 新增
connect()、RequestDataOnSimObjectType读路径、游戏目录 DLL 优先
迁移提示
若旧代码手动传 datatype=0 期望 float64,请改为 4 或 SIMCONNECT_DATATYPE_FLOAT64。
开发与测试
python -m unittest discover -s tests -v
许可证
MIT License — 可自由用于商业与闭源项目。
Metadata
Release files for simconnect-H 0.5.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| simconnect_h-0.5.2.tar.gz | 27.7 kB | With provenance | Details for simconnect_h-0.5.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| simconnect_h-0.5.2-py3-none-any.whl | Python 3 | none | any | Details for simconnect_h-0.5.2-py3-none-any.whl |
Total release size: 56.1 kB
Release files / simconnect_h-0.5.2.tar.gz
| Download URL | simconnect_h-0.5.2.tar.gz |
|---|---|
| Size | 27.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0b03513592e62c1975f2ad873787eb397f5a7663198c75268f01957616906d3c
|
|
BLAKE2b-256 checksum How to use checksums |
4ce1eb188b0137b8eaefc2b6e89267f95bce4b066852d37a5391017d4cae3178
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jun 11, 2026.
Transparency logRelease files / simconnect_h-0.5.2-py3-none-any.whl
| Download URL | simconnect_h-0.5.2-py3-none-any.whl |
|---|---|
| Size | 28.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
213877e4ab8792b3e45f89da91228bdbcf27bf793922aa5c6d800a9aeb19bdd8
|
|
BLAKE2b-256 checksum How to use checksums |
04921dfae8cd6b0255eedcaa0772c5160c0edef7994a5f91ad4c3b625e7b3fe0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jun 11, 2026.
Transparency logSecurity for simconnect-H 0.5.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for simconnect_h-0.5.2.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jun 11, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.