Skip to content

Commit 46101aa

Browse files
Update Program.cs
更新 支持串口连接, 通过USB数据线发送信息
1 parent 52a4481 commit 46101aa

1 file changed

Lines changed: 101 additions & 45 deletions

File tree

客户端/Program.cs

Lines changed: 101 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
using System.Net;
88
using OpenHardwareMonitor.Hardware;
99
using System.Collections.Generic;
10+
using System.IO.Ports;
1011

1112
namespace ConsoleApp1
1213
{
1314
public class Class1
1415
{
15-
static double getAllRam = 0;
1616
static double useVoltage = 0;
1717
static int updateTime = 0;
1818
static int sendValueID_1 = 0;
@@ -29,22 +29,47 @@ public class Class1
2929
static double gpuLoad = 0;
3030
static double gpuRamLoad = 0;
3131
static double gpuTemperature = 0;
32+
33+
static IPAddress remoteIp;
34+
static IPEndPoint remotePoint;
35+
36+
static SerialPort serialPort = new SerialPort();
37+
static bool isWiredWireless = false;
38+
static string serialPortIndex = "";
39+
static int baudRateValue = 115200;
40+
3241
static void Main(string[] args)
3342
{
3443

3544
if (args.Length != 6)
3645
{
3746
Console.WriteLine("输入的配置项不正确!\n");
38-
Console.WriteLine("右键CpuRamGet.exe -> 创建快捷方式 -> 快捷方式右键属性 -> 目标 -> CpuRamGet.exe 尾部依照下列说明添加\n");
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");
47+
Console.WriteLine("【1】右键CpuRamGet.exe -> 创建快捷方式 -> 快捷方式右键属性 -> 目标 -> CpuRamGet.exe 尾部依照下列说明添加\n");
48+
Console.WriteLine("【2】1.IP/串口号 2.端口号/波特率 3.电压表最大值(3.3V以内) 4.刷新时间(单位毫秒) 5.发送数据ID_1 6.发送数据ID_2\n");
49+
Console.WriteLine("【3】可选的ID(6选2): 1 CPU使用率, 2 CPU温度, 3 内存使用率, 4 GPU使用率, 5 GPU显存占用, 6 GPU温度\n");
50+
Console.WriteLine("注意:每项中间空格区分\n");
51+
Console.WriteLine("\n无线示例: 192.168.1.199 2333 1 500 1 2");
52+
Console.WriteLine("\nUSB串口示例: com4 115200 1 500 1 2");
4253
Console.ReadKey();
4354
return;
4455
}
56+
char[] switchString = "com".ToCharArray();
57+
char[] switchString2 = "COM".ToCharArray();
58+
int inquiryIndex = args[0].ToString().IndexOfAny(switchString);
59+
int inquiryIndex2 = args[0].ToString().IndexOfAny(switchString2);
60+
61+
if (inquiryIndex == 0 || inquiryIndex2 == 0)
62+
{
63+
serialPortIndex = args[0];
64+
baudRateValue = Convert.ToInt32(args[1]);
65+
isWiredWireless = true;
66+
}
67+
else
68+
{
69+
esp32Ip = args[0];
70+
esp32Port = Convert.ToInt32(args[1]);
71+
}
4572

46-
esp32Ip = args[0];
47-
esp32Port = Convert.ToInt32(args[1]);
4873
useVoltage = (255.0f / 3.30f) * Convert.ToDouble(args[2]);
4974
updateTime = Convert.ToInt32(args[3]);
5075
sendValueID_1 = Convert.ToInt32(args[4]);
@@ -117,16 +142,13 @@ static void SetEsp32()
117142
{
118143
if (sensor != null)
119144
{
120-
if (sensor != null)
145+
if (sensor.Index == 0) //总占用率
146+
{
147+
gpuLoad = (double)sensor.Value;
148+
}
149+
else if (sensor.Index == 4) //显存占用
121150
{
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-
}
151+
gpuRamLoad = (double)sensor.Value;
130152
}
131153
}
132154
}
@@ -172,7 +194,6 @@ static void SetEsp32()
172194
}
173195
}
174196
}
175-
176197
}
177198

178199
cpuLoad = Math.Round(cpuLoad,0);
@@ -183,23 +204,22 @@ static void SetEsp32()
183204
gpuTemperature = Math.Round(gpuTemperature, 0);
184205

185206
Console.WriteLine("===================bilibili日出东水===================\n");
186-
Console.WriteLine("ID_1 CPU 使用率: " + cpuLoad + " %\n");
187-
Console.WriteLine("ID_2 CPU 温度: " + cpuTemperature + " C\n");
207+
Console.WriteLine("ID_1 CPU 使用率: {0} %\n", cpuLoad);
208+
Console.WriteLine("ID_2 CPU 温度: {0} C\n", cpuTemperature);
188209

