在 C# 中,我如何监听已打开的 COM(串行)端口?

发布于 2024-07-10 16:47:11 字数 84 浏览 8 评论 0原文

我正在使用一个与我的通讯端口对话的程序,但我制作了另一个程序,我想“嗅探”通讯端口消息并针对这些消息执行它自己的操作。 这在 .NET c# 中可能吗?

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#?

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

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

发布评论

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

评论(4

歌枕肩 2024-07-17 16:47:11

有第三方库/工具/产品可以公开您感兴趣的流量。

这是我用于串行端口仿真的一个 - 但我认为它提供了您可以使用的东西:
http://com0com.sourceforge.net/

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/

伴随着你 2024-07-17 16:47:11

如果您可以控制与 COMM 端口通信的第一个程序,为什么不更改该程序以通过远程处理或任何其他类型的 IPC 将从端口接收到的数据传递到您的第二个程序。 如果您可以编写一个连接到 COMM 端口的代理程序,并让另外 2 个程序与该代理通信以完成通信,那就更好了。

另一个想法是,如果您只需要嗅探传入的数据,您可以使用 Y 型电缆(分路器)并连接到 2 个 COMM 端口,每个程序连接到每个 COMM 端口。 但您需要确保第二个程序没有尝试传输。 在某些情况下,您可能需要一个仅连接第二个输出的 RX 引脚的分离器。 如果您需要图表,请告诉我。

如果您没有 2 个 COMM,您可以以不到 10 美元的价格轻松获得 USB 串行转换器。

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.

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.

If you don't have 2 COMM, you can easily get a USB-Serial Converter for less than USD10.

暮凉 2024-07-17 16:47:11

可以从串行端口嗅探流量

,但是似乎没有“COMPortSniffer”控件

提供了 sysinternals 使用的有效技术 那里

它似乎依赖于Win32编程,但是,我认为直接用C#不可能实现这样的事情

It is possible to sniff traffic from the serial port

However there doesnt seem to be a "COMPortSniffer" Control

A valid technique used by sysinternals is presented there

It seems to rely on Win32 programming however, I dont think such a thing is possible directly with C#

始终不够爱げ你 2024-07-17 16:47:11

代码项目 (http://www.codeproject.com/Articles /75770/Basic-serial-port-listening-application),对此有一个很好的教程。

它展示了如何读取来自串行端口的数据,并且您应该能够从中读取数据。

一个简短的片段:

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));
}

the code project (http://www.codeproject.com/Articles/75770/Basic-serial-port-listening-application) that has a great tutorial on this.

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