可以先展示一下这个界面

可以显示从单片机传过来的数据,以温度计的形式显示,下面展示一下代上位机的代码
using System; using System.Collections.Generic; using System.ComponentModel;
using System.Data; using System.Drawing; using System.IO; using System.IO.Ports;
using System.Linq; using System.Text; using System.Threading; using System.
Threading.Tasks; using System.Windows.Forms; namespace _8个温度 { public partial
class Form1 : Form { public Form1() { InitializeComponent(); System.Windows.
Forms.Control.CheckForIllegalCrossThreadCalls = false; } private string
resultFile; //float tempreture, tempreture1; private void Form1_Load(object
sender, EventArgs e) { string[] baud = { "9600", "38400", "57600", "115200" };
//添加波特率列表 comboBox2.Items.AddRange(baud); comboBox2.Text = "115200";//设置默认值
comboBox1.Items.AddRange(System.IO.Ports.SerialPort.GetPortNames());
//获取当前电脑可用串口并添加到选项列表中 comboBox3.Text = "1"; comboBox4.Text = "8"; comboBox5.Text
= "无"; serialPort1.DataReceived += new SerialDataReceivedEventHandler(
serialPort1_DataReceived);//添加事件处理程序 serialPort1.Encoding = Encoding.Default;
//设置编码,避免中文乱码 label25.Text = " " + DateTime.Now.ToString(); } private void
button1_Click(object sender, EventArgs e) { try { if (serialPort1.IsOpen) {
serialPort1.Close();//关闭串口 button1.Text = "打开串口"; button1.ForeColor = Color.
Black; ovalShape1.FillColor = Color.Black; comboBox1.Enabled = true; comboBox2.
Enabled= true; comboBox3.Enabled = true; comboBox4.Enabled = true; comboBox5.
Enabled= true; } else { //串口已经处于关闭状态,则设置好串口属性后打开 comboBox1.Enabled = false;
comboBox2.Enabled = false; comboBox3.Enabled = false; comboBox4.Enabled = false;
comboBox5.Enabled = false; serialPort1.PortName = comboBox1.Text; serialPort1.
BaudRate= Convert.ToInt32(comboBox2.Text); serialPort1.DataBits = 8; serialPort1
.Parity = System.IO.Ports.Parity.None; serialPort1.StopBits = System.IO.Ports.
StopBits.One; serialPort1.Open(); button1.Text = "关闭串口"; button1.ForeColor =
Color.Red; ovalShape1.FillColor = Color.Red ; } } catch (Exception ex) {
//捕获可能发生异常并进行处理 //捕获到异常,创建一个行的对象,之前的不可再用 serialPort1 = new System.IO.Ports.
SerialPort(); //刷新COM口 comboBox1.Items.Clear(); comboBox1.Items.AddRange(System.
IO.Ports.SerialPort.GetPortNames()); //响铃并显示异常给用户 System.Media.SystemSounds.Beep
.Play(); button1.Text = "打开串口"; MessageBox.Show(ex.Message ); comboBox1.Enabled
= true; comboBox2.Enabled = true; comboBox3.Enabled = true; comboBox4.Enabled =
true; comboBox5.Enabled = true; } } //接收串口数据函数 private void
serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e) { Thread.
Sleep(500);//休眠50ms try { //定义一个字段,来保存串口传来的信息 string str = serialPort1 .
ReadExisting();//字符串方式读取 int length = serialPort1.BytesToRead;//读取数据的长度 Byte[]
data= new byte[length];//定义缓冲区,因为串口时间触发时有可能不止一个字节 serialPort1.Read(data, 0,
length);//读取数据 textBox1.AppendText(str); string data1 = str.Substring(0, 4);
string data2 = str.Substring(6, 4); string data3 = str.Substring(12, 4); string
data4= str.Substring(18, 4); string data5 = str.Substring(24, 4); string data6 =
str.Substring(30, 4); string data7 = str.Substring(36, 4); string data8 = str.
Substring(42, 4); try { label14.Text = data1; label15.Text = data2; label16.Text
= data3; label17.Text = data4; label18.Text = data5; label19.Text = data6;
label20.Text = data7; label22.Text = data8; thermometer1.Value = Convert.
ToDouble(data1); thermometer2.Value = Convert.ToDouble(data2); thermometer3.
Value= Convert.ToDouble(data3); thermometer4.Value = Convert.ToDouble(data4);
thermometer5.Value = Convert.ToDouble(data5); thermometer6.Value = Convert.
ToDouble(data6); thermometer7.Value = Convert.ToDouble(data7); thermometer8.
Value= Convert.ToDouble(data8); } catch { MessageBox.Show("数据发送错误"); }
//label14.Text = str; //thermometer1.Value = Convert.ToInt32 (data [0])
;//1130直接这样用所以会超出数组界限 // foreach (byte Member in data) //遍历用法 // { // string
str = Convert.ToString(Member, 16).ToUpper(); // textBox1.AppendText("0x" +
(str.Length == 1 ? "0" + str : str) + " "); //文本框显示1 //上一句等同于 if(str.Length==1)
//str = '0' + str; //else //str = str; //textBox1.AppendText("0x" + str); // } }
catch(Exception ex) { serialPort1.Close(); MessageBox.Show(ex.Message ); } }
private void button2_Click(object sender, EventArgs e) { textBox1.Text = ""; }
private void button3_Click(object sender, EventArgs e) { if(textBox1 .Text =="")
{ MessageBox.Show("没有数据保存!", "错误"); return; } SaveFileDialog SaveFile = new
SaveFileDialog(); SaveFile.Filter = "All files(*.*)|*.*|txt files(*.txt)|*.txt";
//文本筛选 SaveFile.FilterIndex = 3;//文本筛选器索引,选择第一项就是1 SaveFile.RestoreDirectory =
true; if(SaveFile .ShowDialog ()==DialogResult.OK ) { resultFile = SaveFile.
FileName; StreamWriter sw = File.CreateText(resultFile); sw.Write(textBox1.Text)
; sw.Close(); } } } }
下位机的程序可以从我的资源页获取

技术
今日推荐
下载桌面版
GitHub
百度网盘(提取码:draw)
Gitee
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:[email protected]
QQ群:766591547
关注微信