-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.cs
429 lines (408 loc) · 14.7 KB
/
Main.cs
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using Microsoft.VisualBasic;
using System.Diagnostics;
using System.Xml.Linq;
using System.Runtime.InteropServices;
namespace LEDSegmentDisplay_Remote
{
public partial class Main : Form
{
Socket PublicRemote = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
public Main()
{
InitializeComponent();
comboBox1.SelectedIndex = 0;
System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;//跨线程调用窗体组件无视警告
}
private void button1_Click(object sender, EventArgs e)
{
AutoStart.Enabled = true;
AutoStop.Enabled = false;
this.Text = Variables.Text_Title;
Thread thread = new Thread(new ThreadStart(ButtonSend));
thread.Start();
}
public void ButtonSend()
{
try
{
button1.Enabled = false;
button1.Text = Variables.Text_PleaseWait;
System.Threading.Thread.Sleep(100);
ConnectItem();
Upload(CommandBox.Text, PublicRemote);
button1.Text = Variables.Text_Done;
System.Threading.Thread.Sleep(2000);
PublicRemote.Close();
}
catch {
button1.Text = Variables.Text_Fail;
System.Threading.Thread.Sleep(500);
}
button1.Enabled = true;
button1.Text = Variables.Text_Sent;
}
public void Upload(string message,Socket Remote)
{
Remote.Send(Tobt(message));//向目标机发送消息
}
static byte[] Tobt(string mgs)
{
byte[] cmgs = Encoding.UTF8.GetBytes(mgs);
return cmgs;
}
private void PortBox_TextChanged(object sender, EventArgs e)
{
try
{
Variables.Port = int.Parse(PortBox.Text);
}
catch { }
}
private void PortBox_KeyPress(object sender, KeyPressEventArgs e)
{
if (!(Char.IsNumber(e.KeyChar)) && e.KeyChar != (char)8)
{
e.Handled = true;
}
}
private void IPBox_TextChanged(object sender, EventArgs e)
{
Variables.IP = IPBox.Text;
}
private void Main_Load(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
AutoStart.Enabled = false;
AutoStart.Text = Variables.Text_PleaseWait;
AutoStop.Enabled = true;
if (comboBox1.SelectedIndex == 0)//CPU占用
{
Thread thread = new Thread(new ThreadStart(CPUCountProcess));
thread.Start();
}
else if (comboBox1.SelectedIndex == 1)//剩余内存
{
Thread thread = new Thread(new ThreadStart(RAMRemainProcess));
thread.Start();
}
else if (comboBox1.SelectedIndex == 2)//已用内存
{
Thread thread = new Thread(new ThreadStart(RAMUsedProcess));
thread.Start();
}
else if (comboBox1.SelectedIndex == 3)//GPU内存
{
Thread thread = new Thread(new ThreadStart(GPURAMRemainProcess));
thread.Start();
}
else if (comboBox1.SelectedIndex == 4)//时间
{
Thread thread = new Thread(new ThreadStart(SystemTime));
thread.Start();
}
else if (comboBox1.SelectedIndex == 5)//下行网速
{
Thread thread = new Thread(new ThreadStart(DownloadSpeedProcess));
thread.Start();
}
}
public void SystemTime()
{
try
{
ConnectItem();
int dottime=0;
int dot = 0;
AutoStart.Text = Variables.Text_Begin;
while (true)
{
System.Threading.Thread.Sleep(50);
dottime = dottime + 50;
string date = DateTime.Now.ToLocalTime().ToString("HH:mm");
string[] a = date.Split(':');
int time= int.Parse(a[0]+a[1]);
if (dottime >= 500)
{
dottime = 0;
if (dot == 0)
dot = 2;
else
dot = 0;
}
Upload(ToCode(time,dot, true), PublicRemote);
}
}
catch
{
PublicRemote.Close();
this.Text = Variables.Text_Title;
}
}
public void CPUCountProcess()
{
try
{
ConnectItem();
//初始化CPU计数器
PerformanceCounter cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");
AutoStart.Text = Variables.Text_Begin;
while (true)
{
System.Threading.Thread.Sleep(500);
int cpu = (int)cpuCounter.NextValue();
Console.WriteLine(cpu);
Upload(ToCode(cpu, 0, Variables.AutoFillZero), PublicRemote);
}
}
catch
{
PublicRemote.Close();
this.Text = Variables.Text_Title;
}
}
public void RAMRemainProcess()
{
try
{
ConnectItem();
//初始化内存计数器
PerformanceCounter ramCounter = new PerformanceCounter("Memory", "Available MBytes");
AutoStart.Text = Variables.Text_Begin;
while (true)
{
System.Threading.Thread.Sleep(50);
int ram = (int)ramCounter.NextValue();
Console.WriteLine(ram);
Upload(ToCode(ram,0, Variables.AutoFillZero), PublicRemote);
}
}
catch
{
PublicRemote.Close();
this.Text = Variables.Text_Title;
}
}
public void RAMUsedProcess()
{
try
{
ConnectItem();
//初始化内存计数器
PerformanceCounter ramCounter = new PerformanceCounter("Memory", "Available MBytes");
int TotalRAM = (int)PerformanceInfo.GetTotalMemoryInMiB();
AutoStart.Text = Variables.Text_Begin;
while (true)
{
System.Threading.Thread.Sleep(50);
int ram = TotalRAM- (int)ramCounter.NextValue();
Console.WriteLine(ram);
Upload(ToCode(ram, 0, Variables.AutoFillZero), PublicRemote);
}
}
catch
{
PublicRemote.Close();
this.Text = Variables.Text_Title;
}
}
public void GPURAMRemainProcess()
{
try
{
ConnectItem();
//
while (true)
{
System.Threading.Thread.Sleep(50);
//
//Console.WriteLine(GPURAM);
//Upload(ToCode(GPURAM, 0, Variables.AutoFillZero), PublicRemote);
}
}
catch
{
PublicRemote.Close();
this.Text = Variables.Text_Title;
}
}
public void DownloadSpeedProcess()
{
Console.WriteLine("1");
PublicRemote.Close();
try
{
//连接目标
PublicRemote = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ipp = new IPEndPoint(IPAddress.Parse(Variables.IP), Variables.Port);
PublicRemote.Connect(ipp);
Console.WriteLine("2");
//初始化下行网速计数器
PerformanceCounter downloadCounter = new PerformanceCounter("Network Interface", "Bytes Received/sec");
PerformanceCounter NetworkDownSpe = new PerformanceCounter("Network Interface", "Bytes Received/sec");
//PerformanceCounter pc2 = new PerformanceCounter("Network Interface", "Bytes Sent/sec");
Console.WriteLine("3");
while (true)
{
System.Threading.Thread.Sleep(500);
Console.WriteLine(downloadCounter.NextValue().ToString());
int down = (int)downloadCounter.NextValue();
Console.WriteLine(down);
Upload(ToCode(down, 0, Variables.AutoFillZero), PublicRemote);
}
}
catch
{
PublicRemote.Close();
this.Text = Variables.Text_Title;
}
}
public void ConnectItem()//连接目标
{
PublicRemote.Close();
PublicRemote = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ipp = new IPEndPoint(IPAddress.Parse(Variables.IP), Variables.Port);
PublicRemote.Connect(ipp);
}
public string ToCode(int number,int dot,bool autofill)
{
string tail="0-0-0-0";
if (dot == 1) tail = "1-0-0-0";
else if (dot == 2) tail = "0-1-0-0";
else if (dot == 3) tail = "0-0-1-0";
else if (dot == 4) tail = "0-0-0-1";
else if (dot == 12) tail = "1-1-0-0";
else if (dot == 13) tail = "1-0-1-0";
else if (dot == 14) tail = "1-0-0-1";
else if (dot == 23) tail = "0-1-1-0";
else if (dot == 24) tail = "0-1-0-1";
else if (dot == 34) tail = "0-0-1-1";
else if (dot == 123) tail = "1-1-1-0";
else if (dot == 124) tail = "1-1-0-1";
else if (dot == 134) tail = "1-0-1-1";
else if (dot == 1234) tail = "1-1-1-1";
string numberstr = number.ToString();
string result = Variables.Data_OverStep;
//Console.WriteLine(numberstr.Substring(0, 1));
if (autofill)
{
if (numberstr.Length == 1)
{
result = "0-0-0-" + numberstr + "-" + tail;
}
else if (numberstr.Length == 2)
{
result = "0-0-" + numberstr.Substring(0, 1) + "-"
+ numberstr.Substring(1, 1) + "-" + tail;
}
else if (numberstr.Length == 3)
{
result = "0-" + numberstr.Substring(0, 1) + "-"
+ numberstr.Substring(1, 1) + "-"
+ numberstr.Substring(2, 1) + "-" + tail;
}
else if (numberstr.Length == 4)
{
result = numberstr.Substring(0, 1) + "-"
+ numberstr.Substring(1, 1) + "-"
+ numberstr.Substring(2, 1) + "-"
+ numberstr.Substring(3, 1) + "-" + tail;
}
}
else
{
if (numberstr.Length == 1)
{
result = "B-B-B-" + numberstr + "-" + tail;
}
else if (numberstr.Length == 2)
{
result = "B-B-" + numberstr.Substring(0, 1) + "-"
+ numberstr.Substring(1, 1) + "-" + tail;
}
else if (numberstr.Length == 3)
{
result = "B-" + numberstr.Substring(0, 1) + "-"
+ numberstr.Substring(1, 1) + "-"
+ numberstr.Substring(2, 1) + "-" + tail;
}
else if (numberstr.Length == 4)
{
result = numberstr.Substring(0, 1) + "-"
+ numberstr.Substring(1, 1) + "-"
+ numberstr.Substring(2, 1) + "-"
+ numberstr.Substring(3, 1) + "-" + tail;
}
}
this.Text = Variables.Text_Title + " " + result;
return result;
}
private void AutoStop_Click(object sender, EventArgs e)
{
AutoStart.Enabled = true;
AutoStop.Enabled = false;
PublicRemote.Close();
this.Text = Variables.Text_Title;
}
private void Main_FormClosing(object sender, FormClosingEventArgs e)
{
Environment.Exit(0);
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
Variables.AutoFillZero = true;
else
Variables.AutoFillZero = false;
}
}
public static class PerformanceInfo
{
[DllImport("psapi.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetPerformanceInfo([Out] out PerformanceInformation PerformanceInformation, [In] int Size);
[StructLayout(LayoutKind.Sequential)]
public struct PerformanceInformation
{
public int Size;
public IntPtr CommitTotal;
public IntPtr CommitLimit;
public IntPtr CommitPeak;
public IntPtr PhysicalTotal;
public IntPtr PhysicalAvailable;
public IntPtr SystemCache;
public IntPtr KernelTotal;
public IntPtr KernelPaged;
public IntPtr KernelNonPaged;
public IntPtr PageSize;
public int HandlesCount;
public int ProcessCount;
public int ThreadCount;
}
public static Int64 GetTotalMemoryInMiB()
{
PerformanceInformation pi = new PerformanceInformation();
if (GetPerformanceInfo(out pi, Marshal.SizeOf(pi)))
{
return Convert.ToInt64((pi.PhysicalTotal.ToInt64() * pi.PageSize.ToInt64() / 1048576));
}
else
{
return -1;
}
}
}
}