189-
//Console.WriteLine("总内存: " + getAllRam + " MB");
190-
Console.WriteLine("ID_3 内存使用率: " + ramLoad + " %\n");
210+
Console.WriteLine("ID_3 内存使用率: {0} %\n", ramLoad);
191211

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");
212+
Console.WriteLine("ID_4 GPU 使用率: {0} %\n", gpuLoad);
213+
Console.WriteLine("ID_5 GPU 显存占用: {0} %\n", gpuRamLoad);
214+
Console.WriteLine("ID_6 GPU 温度: {0} C\n", gpuTemperature);
195215

196216
//dac的数值范围为0-255,实际输出电压值为0-3.3v
197217
string _sendType_1;
198218
string _sendType_2;
199219
double _sendValue_1 = SwitchSendValue(sendValueID_1, out _sendType_1);
200220
double _sendValue_2 = SwitchSendValue(sendValueID_2, out _sendType_2);
201221

202-
Console.WriteLine("配置的数据类型: [" + _sendType_1 +"] / ["+ _sendType_2+"]\n");
222+
Console.WriteLine("配置的数据类型: [{0}] / [{1}] \n",_sendType_1,_sendType_2);
203223

204224
string sendStr1 = Convert.ToString(Math.Round(RamapValue(_sendValue_1, 0, 100.0, 0, useVoltage), 0));
205225
string sendStr2 = Convert.ToString(Math.Round(RamapValue(_sendValue_2, 0, 100.0, 0, useVoltage), 0));
@@ -238,37 +258,73 @@ static double SwitchSendValue(int index,out string dataType)
238258

239259
static void Init()
240260
{
261+
if (isWiredWireless)
262+
{
263+
serialPort.PortName = serialPortIndex;//串口号
264+
serialPort.BaudRate = baudRateValue;//波特率
265+
serialPort.DataBits = 8;//数据位
266+
try
267+
{
268+
serialPort.Open();
269+
}
270+
catch (Exception _exception)
271+
{
272+
Console.WriteLine(_exception.Message);
273+
}
274+
}
275+
else
276+
{
277+
try
278+
{
279+
remoteIp = IPAddress.Parse(esp32Ip);
280+
remotePoint = new IPEndPoint(remoteIp, esp32Port);
281+
}
282+
catch (Exception _exception)
283+
{
284+
Console.WriteLine(_exception.Message);
285+
}
286+
287+
}
288+
241289
//获取Cpu,Gpu使用率温度等
242290
computer = new Computer();
243291
computer.CPUEnabled = true;
244292
computer.RAMEnabled = true;
245293
computer.GPUEnabled = true;
246294
computer.Open();
247-
248-
//获取总物理内存大小
249-
ManagementClass cimobject1 = new ManagementClass("Win32_PhysicalMemory");
250-
ManagementObjectCollection moc1 = cimobject1.GetInstances();
251-
double capacity = 0;
252-
foreach (ManagementObject mo1 in moc1)
253-
{
254-
capacity += ((Math.Round(Int64.Parse(mo1.Properties["Capacity"].Value.ToString()) / 1024 / 1024 / 1024.0, 1)));
255-
}
256-
moc1.Dispose();
257-
cimobject1.Dispose();
258-
getAllRam = capacity * 1024;
259295
}
260296

261297
static void Esp32Connected(string message)
262298
{
263-
//UDP 方式发送信息给开发板
264-
byte[] buffer = Encoding.UTF8.GetBytes(message);
265-
266-
if (udpClient != null)
299+
if (isWiredWireless)
300+
{
301+
if(serialPort.IsOpen)
302+
{
303+
serialPort.WriteLine(message);
304+
Console.WriteLine("串口号: {0} 波特率: {1} 发送信息: {2}", serialPortIndex, baudRateValue, message);
305+
}
306+
else
307+
{
308+
Console.WriteLine("出错啦! 串口: {0} 无法打开,请检查!", serialPortIndex);
309+
}
310+
}
311+
else
267312
{
268-
IPAddress remoteIp = IPAddress.Parse(esp32Ip);
269-
IPEndPoint remotePoint = new IPEndPoint(remoteIp, esp32Port);
270-
udpClient.Send(buffer, buffer.Length, remotePoint);
271-
Console.WriteLine("IP:" + esp32Ip + " 端口:" + esp32Port + " 发送信息: " + message);
313+
//UDP 方式发送信息给开发板
314+
byte[] buffer = Encoding.UTF8.GetBytes(message);
315+
316+
if (udpClient != null)
317+
{
318+
if (remotePoint !=null)
319+
{
320+
udpClient.Send(buffer, buffer.Length, remotePoint);
321+
Console.WriteLine("IP: {0} 端口: {1} 发送信息: {2}", esp32Ip, esp32Port, message);
322+
}
323+
else
324+
{
325+
Console.WriteLine("出错啦! IP地址: {0} 无效,请检查!", esp32Ip);
326+
}
327+
}
272328
}
273329
}
274330
}

0 commit comments

Comments
 (0)