Test Your Serial Application Without the Hardware: The com0com Workflow

Serial software has a cruel dependency: it cannot be exercised at all without something on the other end of the cable. So the device becomes the test schedule — development stalls when it is booked by another team, automated tests are impossible, and ‘works on my bench’ means one specific unit with one specific firmware. The way out is to notice that your application cannot see the device; it can only see a COM port. Give it a virtual port with something believable behind it, and the application is testable on any desk, in any CI runner, at 2 a.m. This post lays out that workflow end to end.

The foundation is one com0com pair, renamed to COM names: your application opens one end, the fake device lives on the other. Setup is the first-pair guide plus the rename guide; if you are new to the pair itself, prove it first with the two-terminal PuTTY loopback test.

Step 1: Think of the pair as a device socket

Decide which end is which and never swap them mid-project: for example, your application always opens COM11, and the fake device always listens on COM10. Write that convention into the project README. It sounds trivial, but half of all ‘the simulator does nothing’ reports are the application and the simulator opening the same end, or a pair of ends that are not partners. Applications that scan for ports may also need the pair in the Ports class so the port enumerates like hardware — the rename guide covers that form.

Step 2: Write a stand-in responder

The fake device is a small program with three behaviours, in increasing order of usefulness:

  1. Echo. Send back whatever was received. Useless as a device, priceless as a smoke test: it proves the application can open the port, write, and read its own words returning.
  2. Canned replies. A table from command to response: when the application sends the device’s ‘measure’ command, reply with a realistic measurement string; when it sends ‘status’, reply with the status format from the manual. This is the workhorse mode — the application can now run its full workflow, parsing included.
  3. Scripted misbehaviour. The same table plus intent: reply slowly, reply with a truncated frame, reply with a checksum error, do not reply at all. Devices in the field do all of these; your error handling should meet them first in the simulator, not in a customer log file.

The implementation can be any language your team already uses. A Python version with pySerial — one script listening, one table of replies — is walked through line by line in the pySerial virtual port guide; the responder there is about twenty lines, and that is genuinely all the machinery this step needs.

Step 3: Record and replay the real device, once

Canned replies are guesses about what the device says. Recordings are facts. When you do have the hardware for a day, capture real sessions: put a logging tool (or hub4com’s trace capability, introduced in the hub4com guide) between the device and the application, run the important workflows, and save the exchanges. Back at your desk, the stand-in responder stops inventing replies and starts replaying them: match the application’s request, answer with the recorded response. Record/replay will never cover paths you did not record, and it ages as firmware changes — but for regression testing (‘did my refactor change how we talk to the device?’) a library of recorded sessions is the most faithful fake there is.

Step 4: Put the fake on the team, not on your laptop

The workflow pays compound interest when it stops being a personal trick. Keep the responder and its reply tables in the project repository, versioned with the code. Document the pair convention (Step 1) next to them. New team members then test serial features without booking hardware, reviewers can reproduce serial bug reports exactly, and CI can run the conversation tests on any Windows machine with the com0com driver installed. Failures found against the fake are cheap; the same failures found against the one physical unit, during integration week, are not.

Keep the recordings and reply tables tidy as you go: one folder per device, one file per session or command set, named for the workflow rather than the date. A recording called ‘startup-handshake’ will be found and reused; one called ‘capture-final-2’ will not, and the difference decides whether the library survives its first month.

Choose how faithful the fake needs to be

Not every test deserves the full treatment, and overbuilding the fake is its own waste. Three levels cover most projects. Level one, the echo: five minutes of work, enough for smoke tests and for proving the application’s port handling — use it the day the project starts. Level two, the reply table: an hour or two with the device manual, enough for feature development and parser work — this is where most teams should live. Level three, the recording: a day with the real hardware, capturing sessions for replay, worth it when the device is expensive, remote, shared, or about to be returned to whoever lent it. Climb only as high as the risk justifies: a hobby project stops at level one, a product team that ships against one scarce prototype usually ends at level three, and both are right.

Whichever level you choose, write the fake’s assumptions down where the team can see them — which commands it answers, which it ignores, which replies are recorded fact and which are read from the manual and hoped. The fake is a theory of the device; keeping the theory explicit is what lets the next engineer trust the tests built on it, and update the theory the day the real device disagrees.

The honest limits

A virtual pair is a byte pipe with emulated dressing, and a professional workflow names what it does not reproduce:

  • Timing is not real. With baud-rate emulation off (the default), transfers are as fast as the machine can move them; a device that answers in 200 ms is answered here in microseconds. Enable EmuBR on the sending port — change CNCA0 EmuBR=yes, see the SetupG guide — when inter-frame gaps or pacing matter, and even then treat timing results as approximate.
  • Modem signals are emulated, not voltages. com0com models the control lines (DTR/DSR, RTS/CTS, DCD, RI) in software and can emulate their behaviour, but nothing physical is asserted: there is no real CTS pin for a device to hold low, and hardware flow control against a real device cannot be validated here.
  • Electrical behaviour does not exist. No RS232 levels, no RS485 bus, no cable length, no noise — except the noise you deliberately inject with the driver’s EmuNoise option, which corrupts data at a set probability for robustness testing.
  • You are testing your software, not the firmware. The fake device behaves the way you believe the device behaves. Every assumption in the reply table is a place the real device can still surprise you.

The conclusion those limits point to is a division of labour, not a dismissal: develop, debug and regression-test against the fake, continuously and automatically — then run a deliberate final validation against the real device, on the real cable, before release. Teams that skip the fake waste the device; teams that skip the final validation ship the assumptions. The pair makes the first part nearly free, which is exactly why it belongs in the workflow.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *