share serial port between two different applications C#

C#

Main Application(){ 
Statement 1;
statement 2;
Write->COMPort1;
.........
Write->COMPort1;
..........
Write->COMPort1;
..........
Thread(){
While(true)
Read->COMPort1;    
        }
}

Here a main program is writing to some serial port repeatedly. and a thread is reading the same serial port independently. How to execute this behavior without conflict?

1 answer

  • answered 2018-11-08 08:05 TheGeneral

    In short you cant share a serial port between 2 applications as per your title.

    The fundamental problem is you cannot open the same port twice. You will either need to :

    1. Create a server application that serves the 2 clients, which broadcasts received data and can take requests

    2. Use a dedicated serial port splitter, for which you just need to search google and i have no idea how well the work.

    3. Or write your own driver

    As for 2 threads sharing the same port for reading and writing.

    Just use a lock if you have a concurrency problem, You'll find the receivedData event (or what ever its called) returns on its own thread anyway.