在 C# 中,如何监听已经打开的 COM(串行)端口?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/358037/
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
In C# how could I listen to a COM (Serial) Port that is already open?
提问by Phobis
I am using a program that talks to my COMM port, but I have made another program that I want to "sniff" the comm port messages and perform it's own actions against those messages in addition. Is this possible in .NET c#?
我正在使用一个与我的 COMM 端口通信的程序,但我制作了另一个程序,我想“嗅探”通信端口消息并另外对这些消息执行它自己的操作。这在 .NET c# 中可能吗?
采纳答案by Tim
There are third party libraries/tools/products that expose the traffic f you are interested.
有第三方库/工具/产品可以公开您感兴趣的流量。
Here is one I used for serial port emulation - but I think it provides something you can use: http://com0com.sourceforge.net/
这是我用于串行端口仿真的一个 - 但我认为它提供了一些你可以使用的东西:http: //com0com.sourceforge.net/
回答by Eric
It is possible to sniff traffic from the serial port
可以从串行端口嗅探流量
However there doesnt seem to be a "COMPortSniffer" Control
但是似乎没有“COMPortSniffer”控件
A valid technique used by sysinternals is presented there
此处介绍了 sysinternals 使用的有效技术
It seems to rely on Win32 programming however, I dont think such a thing is possible directly with C#
然而,它似乎依赖于 Win32 编程,我认为直接用 C# 不可能实现这样的事情
回答by faulty
If you have control over the first program that talks to you COMM port, why not change the program to pass data received from the port to the 2nd program of yours via remoting or any other type of IPC. Better still if you can write a proxy program that connected to the COMM port, and have 2 of the other program talk to this proxy to get the communication done.
如果您可以控制与您的 COMM 端口通信的第一个程序,为什么不更改程序以通过远程处理或任何其他类型的 IPC 将从端口接收到的数据传递到您的第二个程序。如果您可以编写一个连接到 COMM 端口的代理程序,并让另一个程序中的 2 个与此代理通信以完成通信,那就更好了。
Another idea is, if you need to sniff only incoming data, you can get a Y-cable (splitter) and connect to 2 COMM port, each program connects to each COMM port. But you need to make sure the 2nd program is not trying to transmit. In some cases you might need a splitter which only connects the RX pin for the 2nd output. Let me know if you need the diagram.
另一个想法是,如果您只需要嗅探传入的数据,您可以使用 Y 型电缆(分离器)并连接到 2 个 COMM 端口,每个程序连接到每个 COMM 端口。但是您需要确保第二个程序没有尝试传输。在某些情况下,您可能需要一个仅连接第二个输出的 RX 引脚的分配器。让我知道您是否需要图表。
If you don't have 2 COMM, you can easily get a USB-Serial Converter for less than USD10.
如果您没有 2 个 COMM,您可以轻松获得一个 USB 串行转换器,价格不到 10 美元。
回答by jbutler483
the code project (http://www.codeproject.com/Articles/75770/Basic-serial-port-listening-application) that has a great tutorial on this.
代码项目(http://www.codeproject.com/Articles/75770/Basic-serial-port-listening-application)有一个很好的教程。
It shows how to read data coming in from a serial port, and from that you should be able to read the data.
它显示了如何读取来自串行端口的数据,并且您应该能够从中读取数据。
A short snippet:
一个简短的片段:
void _serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
int dataLength = _serialPort.BytesToRead;
byte[] data = new byte[dataLength];
int nbrDataRead = _serialPort.Read(data, 0, dataLength);
if (nbrDataRead == 0)
return;
// Send data to whom ever interested
if (NewSerialDataRecieved != null)
NewSerialDataRecieved(this, new SerialDataEventArgs(data));
}