串行端口电缆未插入

发布于 2024-07-26 04:09:03 字数 47 浏览 5 评论 0原文

在.Net的SerialPort对象中,有没有办法确定串行端口是否被拔出或插入。

In .Net's SerialPort object, is there a way to determine if a serial port gets unplugged or plugged in.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

旧城空念 2024-08-02 04:09:03

与 USB 不同,串行端口没有任何内置方法来检测链路状态的物理变化。 可以使用某些引脚(即 DTR、DSR,有时还有 DCD)来完成有限形式的“设备就绪/未就绪”信号,但这似乎并不完全是您正在寻找的(它不是内置的) RS232 - 设备必须支持它,您主要用它来与调制解调器通信。)

所以,简而言之:不,在一般情况下。 如果您知道/可以对您尝试与之通信的设备进行编程,并且您知道它会将某条线保持在高电平(例如),您可以轮询它以查找该线变高。 但如果你插入的设备没有经过编程来执行类似的可预测操作,那么就真的无从得知。 (某些设备默认情况下可能会将 DSR 保持在高水平,但这绝不是一个确定的选择。)

Unlike USB, the serial port does not have any built-in way to detect a physical change in link status. A limited form of "device ready/not ready" signalling can be done using some of the pins (namely DTR, DSR, and sometimes DCD), but this doesn't seem like exactly what you're looking for (it's not built in to RS232 -- the device must support it, you mainly use it to talk to modems.)

So, in short: no, in the general case. If you know/can program the device you're trying to communicate with, and you know that it will hold a certain line high (for example), you could poll it looking for that line to go high. But if you plug in a device which isn't programmed to do something predictable like that, then there's really no way to tell. (Some devices may hold DSR high by default but it's in no way a sure bet.)

荆棘i 2024-08-02 04:09:03

大多数串行设备对查询都有某种类型的确认响应。 只需发送一个简单的查询并等待响应。 如果您没有收到它,则设备不存在或至少没有响应。

Most serial devices have some type of ack response to a query. just send a simple query and wait for response. If you don't get it, the device is not there or at least not responding.

邮友 2024-08-02 04:09:03

我还没有尝试过,但看看 SerialPort.PinChanged 事件和 DsrChanged< /a>.

每当有任何正常设备插入串行端口并打开时,我都希望该端口的 DSR 引脚被置位; 相反,如果该设备被拔掉,或者当它被关闭时,那么我预计 DSR 引脚状态会改变/下降。


各个引脚的通常含义是:

  • DSR:设备已插入并打开
  • CTS:设备已准备好接收数据(即使设备已插入,例如当设备的板载缓冲区有限时,该状态也可能会关闭)并使用此引脚来控制来自 PC 的数据传输)
  • DCD:设备(调制解调器)已通过电话线与另一个调制解调器建立连接(因此您发送的任何内容都将被视为要传输到远程调制解调器的数据)

其中,回答你的OP的是DSR。

I haven't tried it, but look at the SerialPort.PinChanged event and DsrChanged.

Whenever there's any normal device plugged in to the serial port and switched on, then I'd expect the port's DSR pin to be asserted; and conversely if that device is unplugged, or when it's switched off, then I'd expect the DSR pin state to change/drop.


The usual meaning of the various pins is:

  • DSR: device is plugged in and switched on
  • CTS: device is ready to receive data (this may be down even when a device is plugged in, e.g. when the device has a limited on-board buffer and uses this pin to flow-control data transfer from the PC)
  • DCD: device (modem) has established a connection over the phone line to another modem (so anything you send is treated as data to be transferred to the remote modem)

Of these, the one that answers your OP is DSR.

梦里人 2024-08-02 04:09:03

一种方法可以检测.NET中的串行端口删除/插入 - 请参阅我的答案检测串口插入/拔出

There is a way to detect Serial Port removal/insertion in .NET - see my answer at Detect serial port insertion/removal

メ斷腸人バ 2024-08-02 04:09:03
    Public Class SerialPort
        Inherits IO.Ports.SerialPort

        Event Disconnected()

        Public Sub OpenWithDisconnectionevent()
            Me.Open()
            Dim t As New Threading.Thread(AddressOf ConnectivityChecker)
            t.Start()
        End Sub


        Sub ConnectivityChecker()
            Do
                If Me.IsOpen = False Then
                    RaiseEvent Disconnected()
                    GoTo ThreadExit
                End If
            Loop
ThreadExit:
        End Sub
    End Class
    Public Class SerialPort
        Inherits IO.Ports.SerialPort

        Event Disconnected()

        Public Sub OpenWithDisconnectionevent()
            Me.Open()
            Dim t As New Threading.Thread(AddressOf ConnectivityChecker)
            t.Start()
        End Sub


        Sub ConnectivityChecker()
            Do
                If Me.IsOpen = False Then
                    RaiseEvent Disconnected()
                    GoTo ThreadExit
                End If
            Loop
ThreadExit:
        End Sub
    End Class
递刀给你 2024-08-02 04:09:03

您可以检测可用的串行端口,您可以尝试在 try...catch 块内与它们进行通信。

这是c#中端口检测的示例

using System;
using System.Collections.Generic;
using System.IO.Ports;

public class MyClass
{
 public static void Main()
 {
   string[] sPorts = SerialPort.GetPortNames();
   foreach(string port in sPorts)
     WL(port);
  RL();
 }

 #region Helper methods

 private static void WL(object text, params object[] args)
 {
  Console.WriteLine(text.ToString(), args); 
 }

 private static void RL()
 {
  Console.ReadLine(); 
 }

 private static void Break()
 {
  System.Diagnostics.Debugger.Break();
 }

 #endregion
}

you can detect the availables serial ports, the you can try to comunicate with them inside a try...catch block.

This is an example of ports detection in c#

using System;
using System.Collections.Generic;
using System.IO.Ports;

public class MyClass
{
 public static void Main()
 {
   string[] sPorts = SerialPort.GetPortNames();
   foreach(string port in sPorts)
     WL(port);
  RL();
 }

 #region Helper methods

 private static void WL(object text, params object[] args)
 {
  Console.WriteLine(text.ToString(), args); 
 }

 private static void RL()
 {
  Console.ReadLine(); 
 }

 private static void Break()
 {
  System.Diagnostics.Debugger.Break();
 }

 #endregion
}
一片旧的回忆 2024-08-02 04:09:03

这取决于您使用哪种电缆连接哪种设备。

最好的选择是尝试处理 PinChanged 事件处理程序。

有些设备在连接和打开时会提高 DSR,有些设备会提高 CTS,有些设备会使用这些设备进行握手。

It will depend on what kind of device you connect with what kind of cable.

Your best bet is to try handling the PinChanged event handler.

Some devices will raise DSR when connected and switched on, others CTS, others will use these for handshaking.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文