views:

579

answers:

4

I am looking for a book and or a code sample of how to do serial port redirection or port splitting. I write a lot of com port applications and want to write a monitor application that will allow me to watch the serial port between the application and the device.

So I want to be able to redirect the serial port to another port so I can monitor the data flowing between ports.

I understand in some cases this must be accomplished at the kernel level. (in USB cases)

I have written the monitor application using system.IO.Ports in C#. If I have to call a C++/C assembly that is fine.

Thanks

Joe

+1  A: 

You might find that PortMon by Mark Russinovich is helpful in your work, although it isn't provided with source code. It will log all system calls related to a port, and has extensive filtering capabilities to keep that log to a manageable size.

Edit: PortMon works by injecting a kernel mode device driver into the system when it runs. That driver hooks the ports that it wants to monitor by inserting a filter driver into the stack. That filter driver reports all IRPs that pass through it to the application. This is not an easy beast to implement by any stretch, and it really can't be done in user mode.

The Windows DDK does have sample code for a port filter driver, but it is a lot of work to go from the sample to something useful.

A pure user mode solution that might work out is to use two additional serial ports to eavesdrop on the wire. A pair of USB to serial adapters and a bit of wiring will do what you need. Then it is only a "small matter of programming" to monitor and correlate the send and receive lines. A single chip solution could be based on the FT2232H device from FTDI, which has an available evaluation module. Add a couple of RS232 level translators and some D connectors and you've got a serial eavesdropper on USB that looks like two COM ports to Windows.

Another outside the box approach is that many logic analyzers and mixed signal oscilloscopes can do serial protocol decoding, often as an optional component. One source of inexpensive USB+software solutions is Saelig. I've bought chips and modules from them myself, but don't have any direct experience with any of the USB-based logic analyzers they sell despite thinking that I should have one in my bag along with the Netbook...

RBerteig
Thanks RB, I am using XPort currently but want to embed the port splitting in the application.
Joe Pitz
I've updated my answer with a couple of other ideas that occurred to me this afternoon...
RBerteig
A: 

I wrote one, many years ago. The DDK (device driver kit) included sample source code for a filter driver for (i.e. immediately above) the parallel port driver: I adapted this sample to work with the serial port driver, and then added functionality such as monitoring and splitting.

This was serial ports, not USBs.

I also implemented another driver, which was a 'virtual' serial port, i.e. it implemented a serial port API but then redirected the data I/O elsewhere.

It was a lot of work (many months) to implement.

ChrisW
A: 

Purchase a RS232 DB9 Serial Data Tap. This is a hardware device that is connected between the two serial ports and listens passively to the "chatter" between the devices. I used the following device for two-and-a-half years and it saved me significant amounts of troubleshooting time and effort. (I use to take it home every night to keep support technicians from "borrowing" it.) It is easy to make this device, but the following data tap is flexible.

"Tap in on a serial data stream and transparently monitor data activity. The output can be feed into a monitor printer or other device. Dipswitches allow programming to monitor the main transmit and receive lines, individually or together. No external power required, easy install, transparent connection Ideal RS232/Serial software development aid."

Alternatively, make or purchase a Serial Tap Monitor Cable. See the NST documentation for one possible configuration.

I strongly recommend you do not use a software solution because the monitoring software may be affected by physical situations.

AMissico
Thanks RB and Anthony. I have been thinking about this solution as well. We do have a serial trap device, but one of our devices uses a USB to COM mapping. But I may try to make a trap monitor cable for USB.
Joe Pitz
A: 

As always it depends on what you're going to do. If you need to mess around with how the lines between two serial devices are connected or need some 'good' timing informations, the best you can get is an external break-out box like Anthony already told.

If you just need some kind of monitoring you can go with PortMon or Serial Port Monitor.

Maybe you write two applications which are going to communicate over a serial connection. To get this done on a single PC without a local loopback cable you can try Com0Com.

Oliver
Thanks Oliver, I have used Com0com in the past, but because one device is USB to COM port com0com appears not to work. I have tried PortMon, but the data it captures is incomplete. Xport works but I may have to put this out on our production line and I do not want the techs to have to load two programs at a time.
Joe Pitz