新增数据处理脚本,可以画折线图
This commit is contained in:
parent
b74569668e
commit
bd8d7778b0
11
ReadMe.md
11
ReadMe.md
@ -38,6 +38,17 @@ uart_temp是用于8051单片机上的一个项目,用于获取温度,并使
|
||||
|
||||
4. 打开串口软件,接收来自单片机的数据
|
||||
|
||||
5. 运行`process.py`解析数据生成图片
|
||||
|
||||
```bash
|
||||
# 新建虚拟环境
|
||||
virtualenv env
|
||||
# 安装依赖
|
||||
pip install -r requirements.txt
|
||||
# 运行脚本
|
||||
python process.py
|
||||
```
|
||||
|
||||
## 更多的信息
|
||||
|
||||
[从Keil迁移到SDCC](https://blog.csdn.net/weixin_44681954/article/details/135286404)
|
||||
|
42
process.py
Normal file
42
process.py
Normal file
@ -0,0 +1,42 @@
|
||||
#!./env/bin/python
|
||||
from datetime import datetime, timedelta
|
||||
import matplotlib.pyplot as plt
|
||||
from pylab import mpl
|
||||
|
||||
# 设置显示中文字体
|
||||
mpl.rcParams["font.sans-serif"] = ["SimHei"]
|
||||
|
||||
# 初始化一个起始日期(这里假设是今天凌晨0点)
|
||||
start_date = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
|
||||
with open('minicom.cap', 'r') as file:
|
||||
lines = file.readlines()
|
||||
|
||||
times = []
|
||||
temperatures = []
|
||||
|
||||
for line in lines:
|
||||
time_str, temp_str = line.strip().split('temp = ')
|
||||
time = start_date + timedelta(seconds=int(time_str.split(':')[2])) + \
|
||||
timedelta(minutes=int(time_str.split(':')[1])) + \
|
||||
timedelta(hours=int(time_str.split(':')[0]))
|
||||
temperature = float(temp_str)
|
||||
times.append(time)
|
||||
temperatures.append(temperature)
|
||||
|
||||
# 确保时间序列是排序的
|
||||
times.sort()
|
||||
|
||||
# 绘制折线图
|
||||
plt.plot(times, temperatures)
|
||||
plt.xlabel('时间')
|
||||
plt.ylabel('温度 (摄氏度)')
|
||||
plt.title('温度随时间变化图')
|
||||
plt.xticks(rotation=45) # 旋转x轴标签以更好地展示时间
|
||||
|
||||
# 自动调整x轴刻度以显示日期时间格式
|
||||
plt.gcf().autofmt_xdate()
|
||||
|
||||
# 显示图形
|
||||
plt.savefig("img.png")
|
||||
plt.show()
|
1
requirements.txt
Normal file
1
requirements.txt
Normal file
@ -0,0 +1 @@
|
||||
matplotlib==3.8.2
|
17
src/main.c
17
src/main.c
@ -28,6 +28,7 @@ void main(void)
|
||||
int pre_temp_value=0;
|
||||
int temp_value=0;
|
||||
u8 temp_buf[5];
|
||||
int HH=0, MM=0, SS=0;
|
||||
|
||||
uart_init(0XFA);//波特率为9600
|
||||
ds18b20_init();//初始化DS18B20
|
||||
@ -59,8 +60,20 @@ void main(void)
|
||||
// }
|
||||
|
||||
// delay
|
||||
delay_ms(1000);
|
||||
printf("temp = %.1f\r\n", temp_value/10.0);
|
||||
if(temp_value != 0) {
|
||||
SS ++;
|
||||
if (SS == 60){
|
||||
SS = 0;
|
||||
MM ++;
|
||||
}
|
||||
if(MM == 60){
|
||||
MM = 0;
|
||||
HH ++;
|
||||
}
|
||||
delay_ms(1000);
|
||||
printf("%02d:%02d:%02d temp = %.1f\r\n", HH, MM, SS, temp_value/10.0);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user