PDA

View Full Version : Share serial port between two programs on Windows



RazZziel
26th January 2012, 18:20
I'm using QExtSerialPort in my application. On Linux, when I open two instances of my program, configured with the same serial port, both instances are able to open the port without problems.

However, on Windows, the second instance fails to open the port, as the first instance has exclusive access. Is there any way to workaround this limitation (without a virtual serial port if possible), or is it impossible on Windows? Does QSerialDevice behave differently?

Thanks in advance,
Ismael

kuzulis
26th January 2012, 18:54
In Windows, you can not open a single port simultaneously by multiple applications.
It is impossible, it is a "feature" Windows.

For these purposes, you can use this solution:
1. There are some single application(manager/server), which opens / closes the necessary ports by requests from other applications, and returns for this applications a open ports descriptors.
(ie provides an interface for queries like:
bool openPort (portName)
bool closePort (portName or descriptor)
HANDLE getDescriptor (portName)
and etc)

2. There are some applications(clients) that make requests to the server-application with a command to open/close port descriptor and perform I / O through these received descriptors.
(ie provides an interface for I/O like:
write(descriptor, ...)
read(descriptor, ...)
and etc)

3. Exchange of data between applications (client-server) or by a QLocalSocket or etc.
4. Must somehow take into account all the nuances associated with simultaneous access to I / O from clients, or to repeated requests for opening / closing.
In general, you have to think everything through very well.

To get a descriptor using libraries QextSerialPort or QSerialDevice need to patch/modify and add a new method of type getDescriptor().

Library QSerialDevice provide only exclusive access to a port to all platforms. This is done on purpose for typization/unification.

marcvanriet
26th January 2012, 18:57
That is impossible afaik on windows. QSerialDevice cannot open the same port twice either.

Regards,
Marc

RazZziel
26th January 2012, 19:25
Thanks a lot for the quick reply guys,

Some time ago to solve this I tried to use virtual COM ports, in particular with com0com and hub4com), but sometimes on concurrent accesses the whole thing hanged, the serial port seemed to become sort of stalled, and I had to reboot Windows.

Do you know of any reliable alternative? Once I implemented the solution kuzulis proposed, for a particular problem, but this time I need the solution to be more transparent to the regular serial port access, so either I use virtual ports (which are no more than kuzulis' solution but with virtual devices involved), or I try to only open the port when I use it and close it right away afterwards (dirty, but implemented it once and worked more or less nicely).

Jeopardy
20th July 2017, 13:06
Well, you can use something like Serial Splitter https://www.eltima.com/products/serialsplitter/, which creates virtual serial ports connected with the real one. Then you can open each virtual serial port in your applications.