Search code examples
usbserial

How to use a USB connection in more than one program?


I have an amateur telescope management application which uses two Windows applications, one is a DLL and the other a winforms app, both created using C#

Both these programs communicate with the same AVR microcontroller via two separate USB to serial FTDI devices.

The volume of serial traffic is very low and could easily be managed (with appropriate protocols) over one USB connection rather than the two currently in use.

Is it possible for the two programs to essentially share one USB connection?

If it’s possible, any pointers to resources which would help me to learn how to do it would be much appreciated.


Solution

  • Some of the options you have are:

    Multiplexing solutions

    Use a single serial connection with a multiplexing protocol. The multiplexing protocol will need to be packet based, and each packet contains a field indicating if it belongs to the communication with the first or second application.

    On the Windows side, you have at least three options to implement it:

    1. Create an additional process for multiplexing (merging/splitting) the two separate communication streams. This process opens and owns the underlying serial connection. The two applications no longer access the serial connection directly. Instead, they use inter-process communication to talk to the multiplexing process.

    2. Make one of the applications the master for the serial connection. It will open and own it. The second application uses inter-process communication to talk to the master application whenever it wants to communicate with the telescope. It will require that the master application runs whenever the second applications runs, possibly in the background only.

    3. Create a Windows driver that creates two additional COM ports and handles the multiplexing on the underlying serial connection.

    All these options require a considerable investment into software and increase complexity. The third one requires specialized know-how.

    USB solutions

    Instead of communicating over serial connections, use USB instead. Use a microcontroller with USB device capability, and implement either two USB CDC interfaces (which appear as two COM ports on Windows), or two USB interfaces with a custom protocol (and use WinUSB calls to access them).

    With two separate USB interfaces, Windows will let two separate applications open the device and communicate with it. And the USB protocol does the multiplexing for you.

    Hardware solution

    Since USB-to-serial converters and USB hubs are inexpensive, a possible approach would be to put the AVR microcontroller, two USB-to-serial converters and a USB hub into a single case. It would make for a nice device connect with a single USB cable (on the PC side). And it wouldn't require any changes to the software.

    (We don't know anything about how your AVR MCU is connected to the telescope. So it's difficult to tell if this is helpful.)