In C# how could I listen to a COM (Serial) Port that is already open?
Asked Answered
R

4

10

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

Robrobaina answered 10/12, 2008 at 23:51 Comment(0)
Z
6

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/

Ziska answered 11/12, 2008 at 1:8 Comment(0)
T
2

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.

Tabatha answered 11/12, 2008 at 7:38 Comment(0)
C
1

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#

Communist answered 10/12, 2008 at 23:56 Comment(0)
E
0

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));
}
Emulsifier answered 3/9, 2014 at 8:10 Comment(1)
In this case you must have the control about the primary consumer source code to publish the read data. It is not sniffing an established serial communication.Shoebill

© 2022 - 2024 — McMap. All rights reserved.