-1

I need to write an application that can seat in the middle of a serial port communication between an application (3rd party, no source) running on my pc and a serial device connected to it (say on COM1).

The application normally communicates with the device as soon as you plug the cable. What I need to do is break this communication by "installing" my app in the middle so I can control what data goes through.

The only way I can think of solving this is creating a a pair of virtual serial ports (COM5<->COM6) using com0com. Then I would redirect the app to use COM5 instead of COM1 (I can set this in the app options) and then write a program that will copy every byte received in COM6 (sent by the application) into COM1 (sent to the device) and viceversa.

The problem is that I need to do this without introducing any delays into the communication, otherwise the application and the device will go out of sync.

I tried creating a C# console app to do this, but it seems to wait for a pause in the stream before raising the datareceived event, effectively introducing several ms of delay (speed is 9600).

What I would like to try now is to rewrite the program (maybe using the win32 api?) in such a way that it relays each rx byte as soon as it arrives, so the device would not notice any delay.

Can I do this using win32? Is there a simpler solution you can recommend?

Thanks

Awer Muller
  • 557
  • 7
  • 17
  • For win32 API you can use [`CreateFile`](https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilea). For example, `HANDLE hPort = CreateFile( L"COM1", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);` – Rita Han Jul 25 '19 at 02:18
  • 1
    you need set [`COMMTIMEOUTS`](https://learn.microsoft.com/ru-ru/windows/win32/api/winbase/ns-winbase-_commtimeouts) - *If an application sets `ReadIntervalTimeout` and `ReadTotalTimeoutMultiplier` to `MAXDWORD` and sets `ReadTotalTimeoutConstant` to a value greater than zero and less than MAXDWORD, one of the following occurs when the ReadFile function is called: If there are any bytes in the input buffer, `ReadFile` returns immediately with the bytes in the buffer.* – RbMm Jul 25 '19 at 07:55

1 Answers1

0

Maybe not much of a complete answer for your question but I think this might be worth a try.

Depending on your needs (you did not explain them) you can try to use Termite together with com0com. You can see a quick example here.

There is also an open source clone but I think the very feature you need (forwarding) was never implemented.

In any case, Termite allows scripting, so it is quite likely to be able to meet your needs.

Marcos G.
  • 3,371
  • 2
  • 8
  • 16