好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

C# 串口接收数据中serialPort.close()死锁的实例

最近在做一个有关高铁模拟仓显示系统的客户端程序,在这个程序中要运用串口serialPort传输数据,因为每次接收数据结束后要更新UI界面,所以就用到了的Invoke,将更新UI的程序代码封装到一个方法中,然后通过Incoke调用,程序跑起来没有任何问题,但是当你执行serialPort.close()是程序就会发生死锁,整个程序卡在那里动都动不了。

上网查了很多资料,有各种这样的说法,有的说定义一个接收数据的标志,如果在执行关闭程序是进行判断,如果数据接收完了就关闭串口,没有的话继续执行,但是经过亲自测试并没有什么卵用,最后自己研究invoke的时候发现还有Begininvoke,同时也发现了他们之间的不同,begininvoke用于后台更新UI数据无需等待的情况,而invoke用于后台更新UI数据无需要等待的情况,弄明白这两个之间的不同之后才明白原来执行serialPort.close()发生死锁的原因就是invoke在作祟,改成begininvoke就不会出现死锁问题。

直接上代码:

?

SerialPort serialPort1 = new SerialPort([COM5], 115200, Parity.None, 8, StopBits.One); //初始化串口设置

//定义委托

public delegate void Displaydelegate(byte[] InputBuf);

Byte[] OutputBuf = new Byte[8];

public Displaydelegate disp_delegate;

 

//接收数据委托

disp_delegate = new Displaydelegate(DispUI);

serialPort1.DataReceived += new SerialDataReceivedEventHandler(Comm_DataReceived);

 

//串口读取数据处理函数

public void Comm_DataReceived(object sender, SerialDataReceivedEventArgs e)

{

 

Byte[] InputBuf = new Byte[8];

 

try

{

serialPort1.Read(InputBuf, 0,6); //读取缓冲区的数据,每次读取6个字节的数据

System.Threading.Thread.Sleep(100);

this.BeginInvoke(disp_delegate, InputBuf);//disp_delegate是定义的委托事件,在委托事件中调用修改UI的程序

}

catch (TimeoutException ex) //超时处理

{

MessageBox.Show(ex.ToString());

}

 

}

 

//更新UI界面

public void DispUI(byte[] InputBuf)

{

 

string str = System.Text.Encoding.Default.GetString(InputBuf);

// Console.WriteLine(str);

string strW = str.Substring(0, 2);//截取str的子串,从index=0开始截取长度为2的字符串

int OutStrW = int.Parse(strW);

string strS = str.Substring(2, 2);//截取str的子串,从index=2开始截取长度为2的字符串

int OutStrS = int.Parse(strS);

OutstrWen = (OutStrW - 4).ToString();

textBox8.Text = strW;

textBox9.Text = (OutStrW - 4).ToString();

textBox10.Text = strS;

textBox11.Text = (OutStrS - 10).ToString();

}

以上这篇C# 串口接收数据中serialPort.close()死锁的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:http://HdhCmsTestcnblogs测试数据/hexiaobao/archive/2017/11/28/7909103.html

dy("nrwz");

查看更多关于C# 串口接收数据中serialPort.close()死锁的实例的详细内容...

  阅读:50次