Connecting multiple virtual serial ports together
However many times you may want to connect a real device to a virtual serial cable. This can be done using program hub4com. While the com0com works on the “driver side” of the com port the hub4com works on the “software side” and can be used to connect mutliple com ports (virtual or real ports) together. Also more than two ports can be connected together and ports may be forwared thru internet.
With this utility also also more complicated connections are possible, such as connecting multiple output serial ports to a single data source or making a software serial port sniffer cable. Hub4com is a part of com0com project, but is distributed separately. It can be downloaded here. You could also check one folder up to see if there are any newer versions. Unless you want to compile the program yourself you should download the binary version which is packed in hub4com-2.1.0.0-386.zip. To install it just unzip the contents of the archive to com0com installation folder (you will probably find it in Program Files x86).
To get some help for the program you have to run it without any arguments. You can do this from command prompt. Just clicking on the executable will show the help, but will dissapear too fast for you to be able to read it. First open a command prompt (Win Button + R, enter cmd into text box and press ok).
Command prompt will start in your user directory so you have to navigate to the directory containing hub4com.exe In my case this can be done by entering “cd C:\Program Files (x86)\com0com” command into command prompt.
Now you can run the hub4com.exe program with “hub4com” command. This will show the help for the program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
Usage: hub4com [options] <port0> [options] [<port1> ...] Common options: --load=[<file>][,<begin>[,<end>]][:<prms>] - load arguments (one argument per line) between <begin> and <end> lines from a file <file> (use standard input if empty) and insert them into the command line. The syntax of <prms> is <PRM1>[,<PRM2>...], where <PRMn> will replace %%n%% in the arguments. Do loading since begining if <begin> is empty. Do loading till end-of-file if <end> is empty. Ignore arguments begining with '#'. <file> will replace %%0%% in the arguments. It is possible up to 256 recursive loads. --help - show this help. --help=* - show help for all modules. --help=<LstM> - show help for modules listed in <LstM>. The syntax of <LstM> above is <MID0>[,<MID1>...], where <MIDn> is a module name. Route options: --route=<LstR>:<LstL> - send data received from any port listed in <LstR> to all ports (except itself) listed in <LstL>. --bi-route=<LstR>:<LstL> - send data received from any port listed in <LstR> to all ports (except itself) listed in <LstL> and vice versa. --echo-route=<Lst> - send data received from any port listed in <Lst> back to itself via all attached filters. --no-route=<LstR>:<LstL> - do not send data received from any port listed in <LstR> to the ports listed in <LstL>. --fc-route=<LstR>:<LstL> - enable flow control route from any port listed in <LstR> to all ports listed in <LstL>. --no-default-fc-route=<LstR>:<LstL> disable default flow control route from any port listed in <LstR> to all ports listed in <LstL> (default flow control route enabled from P1 to P2 if enabled data route from P1 to P2 and from P2 to P1). If no any route option specified, then the options --route=0:All --route=1:0 used by default (route data from first port to all ports and from second port to first port). Filter options: --create-filter=<MID>[,<FGID>[,<FN>]][:<Args>] - by using filter module with name <MID> create a filter with name <FN> (<FN> is <FGID> by default) and put arguments <Args> (if any) to the filter. Add filter to the end of filter group <FGID> (<FGID> is <MID> by default). Any filter can accept described above option --load=[...]. --add-filters=<Lst>:<LstF> - attach the filters listed in <LstF> to the ports listed in <Lst>. These filters will handle the data by IN method just after receiving from ports listed in <Lst> or by OUT method just before sending to ports listed in <Lst>. The syntax of <LstF> above is <F1>[,<F2>...], where the syntax of <Fn> is <FGID>[.<Method>][(<Lst>)], where <FGID> is a filter group, <Method> is IN or OUT and <Lst> lists the source ports (the data only from them will be handled by OUT method). The <FGID> w/o <Method> is equivalent to adding IN and OUT for each filter from the group <FGID>. If the list of the source ports is not specified then the data routed from any port will be handled by OUT method. Port options: --use-driver=<MID> - use driver module with name <MID> to create the following ports (<MID> is serial by default). The syntax of <LstR>, <LstL> and <Lst> above is <P1>[,<P2>...], where <Pn> is a zero based position number of port or All. List of filter modules: awakseq - Connect on awakening sequence filter crypt - Encrypting/decrypting filter echo - Echo filter (alternative to --echo-route option) escinsert - Escaped datastream generating filter escparse - Escaped data stream parsing filter linectl - Baudrate and line control mapping filter lsrmap - LSR mapping filter pin2con - Connect or disconnect on changing of line or modem state filter pinmap - Pinouts mapping filter purge - Purge mapping filter tag - Tag filter tag-sync - Tag synchronization filter telnet - Telnet protocol filter trace - Trace filter List of driver modules: connector - Connectable fake port driver serial - Serial port driver tcp - TCP port driver Examples: hub4com --route=All:All \\.\CNCB0 \\.\CNCB1 \\.\CNCB2 - receive data from CNCB0 and send it to CNCB1 and CNCB2, receive data from CNCB1 and send it to CNCB0 and CNCB2, receive data from CNCB2 and send it to CNCB0 and CNCB1. hub4com --echo-route=0 COM2 - receive data from COM2 and send it back to COM2. hub4com --load= --echo-route=0 COM2 ^Z - the same as above. hub4com --load=,_BEGIN_,_END_ blah blah blah _BEGIN_ --echo-route=0 COM2 _END_ - the same as above. |
For example a very basic setting would be to create a hub for three ports:
1 |
hub4com --route=All:All \\.\CNCB0 \\.\CNCB1 \\.\CNCB2 |
This will forward any data received on any port to both other ports. Note that CNC* ports are usually used with virtual only (not available to software) com0com driver and COM* are real or virtual comports. For running the above command you should have all the named ports set up as a com0com virtual pairs (for example CNCB0<>CNCA0, CNCB1<>CNCA1, CNCB2<>CNCA2).
If the communication devices don’t need any hardware flow control it is good to add the following modifiers to the hub4com to prevent issues with flow control:
1 |
--octs=off --no-default-fc-route=All:All |
Flow control is not necessary a bad thing, but if it is not used (and you don’t want to think / worry about it) it is better to just turn it off. The full command would be:
1 |
hub4com --octs=off --no-default-fc-route=All:All --route=All:All \\.\CNCB0 \\.\CNCB1 \\.\CNCB2 |
For example we could use the above command for monitoring a communication between a USB GPS device connected to COM8 and host PC GPS software. Note that one of the ports has been changed to COM8 to directly connect to hardware port. (You will probably also want to set the baudrate with –baud=… setting.)
1 |
hub4com --octs=off --no-default-fc-route=All:All --route=All:All \\.\CNCB0 \\.\CNCB1 \\.\COM8 |
Our host PC GPS software would now connect to one of the two com0com connectors, for example to CNCA0. Our “sniffer” software would connect to the other connector CNCA1. On the CNCA1 port you can receive the data from both sides, however this simple sniffing configuration will only work nicely for half duplex communication (only one device communicating at one time). When both devices are sending data at once you will get mixed characters from both sources. There is no way to directly tell which device sent the data and you can not inject data to both devices.
For a more elaborate sniffing cable connection you could use the following command:
1 |
hub4com --octs=off --no-default-fc-route=All:All --route=0:1,3 --route=3:0,2 \\.\CNCB0 \\.\CNCB1 \\.\CNCB2 \\.\COM8 |
With this command the data from COM8 is connected to CNCB0(CNCA0) and CNCB1(CNCA1). The data from CNCB0(CNCA0) is forwarded to COM8 and CNCB2(CNCA2). This way the COM8 and CNCA0 communicate bidirectionally while we have a receive only ports CNCA1 and CNCA2. Each of these ports will be used for receiving/sniffing data from one of the bidirectional ports. This configuration is actually equivalent to what you will usually find online as a hardware bidirectional serial port sniffer cable.
By adding additional data routes you could also make the ports CNCA1 and CNCA2 be able to inject data to each of the bidirectional ports. This is a feature that can only be implemented in software as there is no way you can combine two RS232 TX signals in hardware…
I have only touched some of the things you can do with hub4com, also additional functions such as multiplexing, serial over internet, internet server and many more modes are available. Actually with so many settings available in hub4com the possibilities are countless, so your imagination is the limit (or maybe the amount of time you want to spend playing with it) 😉
Thanks for the article, a very nice read! However, I would like also to add, that there are also other pieces of software that can create virtual COM ports or let you connect a real device with a virtual port. All additional details can be found in the articles, like this one
https://www.eltima.com/create-virtual-serial-port.html
Software to connect real devices to a virtual serial cable
https://www.eltima.com/products/serialsplitter/
Please keep advercomments off this site. I’ve seen this comment, a lot, in different posts. com0com and hub4com are open source. These Eltima and the notorious “free serial monitor” program (which I will not list here) are not open source.
Is it possible to switch baud rate up to 57600?
Can you describe it?
The com0com emulation normally doesn’t care about the baudrate, so you can safely connect with 57600 baud on one virtual port and 9600 baud on other port (which would not be possible in physical word). Actually com0com won’t emulate the baudrate mismatch on virtual fable (wrong data you would get), however you can use the “enable baud rate” tick to emulate the timings/delays caused by sending at the chosen baudrate.
Hello I have had good success using com0com to practice with virtual serial before going to real hardware and I wired together a serial sniffer cable so that I could separately capture the outgoing byte stream each direction. Now I need to do the same thing with a USB GPS device which uses the Prolific chipset USB serial and hub4com looks like the perfect solution but I cannot make it work.
I have three virtual com port pairs setup:
command> list
CNCA0 PortName=-
CNCB0 PortName=COM#,RealPortName=COM20
CNCA1 PortName=COM#,RealPortName=COM16
CNCB1 PortName=COM#,RealPortName=COM14
CNCA2 PortName=COM#,RealPortName=COM18
CNCB2 PortName=COM#,RealPortName=COM19
and I launch hub4com in an admin cmd window with the command:
hub4com –octs=off –no-default-fc-route=All:All –route=0:1,3 –route=3:0,2 \\.\COM20 \\.\COM14 \\.\COM19 \\.\COM8
but I cannot open a connection to any of the com ports.
The GPS software returns:
“Error.. Cannot create UART handle. Comm port is not available or may be in use”
and PuTTY returns:
“Unable to open connection to COM? Unable to open serial port”
I tried to connect to COM8, COM14, COM19 & COM20 I did not expect COM8 to work. I also tried the same hub4com as a standard user.
Can you see what I’m doing wrong?
Hi, it’s been quite some time since I used this. The error seems to indicate that you probably have a handle open somewhere to that port. This may be due to other software trying to access it (eg. open in terminal or by system – legacy mouse and bt drivers may sometimes take over) or it being already in use. You can use RealTerm in sniffer mode (Echo port tab, Echo on, Monitor) with a single com0com port to do the bidirectional logging, however it will not work for high data troughput and is not as fast, so for some applications it may not work. There are also software solutions that enable you to examine tx/rx data at the driver level. I mainly still use a hardware 4 port cable or the above RealTerm solution. Regards,
Dejan
You might try a cable with an FTDI chip; some of those Prolific cables are counterfeit and don’t work.