wpf 如何自动检测串行COM端口的连接c#

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19567324/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:56:40  来源:igfitidea点击:

How to autodetect connection of serial COM port c#

c#wpfevent-handlingserial-port

提问by Martin Gabriel

I have application to communication with device. Device is connected through serial COM port. My app can comunicate with device.

我有与设备通信的应用程序。设备通过串口连接。我的应用程序可以与设备通信。

I need some method / event, that can scan COM ports through running app. When I'll connect device to PC - method / event will print MessageBox with message "connected", or something like that.

我需要一些方法/事件,可以通过运行应用程序扫描 COM 端口。当我将设备连接到 PC 时 - 方法/事件将打印带有消息“已连接”或类似内容的 MessageBox。

I found something like this:

我发现了这样的事情:

comPort.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);

But it doesnt work.

但它不起作用。

回答by Mike Dinescu

I'm not sure if you are trying to auto-detect which port a device is connected to, or auto-detect whether a device is connected to a specific port.

我不确定您是在尝试自动检测设备连接到哪个端口,还是自动检测设备是否连接到特定端口。

In both cases though, the principle is the same:

但在这两种情况下,原理是相同的:

  1. you enumerate the the serial ports using: SerialPort.GetPortNamesif you need to determine the port, or skip to the Step 2 if you already know the port
  2. for each port in the collection you open a connection by creating a new SerialPortobject for it, and by calling Open
  3. for each open connection you attempt to write/read from the port the sequence of data that determines whether your device is attached
  4. for each open connection if the data read times out then there is no device attached to the port; otherwise, if you get back what you expect you know your device is attached
  5. for each port, close the connection using Closeon the SerialPortobject.
  1. 您可以使用以下方法枚举串行端口:SerialPort.GetPortNames如果您需要确定端口,或者如果您已经知道端口,请跳到步骤 2
  2. 对于集合中的每个端口,您通过SerialPort为它创建一个新对象并调用Open 来打开一个连接
  3. 对于每个打开的连接,您尝试从端口写入/读取确定您的设备是否已连接的数据序列
  4. 对于每个打开的连接,如果数据读取超时,则没有设备连接到端口;否则,如果您得到预期的结果,您就知道您的设备已连接
  5. 对于每个端口,在对象上使用Close 关闭连接SerialPort

Performing the above at any given point will tell you whether your device is attached at that point, and which port it is attached to.

在任何给定点执行上述操作将告诉您您的设备是否在该点连接,以及它连接到哪个端口。

If you need to do presence detection continuously, then you will probably want to create a timer and perform this test periodically (every 30 seconds, or every 2 minutes - depending on the latency you are willing to accept).

如果您需要连续进行存在检测,那么您可能需要创建一个计时器并定期执行此测试(每 30 秒或每 2 分钟 - 取决于您愿意接受的延迟)。

NOTE

笔记

As others have indicated in the answers, you will want to run the serial port detection code asynchronously so as not to block your main application while scanning the ports. The scanning is guaranteed to take a while because of the time-outs of the ports that have no device attached.

正如其他人在答案中指出的那样,您将希望异步运行串行端口检测代码,以免在扫描端口时阻塞您的主应用程序。由于没有连接设备的端口超时,扫描肯定需要一段时间。