基于C#實(shí)現(xiàn)一個(gè)溫濕度監(jiān)測(cè)小工具
概述
這一章節(jié),我們主要實(shí)現(xiàn)的功能是為軟件增加實(shí)時(shí)顯示曲線。
winform在4.x版本的時(shí)候,還有charts組建,到了5.0時(shí)代,沒(méi)有了原生的charts組件
我這邊選擇第三方曲線包。ScottPlot
安裝插件包
比較簡(jiǎn)單,已經(jīng)提了很多次了。工具,nuget包管理。搜索scottplot,之后點(diǎn)擊安裝
注意安裝的是scottplot.winform

修改界面,增加曲線顯示
在表格下方,增加一個(gè)曲線。在工具欄中搜索plot

如果沒(méi)有搜索到,可以重新啟動(dòng)一下vs修改完畢的界面如圖

修改代碼
增加曲線初始化
具體的曲線配置我們可以參考scottplot的demo,這里我直接給出code
本次窗口比較小,因此我禁止了x軸的顯示
/// <summary>
/// 初始化曲線
/// </summary>
private void InitPlot()
{
//line
formsPlot1.Plot.Style(Style.Seaborn);
formsPlot1.Plot.Title("數(shù)據(jù)曲線");
//不顯示x軸
formsPlot1.Plot.XAxis.Ticks(false);
formsPlot1.Plot.XAxis.Line(false);
formsPlot1.Plot.YAxis2.Line(false);
formsPlot1.Plot.XAxis2.Line(false);
}增加曲線刷新
scottplot的刷新目前沒(méi)有找到合適的辦法
使用了比較笨的辦法,就是先clear然后重新賦值
有什么好的辦法,歡迎交流
/// <summary>
/// 刷新曲線
/// </summary>
private void RefreshLine(DataTable ThisData)
{
try
{
if (ThisData.Rows.Count < 1)
return;
//清空
formsPlot1.Plot.Clear();
//溫度
string[] data = ThisData.AsEnumerable().Select(d => d.Field<string>("溫度")).ToArray();
double[] ys = Array.ConvertAll<string, double>(data, s => double.Parse(s));
//濕度
string[] data2 = ThisData.AsEnumerable().Select(d => d.Field<string>("濕度")).ToArray();
double[] ys2 = Array.ConvertAll<string, double>(data2, s => double.Parse(s));
formsPlot1.Plot.AddSignal(ys);
formsPlot1.Plot.AddSignal(ys2);
formsPlot1.Refresh();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + ex.StackTrace);
MyLogger._.Error(ex.Message + "\r\n" + ex.StackTrace);
}
}修改load函數(shù),增加曲線初始化
/// <summary>
/// 界面加載函數(shù)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_Load(object sender, EventArgs e)
{
MyLogger._.Debug("程序啟動(dòng)");
//掃描串口
string[] comlist = ComPort.V_ScanPort();
if (comlist.Length < 1)
{
MessageBox.Show("沒(méi)有掃描到串口,請(qǐng)檢查硬件連接");
return;
}
else
{
foreach (string name in comlist)
{
this.comboBox1.Items.Add(name);
}
//默認(rèn)
this.comboBox1.Text = comlist[0];
}
//波特率初始化
this.comboBox2.Items.Add("4800");
this.comboBox2.Items.Add("9600");
this.comboBox2.Items.Add("115200");
//默認(rèn)
this.comboBox2.Text = "9600";
//默認(rèn)地址
this.textBox1.Text = "01";
//默認(rèn)背景色
this.button1.BackColor = Color.LightGreen;
//初始化數(shù)據(jù)
InitDataTable();
InitDataGrid();
InitPlot();
}修改定時(shí)器函數(shù),增加曲線實(shí)時(shí)刷新
/// <summary>
/// 定時(shí)器回調(diào)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void timer1_Tick(object sender, EventArgs e)
{
if (Runport.IsOpen)
{
//獲取地址
byte addr = byte.Parse(this.textBox1.Text);
string ret = THSensor.ReadTHDataFromSensor(Runport, addr);
if (!string.IsNullOrEmpty(ret))
{
string temp = ret.Split('&')[0];
string humi = ret.Split('&')[1];
//入庫(kù)操作
DB_SerAPI.SaveTHData(temp, humi);
//刷新列表
string time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
if (DataBuffer != null)
{
Index++;
DataRow row = DataBuffer.NewRow();
row["ID"] = Index.ToString();
row["時(shí)間"] = time;
row["溫度"] = temp;
row["濕度"] = humi;
DataBuffer.Rows.Add(row);
}
//刷新曲線
RefreshLine(DataBuffer);
}
else
{
MessageBox.Show("無(wú)數(shù)據(jù),請(qǐng)檢查配置參數(shù)");
}
}
}測(cè)試

到目前為止,我們做的功能都是基于一個(gè)固定不變的小窗口驅(qū)完成。
后續(xù),我們?yōu)榱私o工具增加歷史數(shù)據(jù)查詢(xún),報(bào)警設(shè)置等基本的功能。需要對(duì)界面進(jìn)行重新的布局和設(shè)計(jì)。
下一節(jié),我們將主要修改布局,是窗體可以自適應(yīng)分辨率。
到此這篇關(guān)于基于C#實(shí)現(xiàn)一個(gè)溫濕度監(jiān)測(cè)小工具的文章就介紹到這了,更多相關(guān)C#溫濕度監(jiān)測(cè)工具內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#觀察者模式(Observer Pattern)實(shí)例教程
這篇文章主要介紹了C#觀察者模式(Observer Pattern),主要以一個(gè)實(shí)例的形式講述了C#觀察者模式的實(shí)現(xiàn)過(guò)程,詳細(xì)講述了接口的定義、通知及動(dòng)作的實(shí)現(xiàn)方法,需要的朋友可以參考下2014-09-09
C# 使用Word模板導(dǎo)出數(shù)據(jù)的實(shí)現(xiàn)代碼
最近接到個(gè)需求,使用word模板導(dǎo)出數(shù)據(jù),怎么實(shí)現(xiàn)這個(gè)需求呢,今天小編通過(guò)實(shí)例代碼給大家介紹C# 使用Word模板導(dǎo)出數(shù)據(jù)的方法,感興趣的朋友一起看看吧2021-06-06
C#使用WebClient實(shí)現(xiàn)上傳下載
這篇文章介紹了C#使用WebClient實(shí)現(xiàn)上傳下載的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05

