gpt4 book ai didi

C# 串行端口 : Application hangs if there is no recipient on Write() (virtual null modem)

转载 作者:行者123 更新时间:2023-11-30 13:49:54 25 4
gpt4 key购买 nike

我正在使用 C# 开发 PC WinForms 应用程序,稍后需要连接到微 Controller 。目前,出于测试目的,我在我的计算机上创建了一个虚拟零调制解调器并将其与终端程序链接。

我使用的虚拟零调制解调器是“com0com”:http://sourceforge.net/projects/com0com/

这是 COM 端口对设置的屏幕截图: enter image description here

在我的应用程序中,我使用组件 serialPort。这是 serialPort 的设置方式:

public bool SerialPortSetup(String cp)
{
String port = cp;
int baud = 19200;
Parity parity = Parity.None;
int databit = 8;
StopBits stopbit = StopBits.One;

try
{
// Initialize serial port
serialPort1 = new SerialPort(port, baud, parity, databit, stopbit);
serialPort1.DataReceived += new SerialDataReceivedEventHandler(serialPortDataReceived);
serialPort1.Open();
return true;
}
catch (Exception e)
{
MessageBox.Show(e.Message);
return false;
}
}

串口的其他属性: enter image description here

我连接到 COM2 的终端程序设置了相同的波特率、奇偶校验、数据位、停止位等。当我将自己的应用程序连接到 COM1 并将终端连接到 COM2 时,一切正常.双向书写和阅读。

但是,当我不使用 COM2 端口时(即终端程序未打开),我的应用程序在我开始使用 serialPort1.Write(str) 的那一刻挂起,其中 str 只是一个随机字符串。从 Visual Studio 运行我的应用程序时,我只能通过停止调试来关闭它。它不像 Windows 将其标记为“无响应”。顺便说一句,我总是在写入之前检查打开的串行端口。

我需要解决这个问题。想法?

最佳答案

我知道这个问题有点老了,但我在寻找相同问题的解决方案时遇到了它。

您需要更改 com0com 中的几个设置,这些设置在此处描述 http://www.magsys.co.uk/comcap/onlinehelp/null_modem_emulator_com0com.htm .

简而言之,您需要为接收端口启用缓冲区溢出,并在发送端口上启用波特率仿真。使用 OP 图像中的对话框很容易做到这一点。

关于C# 串行端口 : Application hangs if there is no recipient on Write() (virtual null modem),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7996331/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com