55using System . Net . Sockets ;
66using System . Text ;
77using System . Net ;
8+ using OpenHardwareMonitor . Hardware ;
9+ using System . Collections . Generic ;
810
911namespace 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