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 可再发行组件),程序会自动在以下位置查找:
- MSFS SDK 安装目录(
Program Files (x86)/Microsoft SDKs/FlightSimulator/) - 本文件同目录
- 当前工作目录
site-packages/SimConnect/(PySimConnect 安装路径)- 系统 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) |
从回调指针按类型读取数据(静态方法,零拷贝) |
parse_exception(pData) |
解析异常消息,返回 (名称, sendID, index)(静态方法) |
start_background_dispatch(callback=None) |
启动后台 dispatch 线程(含自动重连) |
stop_background_dispatch() |
停止后台 dispatch 线程 |
subscribe(var_name, unit, callback, period=3) |
高层 SimVar 订阅(自动管理定义+请求) |
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.3.1" |
许可证
MIT License
Metadata
Release files for simconnect-H 0.3.1
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.3.1.tar.gz | 14.5 kB | With provenance | Details for simconnect_h-0.3.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| simconnect_h-0.3.1-py3-none-any.whl | Python 3 | none | any | Details for simconnect_h-0.3.1-py3-none-any.whl |
Total release size: 29.0 kB
Release files / simconnect_h-0.3.1.tar.gz
| Download URL | simconnect_h-0.3.1.tar.gz |
|---|---|
| Size | 14.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
196ba62e906c1d321c9d895fb48a19c71076f24ce33545faf24a14a14d3f6644
|
|
BLAKE2b-256 checksum How to use checksums |
48381c26331efdd28b2ffd1b1edea84af37a78479e0adfe992b8dc60936f8b31
|
| 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 9, 2026.
Transparency logRelease files / simconnect_h-0.3.1-py3-none-any.whl
| Download URL | simconnect_h-0.3.1-py3-none-any.whl |
|---|---|
| Size | 14.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
66bd3c8e7fce90ffbcbc66cd25dac23c7b1bd32482d544568a591bd4c6a1db96
|
|
BLAKE2b-256 checksum How to use checksums |
50d6ff08e6901beff62ade6676bb8b52f15cf172ffc20a3bfbc158af70b9fb2d
|
| 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 9, 2026.
Transparency logSecurity for simconnect-H 0.3.1
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.3.1.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 9, 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.