Serial Communication Programming with Virtual COM Ports

Serial port communication

Virtual serial ports are essential when legacy software programs need to connect to COM port devices that cannot be connected to the computer with the software due to a lack of physical serial ports. The device can also send serial data to the app over the Internet or LAN over TCP/IP connection no matter how remote they are from each other.

In this article, you’ll find a brief virtual serial port emulator tutorial that will tell you how to create virtual serial interfaces in your computer with the help of a simple but yet efficient virtual port driver software. And I hope that the information provided in this article will put you ahead of the more traditional forms of serial app testing and debugging.

Create Virtual Serial Port now

Contents:

  1. Using VSPD with programming languages
  2. VSPD with C/C++
  3. VSPD with Visual Basic
  4. VSPD with Delphi
  5. The advantages of Virtual Serial Port by Eltima
  6. FAQ about Serial Communication

Using VSPD with programming languages


The method of testing and diagnosing software using virtual devices today has become widely accepted by developers of the software for communication systems. A virtual device simulates behavior of a real one at the software level, allowing a programmer to test and debug an application at the development stage.

The method of serial port emulation is widely applied in the development of the most complex communication systems, including devices with RS232 and RS485 interfaces. Currently, there is a wide range of software tools that allow creating multiple virtual serial ports in operating systems (prevalently Windows OS).

The Windows operating system is a prime target for applications due to its tremendous popularity. Windows RS232 programming deals with the COM ports of the operating system through which all data flows in and out of the computer. Many languages can be used, but C is one of the main languages used for this type of programming. You will be called upon to use the WIN32 API and depending on your application may need to delve into the TAPI Windows telephony interface.

Linux is another very popular operating system with many variations in specific implementations. Your serial ports are named differently in Linux, with names such as /dev/ttys0 and /dev/ttyUSB0, so right away there are differences in how you approach Linux serial programming. C again is a popular language choice for serial programming and you will be interacting with devices that Linux treats as files. You will be using an API such as termios to communicate between your application and the serial ports.

The specialized nature of serial port programming may be outside the scope of your team’s expertise, and problems in communication between your application and its intended target devices can lead to a crippling delay in your development project. Problems such as unexpectedly corrupted text or connectivity issues can be hard to diagnose for programmers without extensive serial port experience.

With the help of additional virtual COM ports, developers can test data exchange between RS232 and RS485 interfaces. When it comes to advanced functionality and ease of use, Virtual Serial Port Emulator developed by Eltima Software seems to be the most powerful program available today. The solution is fully compatible with various versions of Windows OS. Above that, the serial port emulator for Windows 10 is digitally signed with WHQL which guarantees its performance and stability.

Create Virtual Serial Port now

VSPD with C/C++


To use virtual port emulator functionality in C and C++, it’s necessary to dynamically load the DLL file, select the required function(s), and call them. Here’s an example of how you can do this in Visual C++:

You can also see a great example of how to program a virtual COM port using Visual Basic and Visual C ++ for communication between AVR and PC using Virtual Serial Port Driver.

typedef bool (stdcall *CreatePairFn)(char *Port1, char *Port2);
typedef bool (stdcall *DeletePairFn)(char *Port);
typedef bool (stdcall *DeleteAllFn)(void);
typedef bool (stdcall *SetStrictBaudrateFn) (char *Port, bool StrictBaudrate);
typedef bool (stdcall *SetBreakFn) (char *Port, bool bBreak);
typedef bool (stdcall *QueryBusFn) (void* InBuffer, long sizeInBuffer, void* OutBuffer, long sizeOutBuffer);
typedef bool (stdcall *SetWiringFn) (char *Port, void *Buffer, long sizeBuffer);
typedef bool (stdcall *SetAccessListFn) (char *Port, void *Buffer, long sizeBuffer);
typedef bool (stdcall *CreatePairExFn)(char *Port1, char *Port2, char * UserSession);
typedef bool (stdcall *CreatePairWFn)(WCHAR *Port1, WCHAR *Port2);
typedef bool (stdcall *CreatePairExWFn)(WCHAR *Port1, WCHAR *Port2, WCHAR * UserSession);
typedef bool (stdcall *DeletePairWFn)(WCHAR *Port);
typedef bool (stdcall *SetStrictBaudrateWFn) (WCHAR *Port, bool StrictBaudrate);
typedef bool (stdcall *SetBreakWFn) (WCHAR *Port, bool bBreak);
typedef bool (stdcall *SetWiringWFn) (WCHAR *Port, void *Buffer, long sizeBuffer);
typedef bool (stdcall *SetAccessListWFn) (WCHAR *Port, void *Buffer, long sizeBuffer);
typedef bool (stdcall *DeletePairExFn)(char *Port, char * UserSession);
typedef bool (stdcall *DeletePairExWFn)(WCHAR *Port, WCHAR * UserSession);
typedef bool (stdcall *GetUserSessionFn)(char *Session, int &iSize);
typedef bool (stdcall *GetUserSessionWFn)(WCHAR *Session, int &iSize);
typedef bool (stdcall *SetStrictBaudrateExWFn) (WCHAR *Port, WCHAR * UserSession, bool StrictBaudrate);
typedef bool (stdcall *SetStrictBaudrateExFn) (char *Port, char * UserSession, bool StrictBaudrate);
typedef bool (stdcall *SetBreakExWFn) (WCHAR *Port, WCHAR * UserSession, bool bBreak);
typedef bool (stdcall *SetBreakExFn) (char *Port, char * UserSession, bool bBreak); typedef bool (stdcall *SetAccessListExWFn) (WCHAR *Port, WCHAR * WCHAR, void *Buffer, long sizeBuffer);
typedef bool (stdcall *SetAccessListExFn) (char *Port, char * WCHAR, void *Buffer, long sizeBuffer);

