Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In Fedora I run the socat command:
socat -d -d pty,link=/dev/ttyS0,raw,echo=0 tcp-listen:1234
so I can receive strings from a TCP Client (created with QT) and forward them to the serial port ttyS0.
In the TCP Client I created a virtual driver too using this code:
C++
<pre lang="c++">
#include <windows.h>

HANDLE hdriver = CreateFileA("\\.\COM12", GENERIC_READ | GENERIC_WRITE, 0, 0, CREATE_NEW, 0, 0); // Create driver
hdriver = CreateFileA("\\.\COM12",  GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); // Open driver
if(hdriver == INVALID_HANDLE_VALUE)
{
    qDebug() << GetLastError();
    CloseHandle(hdriver);
    return 0;
}
else
{
    qDebug("Open");
    qDebug() << hdriver;
    return 1;
}
That I want now is create a virtual serial port over the driver that I created.
Is possible in c++?

What I have tried:

I create the TCP Client and I create the driver
Posted
Updated 9-Oct-18 4:25am

1 solution

You should clarify your question.

A virtual serial port can be only "created" by providing a driver which is a system component and can't be therefore implemented by a normal application.

Even the socat examples uses an existing serial port (/dev/ttyS0). If that is virtual or physical does not care. But it must exist already.

Yes, it would be possible to implement something similar to the socat example with C++ on Windows. But that requires an existing serial port (again: it does not care if it is physical or virtual). Then you just have to implement functions that reads on the endpoints (socket and serial for bi-directional transfers; typically threaded and event driven) which writes the received data to the other endpoint.

But you can't create a new virtual serial port from an application. Even if that would be possible it would not work with Windows because serial ports are exclusive resources (can be only opened by one application at time).
 
Share this answer
 
Comments
Afzaal Ahmad Zeeshan 20-Sep-17 7:59am    
5ed.
Jacopo Gardin 20-Sep-17 11:31am    
What does "5ed" means?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900