C# Serialport执行close()方法时,程序卡死的解决办法

根据网上搜到的文章,程序中添加两个bool变量,作为状态标记,保证串口关闭时,串口事件已处理完

private volatile bool is_serial_listening = false;//串口正在监听标记
private volatile bool is_serial_closing = false;//串口正在关闭标记

 1 //Program Begins
 2 
 3 using System;
 4 using System.Collections.Generic;
 5 using System.ComponentModel;
 6 using System.Data;
 7 using System.Drawing;
 8 using System.Linq;
 9 using System.Text;
10 using System.Windows.Forms;
11 using System.IO.Ports; //使用Serialport需要用到这个namespace
12 
13 namespace commtest
14 {
15     public partial class Form1 : Form
16     {
17         private string DispString;   //used to store the values read
18         private volatile bool is_serial_listening = false;//串口正在监听标记
19         private volatile bool is_serial_closing = false;//串口正在关闭标记
20         public Form1()
21         {
22             InitializeComponent();
23         }
24 
25         private void Form1_Load(object sender, EventArgs e)
26         {
27             //Port name can be identified by checking the ports
28             // section in Device Manager after connecting your device
29             serialPort1.PortName = "COM3";
30             //Provide the name of port to which device is connected
31 
32             //default values of hardware[check with device specification document]
33             serialPort1.BaudRate = 9600;
34             serialPort1.Parity = Parity.None;
35             serialPort1.StopBits = StopBits.One;
36             serialPort1.Handshake = Handshake.None;
37 
38             serialPort1.Open(); //opens the port
39             serialPort1.ReadTimeout = 200;
40             if (serialPort1.IsOpen)
41             {
42                 DispString = "";
43                 //txtCardKeyDeactivate.Text = "";
44             }
45             serialPort1.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived);
46         }
47 
48         private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
49         {
50             if (is_serial_closing)
51             {
52                 is_serial_listening = false; //准备关闭串口时,reset串口侦听标记
53                 return;
54             }
55             try
56             {
57                 if (serialPort1.IsOpen)
58                 {
59                     is_serial_listening = true;
60                     DispString = serialPort1.ReadExisting();
61                     this.Invoke(new EventHandler(DisplayText));
62                     Byte[] BSendTemp = new Byte[1];
63                     BSendTemp[0] = 0x00;
64                     serialPort1.Write(BSendTemp, 0, 1);
65                 }
66             }
67             finally
68             {
69                 is_serial_listening = false;//串口调用完毕后,reset串口侦听标记
70             }
71         }
72 
73         private void DisplayText(object sender, EventArgs e)
74         {
75             textBox1.AppendText(DispString);
76             textBox2.Text = SubstringCount(textBox1.Text, "ok").ToString();//自己写的测试语句,统计"ok"字符串出现的次数
77         }
78         private static int SubstringCount(string str, string substring)
79         {
80             if (str.Contains(substring))
81             {
82                 string strReplaced = str.Replace(substring, "");
83                 return (str.Length - strReplaced.Length) / substring.Length;
84             }
85 
86             return 0;
87         }
88 
89         private void Form1_FormClosing(object sender, FormClosingEventArgs e)
90         {
91             is_serial_closing = true;//关闭窗口时,置位is_serial_closing标记
92             while (is_serial_listening) Application.DoEvents();
93             serialPort1.Close();
94         }
95     }
96 }

 

posted @ 2015-01-22 08:23  feifeivictor  阅读(3382)  评论(1编辑  收藏  举报