.net 侦听正在使用的串行通信端口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/915904/
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-03 12:46:00  来源:igfitidea点击:

Listening to Serial Com Ports that are In use

.netserial-communication

提问by Daniel

I'm dealing with some legacy systems that are using RS232 to communicate with peripherals. I'm not very experienced with COM interfacing. I have some code that can open and use COM ports, but it can't open ports that are used by other applications. I need to black box the packets so that we can use the same protocol for updated communications.

我正在处理一些使用 RS232 与外围设备通信的旧系统。我对 COM 接口不是很有经验。我有一些可以打开和使用 COM 端口的代码,但它不能打开其他应用程序使用的端口。我需要对数据包进行黑盒处理,以便我们可以使用相同的协议进行更新的通信。

Is there any way to "middle man" incoming packets to an open COM port and detect what packets are being sent?I'm using .NET, but I'm open to any type of solution.

有没有办法将传入的数据包“中间人”到一个开放的 COM 端口并检测正在发送的数据包?我正在使用 .NET,但我愿意接受任何类型的解决方案。

(I found thisout there, but I don't think this will work for me.)

(我在那里发现了这个,但我认为这对我不起作用。)

采纳答案by Kieveli

I've used com0com - it's great for setting up virtual com ports - which doesn't help you at all.

我使用过 com0com - 它非常适合设置虚拟 com 端口 - 它根本没有帮助你。

The COM port interface is basically a 'file read'. My application throws an exception when I try to connect to a COM port that already has another instance reading from it. I'm not sure if you could try opening it as a 'read only' instead of read-write, but it's worth a try.

COM 端口接口基本上是“文件读取”。当我尝试连接到已经有另一个实例读取的 COM 端口时,我的应用程序抛出异常。我不确定您是否可以尝试将其打开为“只读”而不是读写,但值得一试。

You should be able to write a virtual com port that can fork off your data to a log file. Com0com is open-source, so you could use that as a starting point.

您应该能够编写一个可以将数据分叉到日志文件的虚拟 com 端口。Com0com 是开源的,因此您可以将其用作起点。

Another possible solution could be to pick up an rs232 splitter cableforks the serial signal to another serial port.

另一种可能的解决方案是拿起rs232 分离器电缆将串行信号分叉到另一个串行端口。

Or yet another possibility is a Serial Sniffer program(or an open source sniffer).

或者另一种可能性是串行嗅探器程序(或开源嗅探器)。

Or try the hub4com appfrom the same com0com website!

或者尝试来自同一个 com0com 网站的hub4com 应用程序

回答by Hans Passant

Is there any way to "middle man"

有没有办法“中间人”

Yes, there are many. Strongly supported in Windows through the concept of a "filter driver". Such a driver can be inserted ahead of a driver that get I/O requests and sees everything that passes by. Normally intended to alter I/O requests but also very suitable for simply monitoring the requests. Man in the middle.

是的,有很多。通过“过滤器驱动程序”的概念在 Windows 中得到强烈支持。这样的驱动程序可以插入在获取 I/O 请求并查看所有经过的驱动程序之前。通常用于更改 I/O 请求,但也非常适合简单地监视请求。中间的人。

The canonical example of such a driver is the venerable SysInternals' PortMon utility. Shows you everything that an app sends and receives to/from a serial port, including configuration and data. There are many such apps, just Google "serial port filter driver" (heavy on source code samples) and "serial port monitor".

此类驱动程序的典型示例是古老的 SysInternals 的 PortMon 实用程序。向您显示应用程序向/从串行端口发送和接收的所有内容,包括配置和数据。有很多这样的应用程序,只有谷歌“串口过滤驱动程序”(大量的源代码示例)和“串口监视器”。

One footnote with this, you do tend to have a problem on a 64-bit version of Windows. The vast majority of these apps, including PortMon, only work on the 32-bit version. The 64-bit version only allows certified drivers to be installed, there is very little money in selling these apps to justify the expense. Beware of this when you shop.

与此相关的一个脚注是,您在 64 位版本的 Windows 上确实会遇到问题。这些应用程序中的绝大多数,包括 PortMon,仅适用于 32 位版本。64 位版本只允许安装经过认证的驱动程序,出售这些应用程序的钱很少来证明费用的合理性。购物时要注意这一点。

回答by R Ubben

I have been down this same path. A hardware splitter is the easiest solution.

我一直在这条相同的道路上。硬件分离器是最简单的解决方案。

hub4com setup will involve the "Add New Hardware" wizard. If you have a lot of machines, geographically separated machines, or users who are not technically savvy and lack the permissions necessary, installation could be awkward.

hub4com 设置将涉及“添加新硬件”向导。如果您有很多机器、地理上分开的机器,或者不精通技术且缺乏必要权限的用户,安装可能会很尴尬。

If this is a legacy application, does it run in ntvdm? If so, you could run it in DosBoxinstead and alter the DosBox code to write to a file in addition to sending/receiving to/from the serial port. DosBox is cross platform as well.

如果这是一个遗留应用程序,它是否在ntvdm 中运行?如果是这样,您可以改为在DosBox 中运行它并更改 DosBox 代码以写入文件,除了向/从串行端口发送/接收。DosBox 也是跨平台的。

回答by Ken

You could also use TCPcomto convert the data to Ethernet packets and monitor it with Wireshark - and also broadcast it elsewhere. You then use another instance of TCPcom to forward it to any com port you like - including a virtual com port. Now you have essentially hiHymaned the data via Ethernet. https://sourceforge.net/projects/combytcp/?source=directory

您还可以使用TCPcom将数据转换为以太网数据包并使用 Wireshark 对其进行监控 - 并在其他地方广播它。然后,您使用 TCPcom 的另一个实例将其转发到您喜欢的任何 com 端口 - 包括虚拟 com 端口。现在您基本上已经通过以太网劫持了数据。 https://sourceforge.net/projects/combytcp/?source=directory