Code example:

bool CreateVSPair(char *Port1, char *Port2) {
OSVERSIONINFO VersionInfo;
HINSTANCE libInst;
libInst = LoadLibrary(VSPDCTL.DLL);
if (!libInst)
return false; /* Couldn't load library */
/* Substitute the typedefs above for functions other than CreatePairFn */
CreatePairFn CreatePair=(CreatePairFn)GetProcAddress(libInst, CreatePair);
if (CreatePair==0) return false; /* Couldn`t find function */
returnvalue=CreatePair(Port1, Port2); /* For example, Port1 = COM5 and Port2 = COM6 */
FreeLibrary(libInst);
return returnvalue;
};

VSPD with Visual Basic


VSPD functions installation is much easier for Visual Basic users. All you need to do is paste the following lines into the source file of Visual Basic:

Declare Function CreatePair Lib "VSPDCTL.DLL" (ByVal Port1$, ByVal Port2$) As Boolean
Declare Function DeletePair Lib "VSPDCTL.DLL" (ByVal Port$) As Boolean
Declare Function DeleteAll Lib "VSPDCTL.DLL" () As Boolean
Declare Function SetStrictBaudrateName Lib "VSPDCTL.DLL" (ByVal Port$, ByVal StrictBaudrate As Boolean) As Boolean
Declare Function SetStrictBaudrateHandle Lib "VSPDCTL.DLL" (ByVal Handle As Long, ByVal StrictBaudrate As Boolean) As Boolean
Declare Function CreatePair Lib "VSPDCTL.DLL" (ByVal Port1$, ByVal Port2$) As Boolean
Declare Function DeletePair Lib "VSPDCTL.DLL" (ByVal Port$) As Boolean
Declare Function DeleteAll Lib "VSPDCTL.DLL" () As Boolean
Declare Function SetStrictBaudrate Lib "VSPDCTL.DLL" (ByVal Port$, ByVal StrictBaudrate As Boolean) As Boolean
Declare Function SetBreak Lib "VSPDCTL.DLL" (ByVal Port$, ByVal bBreak As Boolean) As Boolean
Declare Function QueryBus Lib "VSPDCTL.DLL" (ByVal InBuffer As String, sizeInBuffer As Long, ByVal OutBuffer As String, sizeOutBuffer As Long) As Boolean
Declare Function SetWiring Lib "VSPDCTL.DLL" (ByVal Port1$, ByVal Buffer As String, sizeBuffer As Long) As Boolean

