当前位置: 移动技术网 > IT编程>开发语言>c# > C# 串口接收数据中serialPort.close()死锁的实例

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

2019年07月18日  | 移动技术网IT编程  | 我要评论
最近在做一个有关高铁模拟仓显示系统的客户端程序,在这个程序中要运用串口serialport传输数据,因为每次接收数据结束后要更新ui界面,所以就用到了的invoke,将更新

最近在做一个有关高铁模拟仓显示系统的客户端程序,在这个程序中要运用串口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()死锁的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网