Faking a serial device
I promised to discuss with you another thing – what to do if you have no device available. Well, there is a reason I used the IComPortWatcher and IAsyncSerial interfaces. We can mock them in a unit test and write fake code that mimics devices. And that is a pretty good idea, as serial communication is brittle and often fails. If you are developing your software, you want an environment you can rely on. Using these interfaces can help you.
For instance, I can have another implementation of IComPortWatcher that contains a Start() method that looks like this:
public void Start()
{
_timer = new Timer(2000);
_timer.Elapsed += (sender, args) =>
{
// Trigger the event every second
if (_deviceIsAvailable)
{
&...