运行上位机,十秒不到就崩,不知道怎么解决?求助
原码如下:
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)
{
}
}
}
|