如何关闭串口 [英] how to Close serial port

查看:298
本文介绍了如何关闭串口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下用于打开串行端口的代码:

I have following code for open serial port:

comPort = new SerialPort();
comPort.DataReceived += new                      SerialDataReceivedEventHandler(comPort_DataReceived);
//set the properties of our SerialPort Object
comPort.BaudRate = int.Parse(_baudRate);
comPort.DataBits = int.Parse(_dataBits);
comPort.StopBits = (StopBits)Enum.Parse(typeof(StopBits), _stopBits);
comPort.Parity = (Parity)Enum.Parse(typeof(Parity), _parity);
comPort.PortName = _portName;
comPort.Open();


从另一种形式,我设置了所有值.
现在,当我想从另一个页面按关闭按钮时,我想关闭comport.

我用comport.close();但它不起作用.


From another form i set all values.
Now i want when i press close button from another page i want to close comport.

I use comport.close(); but it not work.

推荐答案

如我所记得的,
As I remember, this[^] answer helps me year ago.


我找到了可以解决的办法"不要关闭打开的端口,因为我从哪个表单中打开端口并设置所有值,我在处理后关闭了该表单,所以当我使用端口时,它会提供默认值,所以现在我不关闭该表单,所以我只是隐藏了该表单,所以现在可以访问所有其他形式的开放端口.
如果(comPort.IsOpen)
{
comPort.Close();
}
I found solution i can''t close port which is open because from which form i open port and set all values i closed that form after process so when ever i use port it give default value so now i an not close that form i just hide that form so now i can access open port in all other form.
if (comPort.IsOpen)
{
comPort.Close();
}


它还不是很清楚,但是听起来好像您拥有拥有" comPort对象的一种形式,以及控制它的另一种形式.然后,将一个事件处理程序添加到预订控制窗体上的btnClose.Click事件的所有者窗体中似乎是合理的.然后,此处理程序将调用comPort.Close().
It''s not terribly clear, but it sounds as if you have one form that "owns" the comPort object, and another form that controls it. It would seem reasonable, then, to add an event handler to the owner form that subscribes to the btnClose.Click event on the controlling form. This handler would then call comPort.Close().


这篇关于如何关闭串口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