You can show this interface first
It can display the data from MCU , In the form of a thermometer , The next generation of the host computer code
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 Temperature { 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" };
// Add baud rate list comboBox2.Items.AddRange(baud); comboBox2.Text = "115200";// Set default value
comboBox1.Items.AddRange(System.IO.Ports.SerialPort.GetPortNames());
// Get the current computer available serial port and add it to the options list comboBox3.Text = "1"; comboBox4.Text = "8"; comboBox5.Text
= " nothing "; serialPort1.DataReceived += new SerialDataReceivedEventHandler(
serialPort1_DataReceived);// Add event handler serialPort1.Encoding = Encoding.Default;
// Set encoding , Avoid garbled Chinese label25.Text = " " + DateTime.Now.ToString(); } private void
button1_Click(object sender, EventArgs e) { try { if (serialPort1.IsOpen) {
serialPort1.Close();// Close the serial port button1.Text = " Open serial port "; button1.ForeColor = Color.
Black; ovalShape1.FillColor = Color.Black; comboBox1.Enabled = true; comboBox2.
Enabled= true; comboBox3.Enabled = true; comboBox4.Enabled = true; comboBox5.
Enabled= true; } else { // The serial port has been closed , After setting the serial port property, open the 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 = " Close the serial port "; button1.ForeColor =
Color.Red; ovalShape1.FillColor = Color.Red ; } } catch (Exception ex) {
// Capture and handle possible exceptions // Exception caught , Create a row object , The former can not be reused serialPort1 = new System.IO.Ports.
SerialPort(); // Refresh COM mouth comboBox1.Items.Clear(); comboBox1.Items.AddRange(System.
IO.Ports.SerialPort.GetPortNames()); // Ring and display the exception to the user System.Media.SystemSounds.Beep
.Play(); button1.Text = " Open serial port "; MessageBox.Show(ex.Message ); comboBox1.Enabled
= true; comboBox2.Enabled = true; comboBox3.Enabled = true; comboBox4.Enabled =
true; comboBox5.Enabled = true; } } // Receive serial data function private void
serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e) { Thread.
Sleep(500);// dormancy 50ms try { // Define a field , To save the information from the serial port string str = serialPort1 .
ReadExisting();// Read in string mode int length = serialPort1.BytesToRead;// Length of read data Byte[]
data= new byte[length];// Define buffer , Because the serial port time trigger may be more than one byte serialPort1.Read(data, 0,
length);// Read data 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(" Data sending error "); }
//label14.Text = str; //thermometer1.Value = Convert.ToInt32 (data [0])
;//1130 If you use it this way, it will go beyond the bounds of the array // foreach (byte Member in data) // Ergodic usage // { // string
str = Convert.ToString(Member, 16).ToUpper(); // textBox1.AppendText("0x" +
(str.Length == 1 ? "0" + str : str) + " "); // Text box display 1 // The last sentence is equivalent to 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(" No data saved !", " error "); return; } SaveFileDialog SaveFile = new
SaveFileDialog(); SaveFile.Filter = "All files(*.*)|*.*|txt files(*.txt)|*.txt";
// Text filtering SaveFile.FilterIndex = 3;// Text filter index , The first choice is 1 SaveFile.RestoreDirectory =
true; if(SaveFile .ShowDialog ()==DialogResult.OK ) { resultFile = SaveFile.
FileName; StreamWriter sw = File.CreateText(resultFile); sw.Write(textBox1.Text)
; sw.Close(); } } } }
The program of the lower computer can be obtained from my resource page
Technology