Every Python developer who works with serial devices hits the same wall: the code is ready, the device is not — it is in another lab, on a colleague’s desk, or still in a shipping box. pySerial does not care whether a port is copper or virtual, and com0com will happily manufacture a port pair on the spot. Put the two together and you get a test bench where one Python script plays the device and your real application plays itself, talking over a virtual null-modem cable. This guide builds that bench with two short, runnable scripts.
Prerequisites are small: com0com installed (the installation guide covers Windows 11 honestly, signing caveats included) and pySerial installed in your environment, which is the usual pip install pyserial. No other libraries are needed.
Step 1: Create the pair and give it COM names
Create one pair and rename both ends, as in the first-pair guide:
command> install - -
command> change CNCA0 PortName=COM10
command> change CNCB0 PortName=COM11
The rename is not cosmetic for Python work. In this guide COM10 is the device side and COM11 is the application side — pick any two free numbers, but rename you must, for the reason in the next section.
The CNCA0 naming gotcha
Out of the box, a com0com pair is called CNCA0 and CNCB0, and code that opens ports by plain name trips over that in two ways. First, many port-scanning helpers only look for names matching the COM pattern, so the CNC names never even appear as candidates. Second, Windows itself needs the device-path prefix \\.\ to open a port whose name is not a classic COM name — the com0com ReadMe documents this for the Windows API, and pySerial is no exception. In Python the working form is a raw string:
import serial
ser = serial.Serial(r'\\.\CNCA0', 9600, timeout=1)
That works, but it makes test code carry a Windows-specific spelling everywhere. The cleaner habit is the rename from Step 1: once the ends are called COM10 and COM11, every library, scanner and script treats them like any other serial port. The full story — including putting ports into the Ports class for tools that enumerate devices — is in the rename guide.
Step 2: The device simulator script
The simulator is the stand-in for your hardware. The minimal useful shape is a loop that waits for a query and answers it the way the real device would. This one answers any line ending in a newline with a canned temperature reading, and answers the specific query READ? with a fixed value — enough to develop against:
import serial
# Device side of the pair
dev = serial.Serial('COM10', 9600, timeout=0.5)
print('simulator listening on COM10')
while True:
line = dev.readline()
if not line:
continue
query = line.decode('ascii', errors='ignore').strip()
print('device received:', query)
if query == 'READ?':
dev.write(b'23.7\n')
else:
dev.write(b'ERR unknown command\n')
Two details matter. The timeout=0.5 makes readline() return periodically even when nothing arrived, so the loop stays responsive and Ctrl+C works. And the simulator prints everything it receives — when a test fails, that printout is how you learn whether your application sent the wrong bytes or the simulator answered them wrongly. The same responder pattern works when the program on the other end is not Python at all — LabVIEW users get the equivalent walkthrough in our LabVIEW virtual COM port guide.
Step 3: Your application (or test) script
The other end is your real code, or a thin slice of it while you are still developing:
import serial
# Application side of the pair
app = serial.Serial('COM11', 9600, timeout=2)
app.write(b'READ?\n')
reply = app.readline()
print('app received:', reply.decode('ascii', errors='ignore').strip())
app.close()
Run the simulator in one terminal, the application in another. The application should print app received: 23.7, and the simulator’s terminal should show it received READ?. That tiny exchange proves the whole chain: pair, names, settings and both programs. From here, growing the simulator is ordinary Python work — answer from a table of canned replies, reply with values read from a file, or add deliberate misbehaviour (answer slowly, answer with garbage, do not answer at all) to see how your application’s error handling copes.
Make both ends agree on settings
One configuration point deserves its own paragraph because it produces the most confusing failures: the two scripts must open their ports with the same line settings. pySerial’s constructor defaults — 9600 baud, eight data bits, no parity, one stop bit — apply to both scripts above, which is why they work untouched. The moment you pass explicit settings to one Serial() call (a device that runs at 19200, say, or even parity), pass the matching settings to the other, or the bytes will be sliced at the wrong boundaries and arrive as garbage that no amount of debugging the logic will explain. On a virtual pair the baud number does not change the actual transfer speed — with baud-rate emulation off, com0com ignores it — but framing parameters still have to agree for the data to survive the trip in the form you expect.
A related growth step: give the simulator memory. A real sensor’s readings change, so let the simulator keep a variable — start at 23.7, drift it slightly with each READ?, and your application’s trending and alarm code gets exercised instead of staring at a constant. Add a second command that changes the simulated state (a setpoint, a mode) and the fake becomes a small state machine, which is all most bench devices ever were.
Timeouts and blocking reads, honestly
Most beginner confusion with pySerial is timeout confusion, so be precise. timeout=None means reads block forever until the requested data arrives — a hung script with no error is usually a blocking read waiting for bytes that are never coming. A numeric timeout makes reads return whatever arrived in that window, possibly an empty bytes object, which your code must handle rather than treat as a reply. timeout=0 is non-blocking: reads return immediately with whatever is in the buffer. None of this is com0com-specific; the virtual pair delivers bytes exactly like a cable, which is precisely why it is a faithful test bench. One pair-specific behaviour is worth knowing: if nothing has the far end open, bytes you write can wait in the buffer and surface at the next connection instead of vanishing as they would on a real cable — the reasons and the EmuOverrun setting that changes it are covered in com0com hangs and leftover characters.
From scripts to a pytest fixture
Once the two-script bench works, the step to automated tests is conceptual rather than technical. The pattern: a pytest fixture starts the simulator — in a thread inside the test process, or as a separate process — on one end of the pair before each test module, hands the test the other end’s port name, and shuts the simulator down afterwards. Your tests then read like hardware tests (send READ?, assert the reply parses to 23.7) while running in CI on a machine with no serial hardware at all, as long as the com0com driver is installed there. Keep the canned replies data-driven — a small dictionary or file per simulated device — and the same fixture grows from one sensor into a whole product line of fake devices. The broader workflow, including echo responders and record/replay ideas, is in testing a serial app without the hardware.
Limits of the bench
A virtual pair moves bytes; it does not reproduce a real device’s timing, its flow-control behaviour on physical pins, or its firmware’s quirks. Baud rate is also notional unless you enable emulation — with EmuBR off (the default) the pair transfers as fast as it can regardless of the 9600 in your code, so throughput and inter-frame timing tests need change CNCA0 EmuBR=yes and the discussion in the SetupG guide. Use the bench to develop and regression-test your PC-side code; still run a final session against the real device before you ship.
