Skip to content

Commit 1821ffe

Browse files
更新添加USB 串口通信固件
更新添加USB 串口通信固件
1 parent 46101aa commit 1821ffe

3 files changed

Lines changed: 116 additions & 5 deletions

File tree

Esp32/串口通信版本/main.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
from machine import I2C, Pin
2+
import time
3+
from machine import Pin,DAC
4+
import utime, math
5+
import select
6+
import _thread
7+
import socket
8+
import sys
9+
10+
11+
def lerp(v1,v2,d):
12+
return v1 * (1 - d) + v2 * d
13+
14+
def dacThread( threadName, delay):
15+
global ram_value
16+
ramlerp = 0
17+
i = 0.0
18+
while(True):
19+
i += 0.01
20+
ramlerp = lerp(ramlerp,ram_value,i)
21+
dac25.write(int(ramlerp))
22+
if(ramlerp >= ram_value):
23+
i = 0.0
24+
time.sleep(0.015)
25+
26+
27+
def dacThread2( threadName, delay):
28+
global cpu_value
29+
global ram_value
30+
cpulerp = 0
31+
i = 0.0
32+
countZero = 0
33+
cpu_valueOld = 0
34+
ram_valueOld = 0
35+
while(True):
36+
i += 0.01
37+
cpulerp = lerp(cpulerp,cpu_value,i)
38+
dac26.write(int(cpulerp))
39+
if(abs(cpulerp - cpu_value) <3):
40+
i = 0.0
41+
time.sleep(0.015)
42+
43+
#如果在1.5秒内数值都相同那么归零表值
44+
countZero += 1
45+
if(countZero == 500):
46+
if(cpu_value == cpu_valueOld and ram_value == ram_valueOld):
47+
ram_value = 0
48+
cpu_value = 0
49+
cpu_valueOld = cpu_value
50+
ram_valueOld = ram_value
51+
countZero = 0
52+
53+
54+
cpu_value = 0
55+
ram_value = 0
56+
57+
#初始化DAC
58+
dac_pin25 = Pin(25, Pin.OUT)
59+
dac_pin26 = Pin(26, Pin.OUT)
60+
61+
dac25 = DAC(dac_pin25)
62+
dac26 = DAC(dac_pin26)
63+
dac25.write(0)
64+
dac26.write(0)
65+
66+
p22 = Pin(2, Pin.OUT)
67+
68+
p = select.poll()
69+
p.register(
70+
sys.stdin, # 检测标准输入 (REPL)
71+
select.POLLIN # 检查是否有数据待读取
72+
)
73+
74+
_thread.start_new_thread( dacThread, ("Thread_1", 1, ) )
75+
_thread.start_new_thread( dacThread2, ("Thread_2", 2, ) )
76+
77+
while(True):
78+
try:
79+
readInfo = sys.stdin.read(5)
80+
array_value = readInfo.split(",")
81+
cpu_value = int(array_value[1])
82+
ram_value = int(array_value[0])
83+
print(cpu_value)
84+
print(ram_value)
85+
time.sleep(0.1)
86+
except:
87+
#print(getTime()+" 连接断开...")
88+
time.sleep(0.1)

客户端/Program.cs

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using OpenHardwareMonitor.Hardware;
99
using System.Collections.Generic;
1010
using System.IO.Ports;
11+
using System.Collections;
1112

1213
namespace ConsoleApp1
1314
{
@@ -38,6 +39,7 @@ public class Class1
3839
static string serialPortIndex = "";
3940
static int baudRateValue = 115200;
4041

42+
4143
static void Main(string[] args)
4244
{
4345

@@ -78,6 +80,9 @@ static void Main(string[] args)
7880
Init();
7981
Thread thread = new Thread(SetEsp32);
8082
thread.Start();
83+
Thread thread2 = new Thread(UpdateReceive);
84+
thread2.Start();
85+
8186
}
8287

8388
static double RamapValue(double Value, double Low1Val, double High1Val, double Low2Val, double High2Val)
@@ -206,9 +211,7 @@ static void SetEsp32()
206211
Console.WriteLine("===================bilibili日出东水===================\n");
207212
Console.WriteLine("ID_1 CPU 使用率: {0} %\n", cpuLoad);
208213
Console.WriteLine("ID_2 CPU 温度: {0} C\n", cpuTemperature);
209-
210214
Console.WriteLine("ID_3 内存使用率: {0} %\n", ramLoad);
211-
212215
Console.WriteLine("ID_4 GPU 使用率: {0} %\n", gpuLoad);
213216
Console.WriteLine("ID_5 GPU 显存占用: {0} %\n", gpuRamLoad);
214217
Console.WriteLine("ID_6 GPU 温度: {0} C\n", gpuTemperature);
@@ -221,8 +224,9 @@ static void SetEsp32()
221224

222225
Console.WriteLine("配置的数据类型: [{0}] / [{1}] \n",_sendType_1,_sendType_2);
223226

224-
string sendStr1 = Convert.ToString(Math.Round(RamapValue(_sendValue_1, 0, 100.0, 0, useVoltage), 0));
225-
string sendStr2 = Convert.ToString(Math.Round(RamapValue(_sendValue_2, 0, 100.0, 0, useVoltage), 0));
227+
string sendStr1 = Math.Round(RamapValue(_sendValue_1, 0.0, 99.0, 0.0, useVoltage), 0).ToString("00");
228+
string sendStr2 = Math.Round(RamapValue(_sendValue_2, 0.0, 99.0, 0.0, useVoltage), 0).ToString("00");
229+
226230
Esp32Connected(sendStr2 + "," + sendStr1);
227231
Console.WriteLine("\n----------------OpenHardwareMonitor------------------");
228232
System.Threading.Thread.Sleep(updateTime);
@@ -294,13 +298,32 @@ static void Init()
294298
computer.Open();
295299
}
296300

301+
static void UpdateReceive()
302+
{
303+
try
304+
{
305+
while (true)
306+
{
307+
String input = serialPort.ReadLine();
308+
Console.WriteLine("回读数据:" + input);
309+
System.Threading.Thread.Sleep(0);
310+
}
311+
}
312+
catch (Exception _exception)
313+
{
314+
Console.WriteLine(_exception);
315+
}
316+
317+
}
318+
297319
static void Esp32Connected(string message)
298320
{
299321
if (isWiredWireless)
300322
{
301323
if(serialPort.IsOpen)
302324
{
303-
serialPort.WriteLine(message);
325+
//serialPort.WriteLine(message);
326+
serialPort.Write(message);
304327
Console.WriteLine("串口号: {0} 波特率: {1} 发送信息: {2}", serialPortIndex, baudRateValue, message);
305328
}
306329
else

0 commit comments

Comments
 (0)