Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
122 lines (108 loc) · 4.42 KB
/
Copy pathProgram.cs
File metadata and controls
122 lines (108 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.IO.Ports;
using OpenHardwareMonitor;
using OpenHardwareMonitor.Hardware;
namespace ArduinoHWMonitor {
class Program {
// Member data
Computer thisComputer; // To store the reference to this computer
SerialPort serPort; // To store connection to serial port
// Temperature stats
String cpuTemp; // To store CPU temperature
String gpuTemp; // To store GPU temperature
// Usage stats
String cpuUsage; // To store CPU usage
String gpuUsage; // To store GPU usage
// Member method
// Constructor
Program() {
// Creating new connection to get GPU and CPU data
thisComputer = new Computer() { CPUEnabled = true, GPUEnabled = true };
// Creating connection to SerialPort
serPort = new SerialPort();
serPort.PortName = "COM3"; // Setting port name
serPort.BaudRate = 500000; // Setting baud rate
// Opening port
serPort.Open();
// Opening connection
thisComputer.Open();
}
// Method to pad stats to 3 digits
private String padStat(String stat) {
if(stat.Length == 1) {
return " " + stat;
}
else if(stat.Length == 2) {
return " " + stat;
}
else if(stat.Length == 3) {
return stat;
}
return " ";
}
// Method to get CPU and GPU stats
private String getStats() {
// Getting each hardware item
foreach(var hwItem in thisComputer.Hardware) {
// Checking if hardware is CPU
if(hwItem.HardwareType == HardwareType.CPU) {
// Updating to get newest stats
hwItem.Update();
// Getting each stat
foreach(var sensor in hwItem.Sensors) {
// For temp
if(sensor.SensorType == SensorType.Temperature) {
cpuTemp = "" + (int)Math.Round(sensor.Value.Value);
}
// For usage
else if(sensor.SensorType == SensorType.Load) {
cpuUsage = "" + (int)Math.Round(sensor.Value.Value);
}
}
}
// Checking if hardware is GPU
else if (hwItem.HardwareType == HardwareType.GpuNvidia) {
// Updating to get newest stats
hwItem.Update();
// Getting each stat
foreach (var sensor in hwItem.Sensors) {
// For temp
if (sensor.SensorType == SensorType.Temperature) {
gpuTemp = "" + (int)Math.Round(sensor.Value.Value);
}
// For usage
else if (sensor.SensorType == SensorType.Load && sensor.Name == "GPU Core") {
gpuUsage = "" + (int)Math.Round(sensor.Value.Value);
}
}
}
}
// Padding stats
cpuTemp = padStat(cpuTemp);
gpuTemp = padStat(gpuTemp);
cpuUsage = padStat(cpuUsage);
gpuUsage = padStat(gpuUsage);
// Creating combined stat
String stats = "CPU : " + cpuUsage + "%" + " " + cpuTemp + "C" + "\nGPU : " + gpuUsage + "%" + " " + gpuTemp + "C";
return stats;
}
// Method to relay stats through serial port
private void sendStats(String stats) {
serPort.WriteLine(stats);
}
// Main method
public static void Main(String[] args) {
Program ob = new Program();
while (true) {
String stats = ob.getStats();
Console.WriteLine(stats);
ob.sendStats(stats);
Thread.Sleep(2000);
}
}
}
}