Skip to content

Commit 99368fe

Browse files
改用 OpenHardwareMonitor 获取硬件信息
改用 OpenHardwareMonitor 获取硬件信息
1 parent e3d6ff6 commit 99368fe

8 files changed

Lines changed: 327 additions & 28 deletions

File tree

客户端/App.config

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
5+
</startup>
6+
</configuration>

客户端/HardwareMonitor.csproj

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{25B85514-8430-4BCB-9CD9-2CDF3E6C9C8A}</ProjectGuid>
8+
<OutputType>Exe</OutputType>
9+
<RootNamespace>HardwareMonitor</RootNamespace>
10+
<AssemblyName>HardwareMonitor</AssemblyName>
11+
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
12+
<FileAlignment>512</FileAlignment>
13+
<Deterministic>true</Deterministic>
14+
</PropertyGroup>
15+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
16+
<PlatformTarget>AnyCPU</PlatformTarget>
17+
<DebugSymbols>true</DebugSymbols>
18+
<DebugType>full</DebugType>
19+
<Optimize>false</Optimize>
20+
<OutputPath>bin\Debug\</OutputPath>
21+
<DefineConstants>DEBUG;TRACE</DefineConstants>
22+
<ErrorReport>prompt</ErrorReport>
23+
<WarningLevel>4</WarningLevel>
24+
</PropertyGroup>
25+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
26+
<PlatformTarget>AnyCPU</PlatformTarget>
27+
<DebugType>pdbonly</DebugType>
28+
<Optimize>true</Optimize>
29+
<OutputPath>bin\Release\</OutputPath>
30+
<DefineConstants>TRACE</DefineConstants>
31+
<ErrorReport>prompt</ErrorReport>
32+
<WarningLevel>4</WarningLevel>
33+
</PropertyGroup>
34+
<ItemGroup>
35+
<Reference Include="OpenHardwareMonitorLib">
36+
<HintPath>.\OpenHardwareMonitorLib.dll</HintPath>
37+
</Reference>
38+
<Reference Include="System" />
39+
<Reference Include="System.Core" />
40+
<Reference Include="System.Management" />
41+
<Reference Include="System.Xml.Linq" />
42+
<Reference Include="System.Data.DataSetExtensions" />
43+
<Reference Include="Microsoft.CSharp" />
44+
<Reference Include="System.Data" />
45+
<Reference Include="System.Net.Http" />
46+
<Reference Include="System.Xml" />
47+
</ItemGroup>
48+
<ItemGroup>
49+
<Compile Include="Program.cs" />
50+
<Compile Include="Properties\AssemblyInfo.cs" />
51+
</ItemGroup>
52+
<ItemGroup>
53+
<None Include="App.config" />
54+
</ItemGroup>
55+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
56+
</Project>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|AnyCPU'">
4+
<StartArguments>192.168.3.162 4999 1.03 500 1 4</StartArguments>
5+
</PropertyGroup>
6+
</Project>