Put VSPDCTL.DLL in your program`s directory. Now the function can be called directly.


VSPD with Delphi


Here are the function declarations for Delphi:

Function CreatePair(Port1, Port2 : PChar) : Boolean; stdcall; external "VSPDCTL.DLL";
Function DeletePair(Port : PChar) : Boolean; stdcall; external "VSPDCTL.DLL";
Function DeleteAll : Boolean; stdcall; external "VSPDCTL.DLL";
Function SetStrictBaudrateName(Port: PChar, StrictBaudrate: Boolean) : Boolean; stdcall; external "VSPDCTL.DLL";
Function SetStrictBaudrateHandle(hPort: THandle, StrictBaudrate: Boolean) : Boolean; stdcall; external "VSPDCTL.DLL";
Function CreatePair(Port1, Port2 : PChar) : Boolean; stdcall; external "VSPDCTL.DLL";
Function DeletePair(Port : PChar) : Boolean; stdcall; external "VSPDCTL.DLL";
Function DeleteAll : Boolean; stdcall; external "VSPDCTL.DLL";
Function SetStrictBaudrate(Port: PChar, StrictBaudrate: Boolean) : Boolean; stdcall; external "VSPDCTL.DLL";
Function SetBreak (Port: PChar, bBreak: Boolean) : Boolean; stdcall; external "VSPDCTL.DLL";
Function QueryBus (InBuffer: Pointer, sizeInBuffer: Integer, OutBuffer: Pointer, sizeOutBuffer: Integer) : Boolean;
stdcall; external "VSPDCTL.DLL";
Function SetWiring (Port: PChar Buffer: Pointer, sizeBuffer: Integer) : Boolean; stdcall; external "VSPDCTL.DLL";

Delphi can load ArmAccess either dynamically or statically. Here is a sample code for dynamic loading:

Function CreatePairFn(PortName1, PortName2 : String) : Boolean;
Type TCreatePair = function(Port1, Port2 : PChar) : Boolean; stdcall;
Var Handle : THandle;
CreatePair : TCreatePair;
Begin
CreatePairFn := False;
Handle := LoadLibrary("VSPDCTL.DLL");
If (Handle <> 0) Then
Begin
@CreatePair:=GetProcAddress(Handle, "CreatePair");
If (CreatePair <> Nil) Then
CreatePairFn := CreatePair(PChar(PortName1), PChar(PortName2));
FreeLibrary(Handle);
End;
End;
Create Virtual Serial Port now

The advantages of Virtual Serial Port by Eltima


Virtual Serial Port Driver demonstrates the depth of knowledge and technical ability of Eltima’s programmers. Take look at some of the features and functions the team has brought to these applications, and think of what they can do for your project.

This software application allows you to create virtual pairs of serial ports and transfer data over virtual null modems. Virtual ports appear to your system just as if they were physical ports, and the program allows for easy configuration of your virtual ports.

Key benefits are:


  • With a Virtual Serial Port Driver, you’ll be to add an unlimited number of virtual COM port pairs linked via a virtual null-modem cable. What’s more, the software gives a way to remove all of the created pairs in one click.

  • Once created and configured virtual serial ports will be automatically re-created on system reboot prior to user login. Also, you won’t need to close ports if you need to change virtual pair settings or completely delete a pair.

  • The app makes it possible to not only create virtual COM ports but give them any desired names including those already used by the existing physical ports. Users will be able to easily find a required virtual interface in "Ports" group of the Device Manager.

  • The software drivers have successfully passed Microsoft Windows Hardware Quality Labs (WHQL) tests and are digitally signed by Microsoft. This means you won’t get any warnings when creating virtual COM ports on Windows 10, Windows Server 16, Windows XP, etc.

  • Virtual Serial Port Driver fully supports standard hardware signal lines including DTR/DSR/CTS/RTS/DCD/RI, etc. and software flow control (Xon/Xoff).

  • Null modem emulation is another advantage you get with this utility. Virtual Serial Port Driver allows emulating a virtual serial line connection and its break. Plus, the software offers various virtual null-modem schemes, which can be saved as a template.

  • On top of that, Virtual Serial Port Driver comes with vspdctl.dll, which allows you to control its functionality right from your own application. By using this dynamic link library, you’ll be able to create and delete virtual COM port pairs on the fly, with no need to rely on the standard configuration utility and its GUI.
Create Virtual Serial Port now

Frequently asked questions

A serial communication means that the data bits are organized in a specific order and are transported via a serial cable one after another, which ensures data integrity. Some other advantages of this type of connection include simple wiring and reliable data transmission over long-distance connections.
To create a virtual serial port in Windows 10, you need a professional software solution Virtual Serial Port Driver. It doesn't require any additional hardware and cables. In order to create a virtual serial port, read the step-by-step instructions below.
A virtual serial port is a software package that fully emulates real ones. So your operating system and all utilities recognize it and interact with it as though it were a real COM interface.
It often happens that when working, the user is faced with the problem of physical lack of a com port. So a major advantage of virtual serial interfaces is that they can be used for testing and debugging software when there is not a physical COM port available in the system.
1. The software connects virtual serial ports with a virtual null-modem cable so that the COM ports can be used to communicate data between different applications over the RS232 interface. Configured for this purpose, virtual ports do not use the default settings of the data transfer speed.

2. Thanks to a serial port simulator it’s possible to exchange data between two or more applications without using the complex software technology of interprocess communication and resource sharing. That way, to provide data exchange, a programmer relies on virtual COM ports rather than physical ones. As a rule, the speed of data transfer via virtual COM ports is higher compared to data transmission via real serial ports.

3. Moreover, a major advantage of virtual serial interfaces is that they can be used for testing and debugging software when there is not a physical COM port available in the system.

The specialists at Eltima Software can help with any of your Windows or Linux serial port programming needs. We are skilled in many programming languages and OS platforms and can work with you to seamlessly integrate your application and the external devices it needs to access. Eltima Port Virtualization Technology can be integrated to the projects of any size. If you need some problem procedures streamlined or want full scale project management and application development, Eltima Software has the skills you need to get your project up and running. Get in touch with them today and find out how they can help solve your serial programming issues and keep your project on schedule.

small logo Virtual Serial Port Driver
#1 at Serial Port Software
Virtual Serial Port Driver
Create virtual COM port pairs in Windows
5 rank based on 86+ users
Get a download link for your desktop
Submit your email address to get a link for quick download on your desktop and get started!
or