B4J Question How to obtain a 'virtual serial port' like com2com or similar ?

peacemaker

Expert
Licensed User
Longtime User
To emulate a **virtual COM port (virtual serial port)** under **Windows**, a software solution needs to interact with the operating system at a low level to create a pair of virtual ports that communicate with each other (like `COM3` ↔ `COM4`). This is typically done using a **kernel-mode driver** or a **user-mode virtual COM port provider**.

### **Approaches to Create a Virtual COM Port in Windows**
1. **Using a Kernel-Mode Driver (Most Reliable)**
- Windows provides the **Serial Port Enumerator (SerCx/Sercx2)** framework for creating virtual COM ports.
- A **Virtual COM Port (VCP) driver** (like `usbser.sys` for USB CDC devices) can be used.
- Third-party tools like **com0com**, **HW VSP3**, or **Eltima Virtual Serial Port Driver** create virtual pairs.

2. **User-Mode Virtual COM Port (via a Provider)**
- Windows allows creating **user-mode COM ports** using the **COM Port Emulator API** (e.g., `CreateFile` with `\\.\` prefix).
- Requires implementing a **COM port provider** that mimics serial port behavior.

---

### **Can Java Create a Virtual COM Port?**
**No, Java cannot directly create a virtual COM port** because:
- Java runs in a **user-mode sandbox** and cannot install kernel drivers.
- Java lacks direct access to Windows' **serial port driver stack**.

However, you can:
1. **Use JNI (Java Native Interface) to call a C/C++ library** that interacts with the Windows COM port API.
2. **Use an existing virtual COM port tool (like com0com) and control it via Java**.
3. **Use a Java library that wraps a virtual COM port solution** (e.g., **RxTx**, **jSerialComm** for existing ports, but not creating new ones).

---

### **Best Workarounds in Java**
1. **Use com0com (Free & Open-Source)**
- Install [com0com](https://sourceforge.net/projects/com0com/) to create virtual COM pairs.
- Control it via Java using `ProcessBuilder`:
```java
ProcessBuilder pb = new ProcessBuilder("command", "to", "configure", "com0com");
Process p = pb.start();
```
- Then use `jSerialComm` to interact with the virtual ports.

2. **Use a Commercial Virtual COM Port Driver (Eltima, HW Group)**
- Some tools provide APIs that Java can call via JNA/JNI.

3. **Simulate a COM Port in User-Mode (Limited Use Case)**
- Create a **TCP-to-Serial bridge** (e.g., using `javax.comm` or `jSerialComm`).
- Example:
```java
// Use jSerialComm to listen on a real/virtual port
SerialPort port = SerialPort.getCommPort("COM3");
port.openPort();
```

---

### **Conclusion**
- **Pure Java cannot create a virtual COM port**, but it can interact with existing ones.
- **Best approach**: Use **com0com** or a **third-party driver** to create virtual ports, then access them via Java (`jSerialComm`).
- For **advanced emulation**, write a **C/C++ driver** and call it via **JNI**.
 
Upvote 0

amorosik

Expert
Licensed User
The question arose because I'm using com2com to create a pair of virtual ports on WIn10pro
But it seems that for some programs that system does not work
If I use RealTerm for example on the virtual ports Com5 and Com6, the ports are seen correctly, they can be opened, and you can transmit on Com5 and see the data arriving on Com6
But some programs are not able to see the Com6 port correctly or in any case they are not able to see the data entering, and I don't know exactly why
I assume there is something different from a classic physical com port and therefore not seeing itself inside the 'PORT' section of the devices, some software is not able to 'see' it correctly
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
So, you have to try another variant of the driver, not only "com2com". And compare.
 
Upvote 0

teddybear

Well-Known Member
Licensed User
If I use RealTerm for example on the virtual ports Com5 and Com6, the ports are seen correctly, they can be opened, and you can transmit on Com5 and see the data arriving on Com6
But some programs are not able to see the Com6 port correctly
Do you mean that some programs are B4J apps?
 
Last edited:
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
No, another driver instead of "com2com".
 
Upvote 0

amorosik

Expert
Licensed User
Do you mean that some programs are B4J apps?
No, there are some programs that only 'see' COM ports that are in the PORTS section of Windows devices
The virtual ports created by com0com are NOT seen in the PORTS section of Windows devices
And so this is a problem for this small group of programs
 
Upvote 0
Top