串口重新连接 [英] Serial port reconnecting

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

问题描述

我将RS232电缆从计算机中取出了.

我运行我的应用程序并尝试连接到端口.

当然,连接失败.

我将RS232电缆连接到计算机.

我尝试再次连接端口.

再次,我失败了.

我现在可以连接的唯一方法是关闭应用程序,然后再次运行.

我似乎无法清洁某些东西,但是我不知道该怎么办...

我的关闭函数如下:

公开

{

",

{

{

m_Port.Close();

}

m_Port.Dispose();

m_Port =

例外 例如)

{

System.Windows.Forms.

+ ex.StackTrace); 解决方案

不要检查它是否打开.将其关闭并忽略该异常.那就是"我将RS232电缆从计算机中取出了. 我运行我的应用程序并尝试连接到端口. 当然,连接会失败. 我将RS232电缆连接到了计算机. 我尝试再次连接端口. 再次,我失败了."

关闭端口并忽略异常,您应该能够打开端口.


Hi,

My application need to communicate a periferial using RS-232. My computer is connected to the periferial by RS232-to-USB of Prolific.

I’m trying to reconnect after the following scenario:

I take the RS232 cable out of the computer.

I run my application and try to connect to the port.

Of course, the connection fails.

I connect the RS232 cable to the computer.

I try to connect the port again.

And again, I fail.

The only way I can connect now is by closing the application and running it again.

It looks like I fail to clean something, but I don't know what...

My close function looks like this:

public

override void Close()

{

Log4NetWrapper.Instance.LogDebug("Rs232Communicator", "Closing", "");

try

{

if (m_Port != null && m_Port.IsOpen == true)

{

m_Port.Close();

}

m_Port.Dispose();

m_Port =

null;

}

catch (Exception ex)

{

System.Windows.Forms.

MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);

}

}

解决方案

Don't check if it's Open.  Close it and ignore the exception.  That is "I take the RS232 cable out of the computer.  I run my application and try to connect to the port.  Of course, the connection fails.  I connect the RS232 cable to the computer.  I try to connect the port again.  And again, I fail."

Close the port and ignore the exception and you should be able to Open the port.


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

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