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

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

如果沒有搜索到,可以重新啟動一下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的刷新目前沒有找到合適的辦法
使用了比較笨的辦法,就是先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("程序啟動");
//掃描串口
string[] comlist = ComPort.V_ScanPort();
if (comlist.Length < 1)
{
MessageBox.Show("沒有掃描到串口,請檢查硬件連接");
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ù),增加曲線實時刷新
/// <summary>
/// 定時器回調(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];
//入庫操作
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["時間"] = time;
row["溫度"] = temp;
row["濕度"] = humi;
DataBuffer.Rows.Add(row);
}
//刷新曲線
RefreshLine(DataBuffer);
}
else
{
MessageBox.Show("無數(shù)據(jù),請檢查配置參數(shù)");
}
}
}測試

到目前為止,我們做的功能都是基于一個固定不變的小窗口驅(qū)完成。
后續(xù),我們?yōu)榱私o工具增加歷史數(shù)據(jù)查詢,報警設(shè)置等基本的功能。需要對界面進(jìn)行重新的布局和設(shè)計。
下一節(jié),我們將主要修改布局,是窗體可以自適應(yīng)分辨率。
到此這篇關(guān)于基于C#實現(xiàn)一個溫濕度監(jiān)測小工具的文章就介紹到這了,更多相關(guān)C#溫濕度監(jiān)測工具內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C# 使用Word模板導(dǎo)出數(shù)據(jù)的實現(xiàn)代碼
最近接到個需求,使用word模板導(dǎo)出數(shù)據(jù),怎么實現(xiàn)這個需求呢,今天小編通過實例代碼給大家介紹C# 使用Word模板導(dǎo)出數(shù)據(jù)的方法,感興趣的朋友一起看看吧2021-06-06

