【问题标题】:C# Serial Port IssueC# 串口问题
【发布时间】:2013-08-23 12:10:20
【问题描述】:

我正在尝试创建一个类似于 Hyperterminal 的终端来与 GSM 调制解调器通信(打开 AT Sierra 无线 sl8080)。调制解调器可以随机复位或通过发送到通信端口的命令来复位。当调制解调器重置时,所有通信都将丢失。我正在使用

private void serialPort1_DataReceived(object sender,System.IO.Ports.SerialDataReceivedEventArgs e)
{
    Console.WriteLine(serialPort1.ReadExisting());
}

从设备中读取。但是重置后我可以做任何事情而什么也得不到。

如果我在重置后尝试阅读,我会得到:

A first chance exception of type 'System.InvalidOperationException' occurred in System.dll
An unhandled exception of type 'System.InvalidOperationException' occurred in System.dll
Additional information: The port is closed.

如果我重置,则关闭我得到的端口:

A first chance exception of type 'System.IO.IOException' occurred in System.dll
An unhandled exception of type 'System.IO.IOException' occurred in System.dll
Additional information: The requested resource is in use.

如果不完全关闭应用程序,我无法从这种状态中恢复。一年多来,我一直在努力解决这个问题。我想出了很多其他的解决方案。主要使用 Python 和 Java/RXTX,它们可以完美地处理设备的重置。我真的很难过,现在要解决这个问题更像是个人挑战。

我尝试使用 WMI 检测 USB 设备的拔出并关闭 commport,但结果仍然相同。

【问题讨论】:

    标签: c# resources serial-port wmi gsm


    【解决方案1】:

    您是否尝试过释放 SerialPort 对象而不是关闭它?

    http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.errorreceived.aspx

    如果 SerialPort 对象在读取操作期间被阻塞,请不要中止线程。相反,要么关闭基本流,要么处置 SerialPort 对象。

    【讨论】:

    • 我有,结果也一样。总是必须关闭应用程序,有时会从设备上拔下 USB,以免设备运行不正常。
    • 对象是否已经被释放?我还发现了你可能已经看过的这篇文章。 .net 似乎有些错误。 (social.msdn.microsoft.com/Forums/vstudio/en-US/…)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-26
    • 1970-01-01
    • 1970-01-01
    • 2020-04-12
    • 1970-01-01
    相关资源
    最近更新 更多