read temperature

it2026-09-03  9

button1, button2, richtexbox1, serialport1,

 

 

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;using System.IO.Ports;

namespace serialCom{ public partial class Form1 : Form {  string datain; public Form1() { InitializeComponent(); }

private void Form1_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { serialPort1.PortName = "COM5"; //通信端口 serialPort1.BaudRate = 4800; //设置波特率 serialPort1.Open();//打开串口 }

private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e) { datain = serialPort1.ReadExisting(); this.Invoke(new EventHandler(DisplayText)); }

//定义返回数据函数 private void DisplayText(object sender, EventArgs e) { richTextBox1.Text = datain;

}

private void button2_Click(object sender, EventArgs e) { serialPort1.Close(); Close(); }

}}

 

 

*************************************************************************

2.

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;using System.Windows.Forms.DataVisualization.Charting;using System.IO;namespace sw{ public partial class Form1 : Form { Queue<string> dataQueue = new Queue<string>(100); int num =10; string datain; Boolean flag = true; public Form1() { InitializeComponent(); serialInt(); InitChart(); } private void button1_Click(object sender, EventArgs e) { serialSetInt(); this.timer1.Start(); }

private void button2_Click(object sender, EventArgs e) { if (flag == true) { this.timer1.Stop(); serialPort1.Close(); flag = false; button2.Text = "继续测量"; } else { this.timer1.Start(); serialPort1.Open(); flag = true; button2.Text = "暂停测量"; } } private void button4_Click(object sender, EventArgs e) { this.timer1.Stop(); Close(); }

private void timer1_Tick(object sender, EventArgs e) { this.chart1.Series[0].Points.Clear();

updateQueue(); for (int i = 0; i < dataQueue.Count; i++) { this.chart1.Series[0].Points.AddXY(i, dataQueue.ElementAt(i));

} }

private void updateQueue() {

if (dataQueue.Count > 90) { //先出列 for (int i = 0; i < num; i++) { dataQueue.Dequeue(); } } for (int i = 0; i < num; i++) {

dataQueue.Enqueue(datain);

} this.chart1.ChartAreas[0].AxisY.Maximum = Convert.ToDouble(dataQueue.Max()) + 5; //this.chart1.ChartAreas[0].AxisY.Minimum = Convert.ToDouble(dataQueue.Min()) +5;

}

private void serialSetInt() { serialPort1.PortName = comboBox1.Text; serialPort1.Open(); serialPort1.BaudRate = int.Parse(comboBox2.Text);

} private void serialInt() { string[] portList = System.IO.Ports.SerialPort.GetPortNames(); for (int i = 0; i < portList.Length; ++i) { string name = portList[i]; comboBox1.Items.Add(name); }

string[] BaudRateList = { "1200","2400", "4800", "9600","19200","38400","57600","115200" }; for (int i = 0; i < BaudRateList.Length; i++) { comboBox2.Items.Add(BaudRateList[i]); }

}

private void InitChart() { //定义图表区域 this.chart1.ChartAreas.Clear(); ChartArea chartArea1 = new ChartArea( ); this.chart1.ChartAreas.Add(chartArea1); //定义存储和显示点的容器 this.chart1.Series.Clear(); Series series1 = new Series( ); this.chart1.Series.Add(series1); //设置图表显示样式 this.chart1.ChartAreas[0].AxisY.Minimum = 260; this.chart1.ChartAreas[0].AxisY.Maximum =360; this.chart1.ChartAreas[0].AxisX.Interval = 5; this.chart1.ChartAreas[0].AxisX.MajorGrid.LineColor = System.Drawing.Color.Silver; this.chart1.ChartAreas[0].AxisY.MajorGrid.LineColor = System.Drawing.Color.Silver; this.chart1.Series[0].Color = Color.Red; this.chart1.Series[0].ChartType = SeriesChartType.Line;

}

private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) { datain= serialPort1.ReadExisting(); }

}}

***********************************************************************

3.

转载于:https://www.cnblogs.com/https/p/9232165.html

最新回复(0)