I've set up virtual COM ports with com0com (COM20 and COM21), but I'm having trouble accessing them through Python's pyserial.
What I've tried
I can successfully create the port pair using com0com's setupc.exe
:
> setupc.exe install PortName=COM20 PortName=COM21
The virtual port pair is visible in the com0com GUI:
and they appear under com0com - serial port emulators
in device manager:
But when I try to access these ports with pyserial, I get a FileNotFoundError:
import serial
import serial.tools.list_ports
# List available ports
print("Available COM ports:")
for port in serial.tools.list_portsports():
print(f" {port.device}: {port.description}")
# Try to open the virtual port
try:
ser = serial.Serial('COM20', 9600, timeout=1)
print("Successfully opened COM20")
ser.close()
except Exception as e:
print(f"Error: {e}")
Output:
Available COM ports:
COM7: Standard Serial over Bluetooth link (COM7)
COM3: Intel(R) Active Management Technology - SOL (COM3)
COM6: Standard Serial over Bluetooth link (COM6)
Error: could not open port 'COM20': FileNotFoundError(2, 'The system cannot find the file specified.', None, 2)
Environment
- Windows 10
- Python 3.12.2
- pyserial 3.5
- com0com 3.0.0.0
- Running script with administrator privileges
Questions
- Why aren't the com0com virtual ports appearing in the pyserial port list?
- How can I make pyserial recognize and use com0com virtual ports?
- Is there a delay or extra step needed after creating ports before they're accessible?
I've already tried:
- Running as administrator
- Creating ports with higher numbers (COM20, COM21)
- Verifying the ports in Device Manager and com0com GUI
- Restarting the computer
Any insights or alternative approaches would be greatly appreciated.