How can I simulate SerialPort interactions for testing?

Abstract away your serial port comms behind an interface so that you can code your app against the interface and then test with a 'fake' implementation. When you've got the hardware for the real thing, you can code up the 'real' implementation of the interface and swap out the fake one.

So for example, you'd have an interface

public interface ISerialComms
{
    void SendMessage(string message)
}

and you'd code your app against that interface using a fake implementation:

public class FakeSerialComms : ISerialComms
{
    public void SendMessage(string message)
    {
        //some implementation
    }
}

Hope that helps!


I've had some success in the past using com0com.