Skip to content

Commit 5496117

Browse files
committed
upload
1 parent 330eb4a commit 5496117

21 files changed

+1515
-0
lines changed

Docs/images/JST.png

194 KB
Loading

Docs/images/MCP2221a.png

254 KB
Loading

Hardware/I2CommunicationHelper.cs

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
namespace KCNVrmModTool.Hardware
2+
{
3+
public class I2CommunicationHelper
4+
{
5+
private readonly MCP2221Device _device;
6+
private const int MaxRetries = 4;
7+
8+
public I2CommunicationHelper(MCP2221Device device)
9+
{
10+
_device = device;
11+
}
12+
13+
public int WriteBlock(
14+
byte smbAddress,
15+
byte[] smbDataToSend,
16+
uint numberOfBytesToWrite,
17+
uint smbSpeed,
18+
int smbPage)
19+
{
20+
int result = 0;
21+
int retryCount = 0;
22+
byte[] pageCommand = new byte[4];
23+
24+
do
25+
{
26+
if (retryCount > 0)
27+
Console.WriteLine($" try #{retryCount}");
28+
29+
retryCount++;
30+
31+
if (smbPage >= 0)
32+
{
33+
pageCommand[0] = 0;
34+
pageCommand[1] = Convert.ToByte(smbPage);
35+
result = _device.WriteBlock(smbAddress, pageCommand, 2, smbSpeed, 0);
36+
}
37+
38+
if (result == 0)
39+
result = _device.WriteBlock(smbAddress, smbDataToSend, numberOfBytesToWrite, smbSpeed, 0);
40+
}
41+
while (result != 0 && retryCount < MaxRetries);
42+
43+
return result;
44+
}
45+
46+
public int ReadBlock(
47+
byte smbAddress,
48+
byte[] smbDataToRead,
49+
uint numberOfBytesToRead,
50+
uint smbSpeed,
51+
int smbPage,
52+
byte readRegIndex)
53+
{
54+
int result = 0;
55+
int retryCount = 0;
56+
byte[] pageCommand = new byte[4];
57+
58+
do
59+
{
60+
if (retryCount > 0)
61+
Console.WriteLine($" retry#{retryCount}");
62+
63+
retryCount++;
64+
65+
if (smbPage >= 0)
66+
{
67+
pageCommand[0] = 0;
68+
pageCommand[1] = Convert.ToByte(smbPage);
69+
result = _device.WriteBlock(smbAddress, pageCommand, 2, smbSpeed, 0);
70+
}
71+
72+
if (result == 0)
73+
result = _device.ReadBlock(smbAddress, smbDataToRead, numberOfBytesToRead, smbSpeed, 0, readRegIndex);
74+
}
75+
while (result != 0 && retryCount < MaxRetries);
76+
77+
return result;
78+
}
79+
80+
public int ReadBlockScan(
81+
byte smbAddress,
82+
byte[] smbDataToRead,
83+
uint numberOfBytesToRead,
84+
uint smbSpeed,
85+
int smbPage,
86+
byte readRegIndex)
87+
{
88+
int result = 0;
89+
int retryCount = 0;
90+
byte[] pageCommand = new byte[4];
91+
92+
do
93+
{
94+
retryCount++;
95+
96+
if (smbPage >= 0)
97+
{
98+
pageCommand[0] = 0;
99+
pageCommand[1] = Convert.ToByte(smbPage);
100+
result = _device.WriteBlock(smbAddress, pageCommand, 2, smbSpeed, 0);
101+
}
102+
103+
if (result == 0)
104+
result = _device.ReadBlock(smbAddress, smbDataToRead, numberOfBytesToRead, smbSpeed, 0, readRegIndex);
105+
}
106+
while (result != 0 && retryCount < 2);
107+
108+
return result;
109+
}
110+
}
111+
}

Hardware/MCP2221Device.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
namespace KCNVrmModTool.Hardware
2+
{
3+
public class MCP2221Device
4+
{
5+
public bool IsConnected { get; private set; }
6+
7+
public bool Initialize()
8+
{
9+
try
10+
{
11+
// 加载DLL并获取函数指针
12+
if (!NativeDllLoader.LoadDllFromResources())
13+
{
14+
Console.WriteLine("无法加载驱动Dll。");
15+
return false;
16+
}
17+
18+
// 使用加载的函数
19+
NativeDllLoader.DllInit();
20+
IsConnected = NativeDllLoader.GetConnectionStatus();
21+
return true;
22+
}
23+
catch (Exception ex)
24+
{
25+
Console.WriteLine($"初始化出错:{ex.Message}\n");
26+
return false;
27+
}
28+
}
29+
30+
public void SelectDevice(int deviceIndex)
31+
{
32+
NativeDllLoader.SelectDev(deviceIndex);
33+
}
34+
35+
public int WriteBlock(byte address, byte[] data, uint length, uint speed, byte usesPec)
36+
{
37+
return NativeDllLoader.SmbWriteBlock(address, data, length, speed, usesPec);
38+
}
39+
40+
public int ReadBlock(byte address, byte[] data, uint length, uint speed, byte usesPec, byte regIndex)
41+
{
42+
return NativeDllLoader.SmbReadBlock(address, data, length, speed, usesPec, regIndex);
43+
}
44+
}
45+
}

Hardware/NativeDllLoader.cs

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
using System.Runtime.InteropServices;
2+
3+
namespace KCNVrmModTool.Hardware
4+
{
5+
public static class NativeDllLoader
6+
{
7+
private static IntPtr _dllHandle = IntPtr.Zero;
8+
private static string _tempFileName = string.Empty;
9+
10+
[DllImport("kernel32.dll")]
11+
private static extern IntPtr LoadLibrary(string dllToLoad);
12+
13+
[DllImport("kernel32.dll")]
14+
private static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName);
15+
16+
[DllImport("kernel32.dll")]
17+
private static extern bool FreeLibrary(IntPtr hModule);
18+
19+
// 添加委托定义,与DLL中的函数签名匹配
20+
[UnmanagedFunctionPointer(CallingConvention.StdCall, CharSet = CharSet.Unicode)]
21+
public delegate void DllInitDelegate();
22+
23+
[UnmanagedFunctionPointer(CallingConvention.StdCall, CharSet = CharSet.Unicode)]
24+
public delegate bool GetConnectionStatusDelegate();
25+
26+
[UnmanagedFunctionPointer(CallingConvention.StdCall, CharSet = CharSet.Unicode)]
27+
public delegate int SelectDevDelegate(int whichDevice);
28+
29+
[UnmanagedFunctionPointer(CallingConvention.StdCall, CharSet = CharSet.Unicode)]
30+
public delegate int SmbWriteBlockDelegate(
31+
byte smbAddress,
32+
byte[] smbDataToSend,
33+
uint numberOfBytesToWrite,
34+
uint smbSpeed,
35+
byte usesPEC);
36+
37+
[UnmanagedFunctionPointer(CallingConvention.StdCall, CharSet = CharSet.Unicode)]
38+
public delegate int SmbReadBlockDelegate(
39+
byte smbAddress,
40+
byte[] smbDataToRead,
41+
uint numberOfBytesToRead,
42+
uint smbSpeed,
43+
byte usesPEC,
44+
byte readRegIndex);
45+
46+
// 存储函数指针;;
47+
public static DllInitDelegate DllInit;
48+
public static GetConnectionStatusDelegate GetConnectionStatus;
49+
public static SelectDevDelegate SelectDev;
50+
public static SmbWriteBlockDelegate SmbWriteBlock;
51+
public static SmbReadBlockDelegate SmbReadBlock;
52+
53+
public static bool LoadDllFromResources()
54+
{
55+
if (_dllHandle != IntPtr.Zero)
56+
return true; // DLL已加载
57+
58+
try
59+
{
60+
// 从资源中获取DLL数据
61+
byte[] dllData = Properties.Resources.MCP2221DLL_UM_x86;
62+
63+
// 创建唯一临时文件路径
64+
_tempFileName = Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid()}.dll");
65+
66+
// 将DLL数据写入临时文件
67+
File.WriteAllBytes(_tempFileName, dllData);
68+
69+
// 加载DLL
70+
_dllHandle = LoadLibrary(_tempFileName);
71+
if (_dllHandle == IntPtr.Zero)
72+
{
73+
int error = Marshal.GetLastWin32Error();
74+
throw new Exception($"无法加载DLL: {_tempFileName},错误代码: {error}");
75+
}
76+
77+
// 获取函数指针并绑定到委托
78+
IntPtr pDllInit = GetProcAddress(_dllHandle, "DllInit");
79+
IntPtr pGetConnectionStatus = GetProcAddress(_dllHandle, "GetConnectionStatus");
80+
IntPtr pSelectDev = GetProcAddress(_dllHandle, "SelectDev");
81+
IntPtr pSmbWriteBlock = GetProcAddress(_dllHandle, "SmbWriteBlock");
82+
IntPtr pSmbReadBlock = GetProcAddress(_dllHandle, "SmbReadBlock");
83+
84+
if (pDllInit == IntPtr.Zero || pGetConnectionStatus == IntPtr.Zero ||
85+
pSelectDev == IntPtr.Zero || pSmbWriteBlock == IntPtr.Zero || pSmbReadBlock == IntPtr.Zero)
86+
{
87+
throw new Exception("无法获取DLL中的函数指针");
88+
}
89+
90+
// 创建委托
91+
DllInit = Marshal.GetDelegateForFunctionPointer<DllInitDelegate>(pDllInit);
92+
GetConnectionStatus = Marshal.GetDelegateForFunctionPointer<GetConnectionStatusDelegate>(pGetConnectionStatus);
93+
SelectDev = Marshal.GetDelegateForFunctionPointer<SelectDevDelegate>(pSelectDev);
94+
SmbWriteBlock = Marshal.GetDelegateForFunctionPointer<SmbWriteBlockDelegate>(pSmbWriteBlock);
95+
SmbReadBlock = Marshal.GetDelegateForFunctionPointer<SmbReadBlockDelegate>(pSmbReadBlock);
96+
97+
// 设置程序退出时清理DLL
98+
AppDomain.CurrentDomain.ProcessExit += (s, e) => CleanupDll();
99+
100+
return true;
101+
}
102+
catch (Exception ex)
103+
{
104+
Console.WriteLine($"加载DLL资源时出错: {ex.Message}");
105+
CleanupDll(); // 清理任何部分创建的资源
106+
return false;
107+
}
108+
}
109+
110+
private static void CleanupDll()
111+
{
112+
try
113+
{
114+
if (_dllHandle != IntPtr.Zero)
115+
{
116+
FreeLibrary(_dllHandle);
117+
_dllHandle = IntPtr.Zero;
118+
}
119+
120+
if (!string.IsNullOrEmpty(_tempFileName) && File.Exists(_tempFileName))
121+
{
122+
try
123+
{
124+
File.Delete(_tempFileName);
125+
_tempFileName = string.Empty;
126+
}
127+
catch (Exception ex)
128+
{
129+
Console.WriteLine($"无法删除临时DLL文件: {ex.Message}");
130+
}
131+
}
132+
}
133+
catch (Exception ex)
134+
{
135+
Console.WriteLine($"清理DLL时出错: {ex.Message}");
136+
}
137+
}
138+
}
139+
}

