新增数据处理脚本,可以画折线图

This commit is contained in:
fengbh 2024-01-15 00:45:08 +08:00
parent b74569668e
commit bd8d7778b0
4 changed files with 69 additions and 2 deletions

View File

@ -38,6 +38,17 @@ uart_temp是用于8051单片机上的一个项目用于获取温度并使
4. 打开串口软件,接收来自单片机的数据 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) [从Keil迁移到SDCC](https://blog.csdn.net/weixin_44681954/article/details/135286404)

42
process.py Normal file
View 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
View File

@ -0,0 +1 @@
matplotlib==3.8.2

View File

@ -28,6 +28,7 @@ void main(void)
int pre_temp_value=0; int pre_temp_value=0;
int temp_value=0; int temp_value=0;
u8 temp_buf[5]; u8 temp_buf[5];
int HH=0, MM=0, SS=0;
uart_init(0XFA);//波特率为9600 uart_init(0XFA);//波特率为9600
ds18b20_init();//初始化DS18B20 ds18b20_init();//初始化DS18B20
@ -59,8 +60,20 @@ void main(void)
// } // }
// delay // delay
delay_ms(1000); if(temp_value != 0) {
printf("temp = %.1f\r\n", temp_value/10.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);
}
} }
} }