DEV Community

Helgayork
Helgayork

Posted on

Serial port simulation with different programming languages

Main principles of serial port emulation

One of the major benefits of creating multiple virtual serial ports is the freedom afforded to developers to test and debug serial applications on computers that do not have any physical serial ports.

For instance, when developing a serial communication program the engineer will need to use at least two serial ports. Most computers on the market today have at most one serial port and many do not have any at all. Workarounds include using two machines or employing a PCI card to add a COM port to the system. Use of a serial port emulator lets a developer create as many virtual serial ports as needed on a single computer. This means that any machine that can run the emulation software can be used to develop serial software.

The emulation of COM interfaces means that software developers can dispense with complicated solutions to control resource sharing and interprocess communication. The speed of transmission between virtual ports is dependent on your system’s capacity and is not tied to the physical limitations of the serial ports.

The parameters of physical serial interfaces are fully replicated when using virtual COM ports, including strict baud-rate emulation.

Eltima Software’s Virtual COM Port Driver (VSPD) is a comprehensive virtual serial port emulator for Windows 7 as well as all later versions of the Windows OS. The tool provides the user with a robust virtual serial port emulator for 32 and 64 bit operating systems all wrapped in a very user-friendly and intuitive interface. Using this serial port emulator for Windows 10 takes advantage of the software being digitally signed with WHQL. VSPD is one of the most effective serial port emulation applications that are available to serial developers

Serial port simulation with different programming languages

Software developers know that in many cases, simulating behavior by using virtual devices can offer advantages over the use of physical devices when testing and debugging their code.

A good example is using a serial port emulator during the development of complicated serial communication systems. There are numerous virtual serial port emulators available that are usually used on the Windows operating system and allow developers to create virtual serial ports for testing purposes. Communication between RS232 or RS484 interfaces can be monitored and tested easily by using these serial port simulator applications.

Development of your own virtual port emulator is possible to assist you in monitoring serial transmissions between an application and physical serial ports. This can enable the testing of programs that usually interact with physical COM ports without requiring a physical connection to the port. Specialized apps can create a virtual null-modem cable that allows communication through pairs of virtual serial interfaces.

VSPD makes it easy to work with files and devices by supporting standard options like CreatePair, DeletePair, and SetStrictBaudrate. Here are some examples that illustrate incorporating the power of Virtual Serial Port Driver when using some pf the more popular programming languages in use today.

Delphi

Function declarations for Delphi are as follows:

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 statically or dynamically. To use dynamic loading, make use of this same code:

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;

C/C++

To use the virtual port emulator, after loading the DLL file dynamically choose and call your desired functions.. Here’s how it looks in Visual C++ :

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

In order to use VSPD with Visual Basic, paste these lines into your 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

If you store the VSPDCTL.DLL in your application’s directory, you can make direct function calls.

Top comments (0)