運行上位機,十秒不到就崩,不知道怎么解決?求助
原碼如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 上位機demo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Thread th = new Thread(Serial);
th.IsBackground = true;
th.Start();
Control.CheckForIllegalCrossThreadCalls = false;
System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
string s = " ";
textBox1.Text = s;
}
private void button1_Click(object sender, EventArgs e)
{
try
{
if (serialPort1.IsOpen)
{
serialPort1.Close();
button1.Text = "打開串口";
}
else
{
serialPort1.PortName = comboBox1.Text;
serialPort1.BaudRate = 115200; //波特率
serialPort1.DataBits = 8; //數據位
serialPort1.Parity = System.IO.Ports.Parity.None;
serialPort1.StopBits = System.IO.Ports.StopBits.One;
serialPort1.Open(); //打開串口
button1.Text = "關閉串口";
}
}
catch
{
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 = "打開串口";
}
}
//串口掃描
void Serial(object o)
{
Control.CheckForIllegalCrossThreadCalls = false;
if (comboBox1.Text == "")
{
comboBox1.Items.AddRange(System.IO.Ports.SerialPort.GetPortNames());
}
}
int listCount = 0;
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
try
{
string str;
string data = string.Empty;
this.Invoke((EventHandler)(delegate
{
data = serialPort1.ReadExisting();
str = data.Substring(0, 1);
// 1/10/20/30/40/1/1/1
string[] info = data.Split('/');
//下位機數據發送格式
//將數據顯示
// textBox1.Text = info[1];
//textBox2.Text = info[2];
if (str == "2")
{
ListViewItem item = new ListViewItem(); //先實例化ListViewItem這個類
listCount++;
string s1 = listCount.ToString();
item.Text = s1; //序號
//item.SubItems.Add(Convert.ToString((Convert.ToInt32(info[1]) + Convert.ToInt32(info[3])) / 2)); //溫度1
//item.SubItems.Add(Convert.ToString((Convert.ToInt32(info[2]) + Convert.ToInt32(info[4])) / 2)); //溫度1
item.SubItems.Add(info[5]); //煙霧濃度
item.SubItems.Add(info[6]); //存在火焰
int cmd = Convert.ToInt32(info[1]);
if (cmd != 0)
{
textBox1.Text = info[1] + "℃";//溫度1
textBox2.Text = info[2] + "%RH";//濕度1
}
textBox3.Text = info[3] + "lx";//光強
}
if (str == "1")
{
ListViewItem item = new ListViewItem(); //先實例化ListViewItem這個類
listCount++;
string s1 = listCount.ToString();
item.Text = s1; //序號
//item.SubItems.Add(Convert.ToString((Convert.ToInt32(info[1]) + Convert.ToInt32(info[3])) / 2)); //溫度1
//item.SubItems.Add(Convert.ToString((Convert.ToInt32(info[2]) + Convert.ToInt32(info[4])) / 2)); //溫度1
item.SubItems.Add(info[5]); //煙霧濃度
item.SubItems.Add(info[6]); //存在火焰
textBox4.Text = info[4] + "%";//煙霧濃度
int cmf = Convert.ToInt32(info[2]);
if (cmf < 10000)
{
textBox5.Text = info[5] + "μg/m3";//PM2.5} }
}
}
}
)
);
}
catch (Exception ex)
{
//響鈴并顯示異常給用戶
System.Media.SystemSounds.Beep.Play();
MessageBox.Show(ex.Message);
}
}
int listCount1 = 0;
private void serialPort2_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
try
{
string str;
string data = string.Empty;
this.Invoke((EventHandler)(delegate
{
data = serialPort1.ReadExisting();
str = data.Substring(0, 1);
// 1/10/20/30/40/1/1/1
string[] info = data.Split('/');
//下位機數據發送格式
//將數據顯示
// textBox1.Text = info[1];
//textBox2.Text = info[2];
if (str == "2")
{
ListViewItem item = new ListViewItem(); //先實例化ListViewItem這個類
listCount1++;
string s1 = listCount.ToString();
item.Text = s1; //序號
//item.SubItems.Add(Convert.ToString((Convert.ToInt32(info[1]) + Convert.ToInt32(info[3])) / 2)); //溫度1
//item.SubItems.Add(Convert.ToString((Convert.ToInt32(info[2]) + Convert.ToInt32(info[4])) / 2)); //溫度1
item.SubItems.Add(info[6]); //煙霧濃度
item.SubItems.Add(info[7]); //存在火焰
textBox1.Text = info[1] + "℃";//溫度1
textBox2.Text = info[2] + "%RH";//濕度1
//textBox6.Text = info[3] + "℃";//溫度2
// textBox7.Text = info[4] + "%RH";//濕度2
textBox3.Text = info[3] + "lx";//光強
textBox4.Text = info[4] + "%";//煙霧濃度
textBox5.Text = info[5] + "μg/m3";//PM2.5
}
}
)
);
}
catch (Exception ex)
{
//響鈴并顯示異常給用戶
System.Media.SystemSounds.Beep.Play();
MessageBox.Show(ex.Message);
}
}
private void textBox6_TextChanged_1(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void groupBox3_Enter(object sender, EventArgs e)
{
}
private void textBox6_TextChanged(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void groupBox3_Enter_1(object sender, EventArgs e)
{
}
private void label14_Click(object sender, EventArgs e)
{
}
private void textBox4_TextChanged(object sender, EventArgs e)
{
}
private void textBox5_TextChanged(object sender, EventArgs e)
{
}
}
}
|