Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I simulate SerialPort interactions for testing?

Tags:

I'm about to start developing a small app (C#) that communicates with a PLC and a testing unit via Serial Ports - this is my first venture into this area.

In essence, I am going to send the PLC a signal to start an operation, and then I am going to wait for the result of that operation from the test unit (which will be independently communicating with the PLC) to return a ASCII string.

Depending on the content of that string, I may want to listen to a signal from the PLC...

It's all new to me, so at the moment, I'm just researching System.IO.Ports.SerialPort; digression: are there third part products out there than simplify interaction with the Serial Port, or are the built-in classes as good as you will get? I'm thinking of ease of use as opposed to better features.

However, it will be a few weeks before the hardware is available for development and testing, so I was wondering how I could simulate communication to/from the serial port so that I can start developing my app?

[I don't yet know how the PLC and the PC are due to communicate - I understand it will be binary rather than text, but at the moment, that is all I know.]

like image 327
CJM Avatar asked Apr 12 '11 14:04

CJM


2 Answers

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!

like image 166
DavidGouge Avatar answered Oct 09 '22 18:10

DavidGouge


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

like image 37
fryguybob Avatar answered Oct 09 '22 19:10

fryguybob