Код: Выделить всё
import os
import platform
import pylink
from pylink import Library
def __init__(self):
# 获取当前脚本所在目录
base_dir = os.path.dirname(__file__)
# 判断 Python 位数
is_64bits = platform.architecture()[0] == "64bit"
# 根据位数选择 DLL 名称
dll_name = "JLink_x64.dll" if is_64bits else "JLinkARM.dll"
dll_path = os.path.join(base_dir, dll_name)
print(f"正在使用 DLL: {dll_path}")
# 创建 Library 对象并传给 JLink
lib = Library(dll_path)
self.jlink = pylink.JLink(lib)
self.connected = False
def open(self):
"""打开 J-Link 设备"""
try:
self.jlink.open()
return True
except Exception as e:
print(f"打开 J-Link 失败: {e}")
return False
def connect_device(self, device, speed):
"""连接目标芯片"""
try:
if not self.jlink.opened():
self.open()
self.jlink.connect(device, speed)
self.connected = True
print(f"已连接到 {device}")
return True
except Exception as e:
print(f"连接设备失败: {e}")
return False
def read_info(self):
"""读取芯片信息"""
if not self.connected:
print("未连接设备")
return None
try:
core_id = self.jlink.core_id()
speed = self.jlink.speed()
return {
"CoreID": core_id,
"Speed": speed
}
except Exception as e:
print(f"读取信息失败: {e}")
return None
# 烧录 addr:Flash 起始地址烧录
def flash_firmware(self, file_path, addr):
"""下载固件到目标设备"""
if not self.connected:
print("未连接设备")
return False
try:
self.jlink.flash_file(file_path, addr)
print("固件下载完成")
return True
except Exception as e:
print(f"固件下载失败: {e}")
return False
def close(self):
"""关闭 J-Link 连接"""
try:
self.jlink.close()
self.connected = False
print("已关闭 J-Link")
except Exception as e:
print(f"关闭失败: {e}")
Шаг 3. Затем в моем MainWindow.xaml.cs я написал…
Код: Выделить всё
public partial class MainWindow : Window
{
dynamic jlinkHandler;
public MainWindow()
{
// 设置Python解释器DLL的路径。这里指定了Python 3.10的安装路径下的python310.dll。
// 注意:这个路径应该根据你的Python安装位置进行调整。
Runtime.PythonDLL = "D:\\Python\\Python311\\python311.dll";
// 初始化 Python 引擎
PythonEngine.Initialize();
using (Py.GIL())
{
dynamic sys = Py.Import("sys");
// 添加 WPF 运行目录到 sys.path
sys.path.append(AppDomain.CurrentDomain.BaseDirectory);
// 导入你的 JLinkHandler.py
dynamic jlinkModule = Py.Import("jlink_handler");
jlinkHandler = jlinkModule.JLinkHandler();
}
InitializeComponent();
}
}
Python.Runtime.PythonException: «Нет модуля с именем
'jlink_handler'» в динамическом jlinkModule =
Py.Import("jlink_handler");
Что может быть причиной этого?
Подробнее здесь: https://stackoverflow.com/questions/798 ... g-py-files
Мобильная версия