KCNVrmModTool.csproj

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net9.0-windows</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
<PlatformTarget>x86</PlatformTarget>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<Compile Update="Properties\Resources.Designer.cs">
13+
<DesignTime>True</DesignTime>
14+
<AutoGen>True</AutoGen>
15+
<DependentUpon>Resources.resx</DependentUpon>
16+
</Compile>
17+
</ItemGroup>
18+
19+
<ItemGroup>
20+
<EmbeddedResource Update="Properties\Resources.resx">
21+
<Generator>ResXFileCodeGenerator</Generator>
22+
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
23+
</EmbeddedResource>
24+
</ItemGroup>
25+
26+
<ItemGroup>
27+
<None Update="Resources\MCP2221DLL-UM_x86.dll">
28+
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
29+
</None>
30+
</ItemGroup>
31+
32+
</Project>

KCNVrmModTool.sln

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.12.35707.178 d17.12
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "KCNVrmModTool", "KCNVrmModTool.csproj", "{9710BFA8-2397-40B4-8E4D-2E4292378807}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{9710BFA8-2397-40B4-8E4D-2E4292378807}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{9710BFA8-2397-40B4-8E4D-2E4292378807}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{9710BFA8-2397-40B4-8E4D-2E4292378807}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{9710BFA8-2397-40B4-8E4D-2E4292378807}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal

Models/CommandOptions.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
namespace KCNVrmModTool.Models
2+
{
3+
public enum Command
4+
{
5+
None,
6+
Scan,
7+
MP2955A,
8+
PXE1610C,
9+
TPS53679,
10+
TPS53678
11+
}
12+
13+
public class CommandOptions
14+
{
15+
public Command Command { get; set; }
16+
public byte Address1 { get; set; }
17+
public byte Address2 { get; set; }
18+
}
19+
}

0 commit comments

Comments
 (0)