Just imagine you are developing embedded device or IoT device solution where you need to interact your software with a hardware device over Com port.
Normally you need to have your real device turn on and connect it to your development PC before you can interact with it. The challenge is that you might developing your software and the embedded prototype is not ready yet, you are waiting for it, you need to testing it locally before connecting to a real device or just to emulate the data communication before developing embedded prototype. Then your only way is to create a virtual Com port.
In this article I am going to use Virtual Serial Port Driver 9.0 from Eltima’s software.
I will demonstrate how to use Virtual Serial Port Driver 9.0 using two different examples:
- Chat example based on C#, using Microsoft System.IO.Ports example.
- Using C# to emulate embedded device that sending sensor data using and receive the data.
Before we dig into examples, let download Virtual Serial Port 9.0 standard edition from https://www.eltima.com/products/vspdxp.
Install the software and start it.
Click on Add Pair
And Ya, That’s it, now you have 2 com virtual ports COM1 and COM2.
I found Virtual Serial Port Driver 9.0 one of the easiest and most stable virtual serial port software on market. It allows me to develop and test my code before spending hours on a real device testing.
I have visited Microsoft SerialPort Class (Sytem.IO.Ports) page and found a chat example. I have created visual studio solution that contains all example of this article on my git ComPort. Clone it and fire up your Visual Studio with the solution twice (I do used VS2017) and yes, I mean start Visual Studio two times because we need to have 2 chats to connect to each other.
Make sure your Chat project is set as Start-up project on both VS.
Start the program (ctrl + F5) on your first VS and your console will start asking you questions, just press ENTER (leave empty) until you reach the name, just give any name, I call my first chatter Joe as you can see in the image below.
Start your second console program, this time you need to tell the program that you are using COM2 as shown in the image, and tap ENTER until the name and just give it any name, I call my second chatter Doe as you can see in the image below.
Now Joe and Doe can chat between each other.
Just imagine if this virtual communication was made with a real device that can receive messages from your system or the other way around, as we gone demonstrate on the next example.
In this example, I am going to use one port to sending some random sensor data, and I will fetch the data on the other port.
This example is based on real scenario that I have used previous in one of my research and development projects. I was developing a hand gesture solution and my only prototype was not available, So I designed a software that emulate the data and send it as it was the hand gesture, but over my virtual com port.
Now in stead of have Chat as start-up project, set your first Visual Studio start-up project to Emulator and the second one to DataReceiver.
Now start-up your Emulator program (ctrl + F5) nothing should happen.
On your second Visual Studio start DataReceiver program and you should see that Emulator program is sending x, y, z data and it being collected by DataReceiver program.
You can also see the amount of data being sent and recevied by ports.
Now you should be able to parse your received data and make your programming logic with out having a real device.