客户端/HardwareMonitor.sln

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.31205.134
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HardwareMonitor", "HardwareMonitor.csproj", "{25B85514-8430-4BCB-9CD9-2CDF3E6C9C8A}"
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+
{25B85514-8430-4BCB-9CD9-2CDF3E6C9C8A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{25B85514-8430-4BCB-9CD9-2CDF3E6C9C8A}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{25B85514-8430-4BCB-9CD9-2CDF3E6C9C8A}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{25B85514-8430-4BCB-9CD9-2CDF3E6C9C8A}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {C49319BD-FD47-4073-8F04-3193E604E642}
24+
EndGlobalSection
25+
EndGlobal
305 KB
Binary file not shown.

客户端/Program.cs

Lines changed: 198 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,52 @@
55
using System.Net.Sockets;
66
using System.Text;
77
using System.Net;
8+
using OpenHardwareMonitor.Hardware;
9+
using System.Collections.Generic;
810

911
namespace ConsoleApp1
1012
{
1113
public class Class1
1214
{
13-
static PerformanceCounter cpuCounter;
14-
static PerformanceCounter ramCounter;
15-
static double getAllRam;
16-
static double useVoltage;
17-
static int updateTime;
15+
static double getAllRam = 0;
16+
static double useVoltage = 0;
17+
static int updateTime = 0;
18+
static int sendValueID_1 = 0;
19+
static int sendValueID_2 = 0;
20+
1821
static string esp32Ip;
19-
static int esp32Port;
22+
static int esp32Port = 4999;
2023
static UdpClient udpClient = new UdpClient();
24+
static Computer computer;
2125

26+
static double cpuLoad = 0;
27+
static double cpuTemperature = 0;
28+
static double ramLoad = 0;
29+
static double gpuLoad = 0;
30+
static double gpuRamLoad = 0;
31+
static double gpuTemperature = 0;
2232
static void Main(string[] args)
2333
{
24-
if (args.Length != 4)
34+
35+
if (args.Length != 6)
2536
{
2637
Console.WriteLine("输入的配置项不正确!\n");
2738
Console.WriteLine("右键CpuRamGet.exe -> 创建快捷方式 -> 快捷方式右键属性 -> 目标 -> CpuRamGet.exe 尾部依照下列说明添加\n");
28-
Console.WriteLine("1.IP 2.端口号 3.电压表最大值(3.3V以内) 4.刷新时间(单位毫秒) 每项中间空格区分\n \n例: 192.168.1.199 2333 1 500");
39+
Console.WriteLine("1.IP 2.端口号 3.电压表最大值(3.3V以内) 4.刷新时间(单位毫秒) 5.发送数据ID 1 6.发送数据ID 2\n");
40+
Console.WriteLine("可选的ID(6选2): 1 CPU使用率, 2 CPU温度, 3 内存使用率, 4 GPU使用率, 5 GPU显存占用, 6 GPU温度\n");
41+
Console.WriteLine("每项中间空格区分\n \n例: 192.168.1.199 2333 1 500 1 2");
2942
Console.ReadKey();
3043
return;
3144
}
32-
45+
3346
esp32Ip = args[0];
3447
esp32Port = Convert.ToInt32(args[1]);
3548
useVoltage = (255.0f / 3.30f) * Convert.ToDouble(args[2]);
3649
updateTime = Convert.ToInt32(args[3]);
50+
sendValueID_1 = Convert.ToInt32(args[4]);
51+
sendValueID_2 = Convert.ToInt32(args[5]);
3752

38-
Init(out cpuCounter, out ramCounter,out getAllRam);
53+
Init();
3954
Thread thread = new Thread(SetEsp32);
4055
thread.Start();
4156
}
@@ -50,28 +65,185 @@ static void SetEsp32()
5065
while (true)
5166
{
5267
Console.Clear();
53-
double cpuProcessor = Math.Round(cpuCounter.NextValue(), 0);
54-
double ramMemory = Math.Round((1.0 - (ramCounter.NextValue() / getAllRam)) * 100, 0);
55-
Console.WriteLine("CPU使用率:" + cpuProcessor + "%");
56-
Console.WriteLine("总内存:" + getAllRam + " MB");
57-
Console.WriteLine("可使用内存:" + ramCounter.NextValue() + " MB");
58-
Console.WriteLine("占用内存百分比:" + ramMemory + "%");
68+
69+
foreach (var hardware in computer.Hardware)
70+
{
71+
hardware.Update();
72+
73+
if (hardware.HardwareType == HardwareType.CPU)
74+
{
75+
foreach (var sensor in hardware.Sensors)
76+
{
77+
if (sensor.SensorType == SensorType.Load)
78+
{
79+
if (sensor != null)
80+
{
81+
if (sensor.Index == 0) //总占用率
82+
{
83+
cpuLoad = (double)sensor.Value;
84+
}
85+
}
86+
}
87+
if (sensor.SensorType == SensorType.Temperature)
88+
{
89+
if (sensor != null)
90+
{
91+
if (sensor.Index == 0)
92+
{
93+
cpuTemperature = (double)sensor.Value;
94+
}
95+
}
96+
}
97+
}
98+
}
99+
else if (hardware.HardwareType == HardwareType.RAM)
100+
{
101+
foreach (var sensor in hardware.Sensors)
102+
{
103+
if (sensor.SensorType == SensorType.Load)
104+
{
105+
if (sensor != null)
106+
{
107+
ramLoad = (double)sensor.Value;
108+
}
109+
}
110+
}
111+
} //没有此显卡为空
112+
else if (hardware.HardwareType == HardwareType.GpuAti)
113+
{
114+
foreach (var sensor in hardware.Sensors)
115+
{
116+
if (sensor.SensorType == SensorType.Load)
117+
{
118+
if (sensor != null)
119+
{
120+
if (sensor != null)
121+
{
122+
if (sensor.Index == 0) //总占用率
123+
{
124+
gpuLoad = (double)sensor.Value;
125+
}
126+
else if (sensor.Index == 4) //显存占用
127+
{
128+
gpuRamLoad = (double)sensor.Value;
129+
}
130+
}
131+
}
132+
}
133+
if (sensor.SensorType == SensorType.Temperature)
134+
{
135+
if (sensor != null)
136+
{
137+
if (sensor.Index == 0) //Gpu温度
138+
{
139+
gpuTemperature = (double)sensor.Value;
140+
}
141+
}
142+
}
143+
}
144+
}
145+
else if (hardware.HardwareType == HardwareType.GpuNvidia)
146+
{
147+
foreach (var sensor in hardware.Sensors)
148+
{
149+
if (sensor.SensorType == SensorType.Load)
150+
{
151+
if (sensor != null)
152+
{
153+
if (sensor.Index == 0) //总占用率
154+
{
155+
gpuLoad = (double)sensor.Value;
156+
}
157+
else if (sensor.Index == 4) //显存占用
158+
{
159+
gpuRamLoad = (double)sensor.Value;
160+
}
161+
}
162+
}
163+
if (sensor.SensorType == SensorType.Temperature)
164+
{
165+
if (sensor != null)
166+
{
167+
if (sensor.Index == 0) //总占用率
168+
{
169+
gpuTemperature = (double)sensor.Value;
170+
}
171+
}
172+
}
173+
}
174+
}
175+
176+
}
177+
178+
cpuLoad = Math.Round(cpuLoad,0);
179+
cpuTemperature = Math.Round(cpuTemperature, 0);
180+
ramLoad = Math.Round(ramLoad, 0);
181+
gpuLoad = Math.Round(gpuLoad, 0);
182+
gpuRamLoad = Math.Round(gpuRamLoad, 0);
183+
gpuTemperature = Math.Round(gpuTemperature, 0);
184+
185+
Console.WriteLine("===================bilibili日出东水===================\n");
186+
Console.WriteLine("ID_1 CPU 使用率: " + cpuLoad + " %\n");
187+
Console.WriteLine("ID_2 CPU 温度: " + cpuTemperature + " C\n");
188+
189+
//Console.WriteLine("总内存: " + getAllRam + " MB");
190+
Console.WriteLine("ID_3 内存使用率: " + ramLoad + " %\n");
191+
192+
Console.WriteLine("ID_4 GPU 使用率: " + gpuLoad + " %\n");
193+
Console.WriteLine("ID_5 GPU 显存占用: " + gpuRamLoad + " %\n");
194+
Console.WriteLine("ID_6 GPU 温度: " + gpuTemperature + " C\n");
59195

60196
//dac的数值范围为0-255,实际输出电压值为0-3.3v
61-
Esp32Connected(Math.Round( RamapValue(ramMemory, 0, 100.0, 0, useVoltage),0) + ","+ Math.Round(RamapValue(cpuProcessor, 0, 100.0, 0, useVoltage), 0));
197+
string _sendType_1;
198+
string _sendType_2;
199+
double _sendValue_1 = SwitchSendValue(sendValueID_1, out _sendType_1);
200+
double _sendValue_2 = SwitchSendValue(sendValueID_2, out _sendType_2);
201+
202+
Console.WriteLine("配置的数据类型: [" + _sendType_1 +"] / ["+ _sendType_2+"]\n");
203+
204+
string sendStr1 = Convert.ToString(Math.Round(RamapValue(_sendValue_1, 0, 100.0, 0, useVoltage), 0));
205+
string sendStr2 = Convert.ToString(Math.Round(RamapValue(_sendValue_2, 0, 100.0, 0, useVoltage), 0));
206+
Esp32Connected(sendStr2 + "," + sendStr1);
207+
Console.WriteLine("\n----------------OpenHardwareMonitor------------------");
62208
System.Threading.Thread.Sleep(updateTime);
63209
}
64210
}
65211

66-
static void Init(out PerformanceCounter cpuCounter, out PerformanceCounter ramCounter, out double getAllRam)
212+
static double SwitchSendValue(int index,out string dataType)
213+
{
214+
switch (index)
215+
{
216+
case 1:
217+
dataType = "CPU 使用率";
218+
return cpuLoad;
219+
case 2:
220+
dataType = "CPU 温度";
221+
return cpuTemperature;
222+
case 3:
223+
dataType = "内存使用率";
224+
return ramLoad;
225+
case 4:
226+
dataType = "GPU 使用率";
227+
return gpuLoad;
228+
case 5:
229+
dataType = "GPU 显存占用";
230+
return gpuRamLoad;
231+
case 6:
232+
dataType = "GPU 温度";
233+
return gpuTemperature;
234+
}
235+
dataType = "传参错误";
236+
return 0;
237+
}
238+
239+
static void Init()
67240
{
68-
//获取Cpu使用率
69-
cpuCounter = new PerformanceCounter();
70-
cpuCounter.CategoryName = "Processor";
71-
cpuCounter.CounterName = "% Processor Time";
72-
cpuCounter.InstanceName = "_Total";
73-
cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");
74-
ramCounter = new PerformanceCounter("Memory", "Available MBytes");
241+
//获取Cpu,Gpu使用率温度等
242+
computer = new Computer();
243+
computer.CPUEnabled = true;
244+
computer.RAMEnabled = true;
245+
computer.GPUEnabled = true;
246+
computer.Open();
75247

76248
//获取总物理内存大小
77249
ManagementClass cimobject1 = new ManagementClass("Win32_PhysicalMemory");
@@ -96,7 +268,7 @@ static void Esp32Connected(string message)
96268
IPAddress remoteIp = IPAddress.Parse(esp32Ip);
97269
IPEndPoint remotePoint = new IPEndPoint(remoteIp, esp32Port);
98270
udpClient.Send(buffer, buffer.Length, remotePoint);
99-
Console.WriteLine("IP:"+esp32Ip + " 端口:"+esp32Port + " 发送信息: " + message);
271+
Console.WriteLine("IP:" + esp32Ip + " 端口:" + esp32Port + " 发送信息: " + message);
100272
}
101273
}
102274
}

0 commit comments

Comments
 (0)