<

[Webinar] Learn how to a build a cloud-first strategyRegister Now

x

Serial Port Emulation.  Learn how to use Virtual Port Emulator.

0 Points
19 Views
Last Modified:
The article will help you learn the basic principles of serial port emulation: how to use virtual serial ports, examples of using VSPD with programming language.

There’s no denying that one of the most widely applied modern methods of testing and diagnosing software under development is based on using virtual devices which are capable of simulating the behavior of real ones.


Many developers of complex serial communication systems rely on the method of COM port emulation. With a wide variety of software solutions able to provide an operating system (mainly Windows OS) with multiple virtual serial ports, today programmers have all necessary tools to test and monitor COM port communication between RS232 and RS485 interfaces.


Even when an application you’re developing is meant to work with hardware COM ports resided on client machines, testing of the program can be done even without connecting to real serial ports. There are dedicated software that make it possible to create pairs of virtual COM ports communicating through a virtual null-modem cable. Alternatively, in case you have all necessary resources, you can develop serial port emulator yourself. This way, you’ll have the required tool to simulate the data flow between your application and any hardware serial port.


Serial port emulation: main principles


1. Having multiple serial ports at their disposal, programmer get the ability to diagnose and debug software on a system fitting no real COM ports.


For example, to debug an application developed for serial communication, a programmer usually relies on at least two physical COM ports. With current computers providing one serial port at the most, for this purpose, a developer requires either two separate machines or a PCI expansion card with a COM port embedded. As an option, a programmer can use serial port emulator which will create multiple COM ports on a single computer. This way, the concept of virtual ports can be applied when developing any serial communication application and not only.


2. The method of COM port emulation relieves developers of the need to use the complex software technology of interprocess communication and resource sharing. Considering that the data transfer is done via virtual COM ports, the speed of data transmission will depend on the specifications of the system but not a COM port itself.


3. Virtual serial ports simulate all settings of physical COM ports and provide strict baudrate emulation. 


Virtual Serial Port Driver (VSPD) by Eltima Software  combines powerful functionality with simple and clean interface. The app provides full support for different versions of Windows operating system. Plus, the software edition for Windows 10 is digitally signed with WHQL, which makes the performance of the program even more stable. All these and many other factors make Virtual Serial Port Driver one of the most efficient solutions for serial port emulation.


Serial port simulation with different programming languages


Working with files and devices, VSPD supports standard options, including CreatePair, DeletePair, SetStrictBaudrate, and others. Here are some examples of using Virtual Serial Port Driver with major programming languages:


C/C++

To take advantage of the functionality of virtual port emulator, you should load the DLL file dynamically, choose and call the required function(s). In Visual C++ it looks like this:


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);
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;
};

Visual Basic


To install the functions of VSPD with Visual Basic, it’s necessary to paste the following lines into the source file:


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

Once the VSPDCTL.DLL is put in your program`s directory, you can call the functions directly.


Delphi


Now, let’s see 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";

ArmAccess can be loaded 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;


0
Comment
0 Comments

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Join & Write a Comment

The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
Suggested Courses

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month